restinio::http_field_parsers::bearer_auth Namespace Reference

## Namespaces

namespace | impl

## Classes

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

## Enumerations

enum class | extraction_error_t { no_auth_http_field , illegal_http_field_value , not_bearer_auth_scheme , invalid_bearer_auth_param }
| Error codes for failures of extraction of bearer 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 bearer authentification from an already parsed HTTP-field. More...
RESTINIO_NODISCARD expected_t< params_t, extraction_error_t > | try_extract_params (authorization_value_t &&http_field)
| Helper function for getting parameters of bearer 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 bearer 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 bearer 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 bearer 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 bearer authentification from a request. More...

## Enumeration Type Documentation

## extraction_error_t

enum class restinio::http_field_parsers::bearer_auth::extraction_error_t

strong

Error codes for failures of extraction of bearer authentification parameters.

Since
  v.0.6.7.1

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_bearer_auth_scheme | Different authentification scheme found. bearer authentification scheme is expected.
invalid_bearer_auth_param | Invalid value of parameter for bearer authentification scheme. The single parameter in the form of b64token is expected.

Definition at line 57 of file bearer_auth.hpp.

## Function Documentation

## to_string_view()

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

inlinenoexcept

Helper function to get a string name of extraction_error enum.

Since
  v.0.6.9

Definition at line 81 of file bearer_auth.hpp.

## try_extract_params() [1/6]

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

inline

Helper function for getting parameters of bearer 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.

Note
  This function can be used if one wants to avoid memory allocation and can reuse value of auth_params.

Usage example (please note that `const` is not used in code when authorization HTTP-field is parsed):

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) {
// parsed_field is a mutable object.
// The content of parsed_field->auth_param can be moved out.
auto parsed_field = authorization_value_t::try_parse(*opt_field);
if(parsed_field) {
if("basic" == parsed_field->auth_scheme) {
... // Dealing with Basic authentification scheme.
}
else if("bearer" == parsed_field->auth_scheme) {
using namespace restinio::http_field_parsers::bearer_auth;
const auto bearer_params =
// Please note the usage of std::move here.
try_extract_params(std::move(*parsed_field));
if(bearer_params) {
const std::string & token = auth_params->token;
... // Do something with token.
}
}
else {
... // Other authentification schemes.
}
}
}
...
}
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:465
restinio::http_field_parsers::bearer_auth
Definition: bearer_auth.hpp:29
restinio::http_field_parsers::bearer_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 bearer authentification from an already parsed HTTP-field.
Definition: bearer_auth.hpp:152
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

Definition at line 214 of file bearer_auth.hpp.

## try_extract_params() [2/6]

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

inline

Helper function for getting parameters of bearer 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) {
... // Dealing with Basic authentification scheme.
}
else if("bearer" == parsed_field->auth_scheme) {
using namespace restinio::http_field_parsers::bearer_auth;
const auto bearer_params = try_extract_params(*parsed_field);
if(bearer_params) {
const std::string & token = auth_params->token;
... // Do something with token.
}
}
else {
... // Other authentification schemes.
}
}
}
...
}

Since
  v.0.6.8

Definition at line 152 of file bearer_auth.hpp.

## try_extract_params() [3/6]

template<typename Extra_Data >

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

inline

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

Usage example:

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

Since
  v.0.6.7.1

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 373 of file bearer_auth.hpp.

## try_extract_params() [4/6]

template<typename Extra_Data >

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

inline

Helper function for getting parameters of bearer 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::bearer_auth;
const auto auth_params = try_extract_params(*req, "X-My-Authorization");
if(auth_params) {
const std::string & token = auth_params->token;
... // Do something with token.
}
...
}

Since
  v.0.6.7.1

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 308 of file bearer_auth.hpp.

## try_extract_params() [5/6]

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

inline

Helper function for getting parameters of bearer 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::bearer_auth;
const auto auth_params = try_extract_params(
fields, restinio::http_field::authorization);
if(auth_params) {
const std::string & token = auth_params->token;
... // Do something with token.
}
...
}
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 340 of file bearer_auth.hpp.

## try_extract_params() [6/6]

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

inline

Helper function for getting parameters of bearer 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::bearer_auth;
const auto auth_params = try_extract_params(fields, "X-My-Authorization");
if(auth_params) {
const std::string & token = auth_params->token;
... // Do something with token.
}
...
}

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 275 of file bearer_auth.hpp.