restinio::http_field_parsers::basic_auth Namespace Reference

## Namespaces

namespace | impl

## Classes

struct | params_t
| Parameters for basic authentification. More...

## Enumerations

enum class | extraction_error_t { no_auth_http_field , illegal_http_field_value , not_basic_auth_scheme , invalid_basic_auth_param , token68_decode_error , invalid_username_password_pair , empty_username }
| Error codes for failures of extraction of basic authentification parameters. More...

## Functions

RESTINIO_NODISCARD string_view_t | to_string_view (extraction_error_t what) noexcept
| Helper function to get a string name of extraction_error enum. More...
RESTINIO_NODISCARD expected_t< params_t, extraction_error_t > | try_extract_params (const authorization_value_t &http_field)
| Helper function for getting parameters of basic authentification from an already parsed HTTP-field. More...
RESTINIO_NODISCARD expected_t< params_t, extraction_error_t > | try_extract_params (const http_header_fields_t &fields, string_view_t auth_field_name)
| Helper function for getting parameters of basic authentification from a set of HTTP-fields. More...
template<typename Extra_Data >
RESTINIO_NODISCARD expected_t< params_t, extraction_error_t > | try_extract_params (const generic_request_t< Extra_Data > &req, string_view_t auth_field_name)
| Helper function for getting parameters of basic authentification from a request. More...
RESTINIO_NODISCARD expected_t< params_t, extraction_error_t > | try_extract_params (const http_header_fields_t &fields, http_field_t auth_field_id)
| Helper function for getting parameters of basic authentification from a set of HTTP-fields. More...
template<typename Extra_Data >
RESTINIO_NODISCARD expected_t< params_t, extraction_error_t > | try_extract_params (const generic_request_t< Extra_Data > &req, http_field_t auth_field_id)
| Helper function for getting parameters of basic authentification from a request. More...

## Enumeration Type Documentation

## extraction_error_t

enum class restinio::http_field_parsers::basic_auth::extraction_error_t

strong

Error codes for failures of extraction of basic authentification parameters.

Since
  v.0.6.7

Enumerator
no_auth_http_field | There is no HTTP field with authentification parameters.
illegal_http_field_value | The HTTP field with authentification parameters can't be parsed.
not_basic_auth_scheme | Different authentification scheme found. Basic authentification scheme is expected.
invalid_basic_auth_param | Invalid value of parameter for basic authentification scheme. The single parameter in the form of token68 is expected.
token68_decode_error | Value of token68 parameter for basic authentification can't be decoded.
invalid_username_password_pair | Wrong format for username:password in decoded token68 parameter. Maybe there is no colon symbol.
empty_username | Empty user name in username:password pair.

Definition at line 65 of file basic_auth.hpp.

## Function Documentation

## to_string_view()

RESTINIO_NODISCARD string_view_t restinio::http_field_parsers::basic_auth::to_string_view | ( | extraction_error_t | what | ) |

inline noexcept

Helper function to get a string name of extraction_error enum.

Since
  v.0.6.9

Definition at line 99 of file basic_auth.hpp.

## try_extract_params() [1/5]

RESTINIO_NODISCARD expected_t< params_t, extraction_error_t > restinio::http_field_parsers::basic_auth::try_extract_params | ( | const authorization_value_t & | http_field | ) |

inline

Helper function for getting parameters of basic authentification from an already parsed HTTP-field.

Attention
  This function doesn't check the content of authorization_value_t::auth_scheme. It's expected that this field was checked earlier.

Usage example:

auto on_request(restinio::request_handle_t & req) {
using namespace restinio::http_field_parsers;
const auto opt_field = req.header().opt_value_of(
restinio::http_field::authorization);
if(opt_field) {
const auto parsed_field = authorization_value_t::try_parse(*opt_field);
if(parsed_field) {
if("basic" == parsed_field->auth_scheme) {
using namespace restinio::http_field_parsers::basic_auth;
const auto basic_params = try_extract_params(*parsed_field);
if(basic_params) {
const std::string & username = auth_params->username;
const std::string & password = auth_params->password;
... // Do something with username and password.
}
}
else if("bearer" == parsed_field->auth_scheme) {
... // Dealing with bearer authentification.
}
else {
... // Other authentification schemes.
}
}
}
...
}
restinio::http_field_parsers::basic_auth
Definition: basic_auth.hpp:31
restinio::http_field_parsers::basic_auth::try_extract_params
RESTINIO_NODISCARD expected_t< params_t, extraction_error_t > try_extract_params(const authorization_value_t &http_field)
Helper function for getting parameters of basic authentification from an already parsed HTTP-field.
Definition: basic_auth.hpp:183
restinio::http_field_parsers
Definition: accept-charset.hpp:20
restinio::request_handle_t
std::shared_ptr< request_t > request_handle_t
An alias for handle for incoming request without additional extra-data.
Definition: request_handler.hpp:463
restinio::http_field_parsers::authorization_value_t::try_parse
static RESTINIO_NODISCARD expected_t< authorization_value_t, restinio::easy_parser::parse_error_t > try_parse(string_view_t what)
An attempt to parse Authorization HTTP-field.
Definition: authorization.hpp:239

Since
  v.0.6.8

Examples
  sample/chained_handlers/main.cpp, and sample/extra_data_factory/main.cpp.

Definition at line 183 of file basic_auth.hpp.

## try_extract_params() [2/5]

template<typename Extra_Data >

RESTINIO_NODISCARD expected_t< params_t, extraction_error_t > restinio::http_field_parsers::basic_auth::try_extract_params | ( | const generic_request_t< Extra_Data > & | req,
|  | http_field_t | auth_field_id
| ) |  |

inline

Helper function for getting parameters of basic authentification from a request.

Usage example:

auto on_request(restinio::request_handle_t & req) {
using namespace restinio::http_field_parsers::basic_auth;
const auto auth_params = try_extract_params(
*req, restinio::http_field::authorization);
if(auth_params) {
const std::string & username = auth_params->username;
const std::string & password = auth_params->password;
... // Do something with username and password.
}
...
}

Since
  v.0.6.7

Parameters

  req | A request that should hold a HTTP-field with authentification parameters.
  auth_field_id | The ID of a HTTP-field with authentification parameters.

Definition at line 362 of file basic_auth.hpp.

## try_extract_params() [3/5]

template<typename Extra_Data >

RESTINIO_NODISCARD expected_t< params_t, extraction_error_t > restinio::http_field_parsers::basic_auth::try_extract_params | ( | const generic_request_t< Extra_Data > & | req,
|  | string_view_t | auth_field_name
| ) |  |

inline

Helper function for getting parameters of basic authentification from a request.

This helper function is intended to be used for cases when authentification parameters are stored inside a HTTP-field with a custom name. For example:

auto on_request(restinio::request_handle_t & req) {
using namespace restinio::http_field_parsers::basic_auth;
const auto auth_params = try_extract_params(*req, "X-My-Authorization");
if(auth_params) {
const std::string & username = auth_params->username;
const std::string & password = auth_params->password;
... // Do something with username and password.
}
...
}

Since
  v.0.6.7

Parameters

  req | A request that should hold a HTTP-field with authentification parameters.
  auth_field_name | The name of a HTTP-field with authentification parameters.

Definition at line 295 of file basic_auth.hpp.

## try_extract_params() [4/5]

RESTINIO_NODISCARD expected_t< params_t, extraction_error_t > restinio::http_field_parsers::basic_auth::try_extract_params | ( | const http_header_fields_t & | fields,
|  | http_field_t | auth_field_id
| ) |  |

inline

Helper function for getting parameters of basic authentification from a set of HTTP-fields.

Usage example:

auto check_authorization(const restinio::http_header_fields_t & fields) {
using namespace restinio::http_field_parsers::basic_auth;
const auto auth_params = try_extract_params(
fields, restinio::http_field::authorization);
if(auth_params) {
const std::string & username = auth_params->username;
const std::string & password = auth_params->password;
... // Do something with username and password.
}
...
}
restinio::http_header_fields_t
Header fields map.
Definition: http_headers.hpp:703

Since
  v.0.6.9

Parameters

  fields | A set of HTTP-fields.
  auth_field_id | The ID of a HTTP-field with authentification parameters.

Definition at line 328 of file basic_auth.hpp.

## try_extract_params() [5/5]

RESTINIO_NODISCARD expected_t< params_t, extraction_error_t > restinio::http_field_parsers::basic_auth::try_extract_params | ( | const http_header_fields_t & | fields,
|  | string_view_t | auth_field_name
| ) |  |

inline

Helper function for getting parameters of basic authentification from a set of HTTP-fields.

This helper function is intended to be used for cases when authentification parameters are stored inside a HTTP-field with a custom name. For example:

auto check_authorization(const restinio::http_header_fields_t & fields) {
using namespace restinio::http_field_parsers::basic_auth;
const auto auth_params = try_extract_params(fields, "X-My-Authorization");
if(auth_params) {
const std::string & username = auth_params->username;
const std::string & password = auth_params->password;
... // Do something with username and password.
}
...
}

Since
  v.0.6.9

Parameters

  fields | A set of HTTP-fields.
  auth_field_name | The name of a HTTP-field with authentification parameters.

Definition at line 261 of file basic_auth.hpp.