bearer_auth.hpp
Go to the documentation of this file.
/*
* RESTinio
*/
#pragma once
#include <restinio/helpers/http_field_parsers/authorization.hpp>
#include <restinio/http_headers.hpp>
#include <restinio/request_handler.hpp>
#include <restinio/expected.hpp>
#include <iostream>
namespace restinio
{
namespace http_field_parsers
{
namespace bearer_auth
{
//
// params_t
//
struct params_t
{
std::string token;
};
//
// extraction_error_t
//
enum class extraction_error_t
{
no_auth_http_field,
illegal_http_field_value,
not_bearer_auth_scheme,
invalid_bearer_auth_param,
};
RESTINIO_NODISCARD
inline string_view_t
to_string_view( extraction_error_t what ) noexcept
{
string_view_t result{ "<unknown>" };
switch( what )
{
case extraction_error_t::no_auth_http_field:
result = string_view_t{ "no_auth_http_field" };
break;
case extraction_error_t::illegal_http_field_value:
result = string_view_t{ "illegal_http_field_value" };
break;
case extraction_error_t::not_bearer_auth_scheme:
result = string_view_t{ "not_bearer_auth_scheme" };
break;
case extraction_error_t::invalid_bearer_auth_param:
result = string_view_t{ "invalid_bearer_auth_param" };
break;
}
return result;
}
//
// try_extract_params
//
RESTINIO_NODISCARD
inline expected_t< params_t, extraction_error_t >
try_extract_params(
const authorization_value_t & http_field )
{
const auto * b64token = get_if<authorization_value_t::token68_t>(
&http_field.auth_param );
if( !b64token )
return make_unexpected( extraction_error_t::invalid_bearer_auth_param );
return params_t{ b64token->value };
}
RESTINIO_NODISCARD
inline expected_t< params_t, extraction_error_t >
try_extract_params(
authorization_value_t && http_field )
{
auto * b64token = get_if<authorization_value_t::token68_t>(
&http_field.auth_param );
if( !b64token )
return make_unexpected( extraction_error_t::invalid_bearer_auth_param );
return params_t{ std::move(b64token->value) };
}
namespace impl
{
RESTINIO_NODISCARD
inline expected_t< params_t, extraction_error_t >
perform_extraction_attempt(
const optional_t< string_view_t > opt_field_value )
{
if( !opt_field_value )
return make_unexpected( extraction_error_t::no_auth_http_field );
auto field_value_parse_result = authorization_value_t::try_parse(
*opt_field_value );
if( !field_value_parse_result )
return make_unexpected( extraction_error_t::illegal_http_field_value );
auto & parsed_value = *field_value_parse_result;
if( "bearer" != parsed_value.auth_scheme )
return make_unexpected( extraction_error_t::not_bearer_auth_scheme );
return try_extract_params( std::move(parsed_value) );
}
} /* namespace impl */
//
// try_extract_params
//
RESTINIO_NODISCARD
inline expected_t< params_t, extraction_error_t >
try_extract_params(
const http_header_fields_t & fields,
string_view_t auth_field_name )
{
return impl::perform_extraction_attempt(
fields.opt_value_of( auth_field_name ) );
}
template< typename Extra_Data >
RESTINIO_NODISCARD
inline expected_t< params_t, extraction_error_t >
try_extract_params(
const generic_request_t< Extra_Data > & req,
string_view_t auth_field_name )
{
return try_extract_params( req.header(), auth_field_name );
}
RESTINIO_NODISCARD
inline expected_t< params_t, extraction_error_t >
try_extract_params(
const http_header_fields_t & fields,
http_field_t auth_field_id )
{
return impl::perform_extraction_attempt(
fields.opt_value_of( auth_field_id ) );
}
template< typename Extra_Data >
RESTINIO_NODISCARD
inline expected_t< params_t, extraction_error_t >
try_extract_params(
const generic_request_t< Extra_Data > & req,
http_field_t auth_field_id )
{
return try_extract_params( req.header(), auth_field_id );
}
} /* namespace bearer_auth */
} /* namespace http_field_parsers */
} /* namespace restinio */
authorization.hpp
Stuff related to value of Authorization HTTP-field.
nonstd::optional_lite::optional
class optional
Definition: optional.hpp:919
restinio::generic_request_t
HTTP Request data.
Definition: request_handler.hpp:201
restinio::generic_request_t::header
const http_request_header_t & header() const noexcept
Get request header.
Definition: request_handler.hpp:256
restinio::http_header_fields_t
Header fields map.
Definition: http_headers.hpp:703
restinio::http_header_fields_t::opt_value_of
optional_t< string_view_t > opt_value_of(string_view_t name) const noexcept
Get optional value of a field.
Definition: http_headers.hpp:1300
RESTINIO_NODISCARD
#define RESTINIO_NODISCARD
Definition: compiler_features.hpp:33
expected.hpp
http_headers.hpp
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:465
restinio::http_field_parsers::bearer_auth::impl::perform_extraction_attempt
RESTINIO_NODISCARD expected_t< params_t, extraction_error_t > perform_extraction_attempt(const optional_t< string_view_t > opt_field_value)
Definition: bearer_auth.hpp:230
restinio::http_field_parsers::bearer_auth::to_string_view
RESTINIO_NODISCARD string_view_t to_string_view(extraction_error_t what) noexcept
Helper function to get a string name of extraction_error enum.
Definition: bearer_auth.hpp:81
restinio::http_field_parsers::bearer_auth::extraction_error_t
extraction_error_t
Error codes for failures of extraction of bearer authentification parameters.
Definition: bearer_auth.hpp:58
restinio::http_field_parsers::bearer_auth::extraction_error_t::no_auth_http_field
@ no_auth_http_field
There is no HTTP field with authentification parameters.
restinio::http_field_parsers::bearer_auth::extraction_error_t::not_bearer_auth_scheme
@ not_bearer_auth_scheme
Different authentification scheme found. bearer authentification scheme is expected.
restinio::http_field_parsers::bearer_auth::extraction_error_t::invalid_bearer_auth_param
@ invalid_bearer_auth_param
Invalid value of parameter for bearer authentification scheme. The single parameter in the form of b6...
restinio::http_field_parsers::bearer_auth::extraction_error_t::illegal_http_field_value
@ illegal_http_field_value
The HTTP field with authentification parameters can't be parsed.
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
Definition: asio_include.hpp:21
restinio::string_view_t
nonstd::string_view string_view_t
Definition: string_view.hpp:19
restinio::http_field_t
http_field_t
C++ enum that repeats nodejs c-style enum.
Definition: http_headers.hpp:233
restinio::expected_t
nonstd::expected< T, E > expected_t
Definition: expected.hpp:22
request_handler.hpp
restinio::http_field_parsers::authorization_value_t
Tools for working with the value of Authorization HTTP-field.
Definition: authorization.hpp:133
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
restinio::http_field_parsers::bearer_auth::params_t
Parameters for bearer authentification.
Definition: bearer_auth.hpp:40
restinio::http_field_parsers::bearer_auth::params_t::token
std::string token
Access Token.
Definition: bearer_auth.hpp:45