/*
 * Copyright (C) 2023 Hasl Developers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <https://www.gnu.org/licenses/>.
 */

#ifndef HASL_GS2_HEADER_H
#define HASL_GS2_HEADER_H

#include <glib.h>
#include <glib-object.h>

#include <hasl/haslversion.h>

G_BEGIN_DECLS

/**
 * HaslGs2HeaderChannelBinding:
 * @HASL_GS2_HEADER_CHANNEL_BINDING_CLIENT_NOT_SUPPORTED: The client doesn't
 *  support channel binding. This is the 'n' value.
 * @HASL_GS2_HEADER_CHANNEL_BINDING_CLIENT_SUPPORTED: The client supports
 *  channel binding and has specified a name. This is the 'p' value.
 * @HASL_GS2_HEADER_CHANNEL_BINDING_CLIENT_SUPPORTED_SERVER_UNKNOWN: The client
 *  supports channel binding, doesn't know if the server does. This is the 'y'
 *  value.
 * @HaslGs2HeaderChannelBindingClientNotSupported: deprecated use
 *  HASL_GS2_HEADER_CHANNEL_BINDING_CLIENT_NOT_SUPPORTED.
 * @HaslGs2HeaderChannelBindingClientSupported: deprecated use
 *  HASL_GS2_HEADER_CHANNEL_BINDING_CLIENT_SUPPORTED.
 * @HaslGs2HeaderChannelBindingClientSupportedServerUnknown: deprecated use
 *  HASL_GS2_HEADER_CHANNEL_BINDING_CLIENT_SUPPORTED_SERVER_UNKNOWN.
 *
 * This enum tells the header about channel bindings for this connection. See
 * https://www.rfc-editor.org/rfc/rfc5801.html#section-4 for more information.
 *
 * Since: 0.3
 */
typedef enum {
	HASL_GS2_HEADER_CHANNEL_BINDING_CLIENT_NOT_SUPPORTED HASL_AVAILABLE_ENUMERATOR_IN_0_5,
	HASL_GS2_HEADER_CHANNEL_BINDING_CLIENT_SUPPORTED HASL_AVAILABLE_ENUMERATOR_IN_0_5,
	HASL_GS2_HEADER_CHANNEL_BINDING_CLIENT_SUPPORTED_SERVER_UNKNOWN HASL_AVAILABLE_ENUMERATOR_IN_0_5,
	HaslGs2HeaderChannelBindingClientNotSupported HASL_DEPRECATED_ENUMERATOR_IN_0_5_FOR(HASL_GS2_HEADER_CHANNEL_BINDING_CLIENT_NOT_SUPPORTED) = HASL_GS2_HEADER_CHANNEL_BINDING_CLIENT_NOT_SUPPORTED,
	HaslGs2HeaderChannelBindingClientSupported HASL_DEPRECATED_ENUMERATOR_IN_0_5_FOR(HASL_GS2_HEADER_CHANNEL_BINDING_CLIENT_SUPPORTED) = HASL_GS2_HEADER_CHANNEL_BINDING_CLIENT_SUPPORTED,
	HaslGs2HeaderChannelBindingClientSupportedServerUnknown HASL_DEPRECATED_ENUMERATOR_IN_0_5_FOR(HASL_GS2_HEADER_CHANNEL_BINDING_CLIENT_SUPPORTED_SERVER_UNKNOWN) = HASL_GS2_HEADER_CHANNEL_BINDING_CLIENT_SUPPORTED_SERVER_UNKNOWN,
} HaslGs2HeaderChannelBinding;

#define HASL_GS2_HEADER_ERROR hasl_gs2_header_error_quark()

/**
 * hasl_gs2_header_error_quark:
 *
 * The error domain to identify errors with gs2 headers.
 *
 * Returns: The error domain.
 *
 * Since: 0.5
 */
HASL_AVAILABLE_IN_0_5
GQuark hasl_gs2_header_error_quark(void);

/**
 * HaslGs2HeaderError:
 * @HASL_GS2_HEADER_ERROR_CB_FLAG_UNKNOWN: the channel binding flag value is
 *  unknown.
 * @HASL_GS2_HEADER_ERROR_CBNAME_NOT_SPECIFIED: the channel binding setting was
 *  not specified
 * @HASL_GS2_HEADER_ERROR_CBNAME_UNEXPECTED: the channel binding setting was
 *  specified but was unexpected
 * @HASL_GS2_HEADER_ERROR_CBNAME_INVALID: the channel binding name is invalid
 *
 * Error codes returned for gs2 headers.
 *
 * Since: 0.5
 */
typedef enum {
	HASL_GS2_HEADER_ERROR_CB_FLAG_UNKNOWN HASL_AVAILABLE_ENUMERATOR_IN_0_5,
	HASL_GS2_HEADER_ERROR_CBNAME_NOT_SPECIFIED HASL_AVAILABLE_ENUMERATOR_IN_0_5,
	HASL_GS2_HEADER_ERROR_CBNAME_UNEXPECTED HASL_AVAILABLE_ENUMERATOR_IN_0_5,
	HASL_GS2_HEADER_ERROR_CBNAME_INVALID HASL_AVAILABLE_ENUMERATOR_IN_0_5,
} HaslGs2HeaderError;

/**
 * HaslGs2Header:
 *
 * This is an object to make it easier to generate a GS2 Header. See
 * https://www.rfc-editor.org/rfc/rfc5801#section-4 for the formal definition.
 *
 * This is primarily meant to be used by mechanism implementations and should
 * not be needed for normal use cases.
 *
 * Since: 0.3
 */
#define HASL_TYPE_GS2_HEADER (hasl_gs2_header_get_type())

HASL_AVAILABLE_IN_0_3
G_DECLARE_FINAL_TYPE(HaslGs2Header, hasl_gs2_header, HASL, GS2_HEADER, GObject)

/**
 * hasl_gs2_header_new:
 *
 * Creates a new gs2_header.
 *
 * Returns: (transfer full): The new gs2_header.
 *
 * Since: 0.3
 */
HASL_AVAILABLE_IN_0_3
HaslGs2Header *hasl_gs2_header_new(void);

/**
 * hasl_gs2_header_parse:
 * @str: the string to parse
 * @error: (out) (nullable): a return address for a #GError
 *
 * Parses a string into a GS2 Header.
 *
 * Returns: (transfer full) (nullable): The new header if found.
 *
 * Since: 0.5
 */
HASL_AVAILABLE_IN_0_5
HaslGs2Header *hasl_gs2_header_parse(const char *str, GError **error);

/**
 * hasl_gs2_header_parse_full:
 * @str: the string to parse
 * @length: (out) (optional): the length of the string that matched.
 * @error: (out) (nullable): a return address for a #GError
 *
 * Parses a string into a GS2 Header.
 *
 * The length parameter can be used to determine the length of the match when
 * the header is part of a larger string.
 *
 * Returns: (transfer full) (nullable): The new header if found.
 *
 * Since: 0.5
 */
HASL_AVAILABLE_IN_0_5
HaslGs2Header *hasl_gs2_header_parse_full(const char *str, int *length, GError **error);

/**
 * hasl_gs2_header_to_string:
 * @header: The instance.
 * @error: (out): A return address for an error.
 *
 * Serializes @header to a string in the correct format.
 *
 * Returns: (transfer full): The serialized header.
 *
 * Since: 0.3
 */
HASL_AVAILABLE_IN_0_3
char *hasl_gs2_header_to_string(HaslGs2Header *header, GError **error);

/**
 * hasl_gs2_header_get_standard_mechanism:
 * @header: The instance.
 *
 * Gets whether or not this header is for a standard GSS-API mechanism.
 *
 * Returns: %TRUE if this mechanism is standard, %FALSE otherwise.
 *
 * Since: 0.3
 */
HASL_AVAILABLE_IN_0_3
gboolean hasl_gs2_header_get_standard_mechanism(HaslGs2Header *header);

/**
 * hasl_gs2_header_set_standard_mechanism:
 * @header: The instance.
 * @standard: The new value.
 *
 * Sets whether or not the mechanism is a standard GSS-API mechanism.
 *
 * Since: 0.3
 */
HASL_AVAILABLE_IN_0_3
void hasl_gs2_header_set_standard_mechanism(HaslGs2Header *header, gboolean standard);

/**
 * hasl_gs2_header_get_channel_binding:
 * @header: The instance.
 *
 * Gets the channel binding value for this header.
 *
 * Returns: The channel binding value.
 *
 * Since: 0.3
 */
HASL_AVAILABLE_IN_0_3
HaslGs2HeaderChannelBinding hasl_gs2_header_get_channel_binding(HaslGs2Header *header);

/**
 * hasl_gs2_header_set_channel_binding:
 * @header: The instance.
 * @channel_binding: The channel binding flag.
 *
 * Sets the channel binding flag for @header.
 *
 * Since: 0.3
 */
HASL_AVAILABLE_IN_0_3
void hasl_gs2_header_set_channel_binding(HaslGs2Header *header, HaslGs2HeaderChannelBinding channel_binding);

/**
 * hasl_gs2_header_get_channel_binding_name:
 * @header: The instance.
 *
 * Gets the name of the channel binding that is in use.
 *
 * Returns: (nullable): The channel binding name.
 *
 * Since: 0.3
 */
HASL_AVAILABLE_IN_0_3
const char *hasl_gs2_header_get_channel_binding_name(HaslGs2Header *header);

/**
 * hasl_gs2_header_set_channel_binding_name:
 * @header: The instance.
 * @name: (nullable): The name of the channel binding data.
 *
 * Sets the name of the channel binding data that should be used.
 *
 * Since: 0.3
 */
HASL_AVAILABLE_IN_0_3
void hasl_gs2_header_set_channel_binding_name(HaslGs2Header *header, const char *name);

/**
 * hasl_gs2_header_get_authzid:
 * @header: The instance.
 *
 * Gets the authzid to use for this header.
 *
 * Returns: (nullable): The authzid for this header.
 *
 * Since: 0.3
 */
HASL_AVAILABLE_IN_0_3
const char *hasl_gs2_header_get_authzid(HaslGs2Header *header);

/**
 * hasl_gs2_header_set_authzid:
 * @header: The instance.
 * @authzid: (nullable): The username to authenticate for.
 *
 * Sets the username to authenticate for. This is used when an administrator
 * or moderator needs to authenticate as a specific user using their own
 * credentials.
 *
 * Since: 0.3
 */
HASL_AVAILABLE_IN_0_3
void hasl_gs2_header_set_authzid(HaslGs2Header *header, const char *authzid);

G_END_DECLS

#endif /* HASL_GS2_HEADER_H */
