basic_auth.hpp
Go to the documentation of this file.
/*
* RESTinio
*/
#pragma once
#include <restinio/helpers/http_field_parsers/authorization.hpp>
#include <restinio/utils/base64.hpp>
#include <restinio/http_headers.hpp>
#include <restinio/request_handler.hpp>
#include <restinio/expected.hpp>
#include <iostream>
namespace restinio
{
namespace http_field_parsers
{
namespace basic_auth
{
//
// params_t
//
struct params_t
{
std::string username;
std::string password;
};
//
// extraction_error_t
//
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,
};
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_basic_auth_scheme:
result = string_view_t{ "not_basic_auth_scheme" };
break;
case extraction_error_t::invalid_basic_auth_param:
result = string_view_t{ "invalid_basic_auth_param" };
break;
case extraction_error_t::token68_decode_error:
result = string_view_t{ "token68_decode_error" };
break;
case extraction_error_t::invalid_username_password_pair:
result = string_view_t{ "invalid_username_password_pair" };
break;
case extraction_error_t::empty_username:
result = string_view_t{ "empty_username" };
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 * token68 = get_if<authorization_value_t::token68_t>(
&http_field.auth_param );
if( !token68 )
return make_unexpected( extraction_error_t::invalid_basic_auth_param );
const auto unbase64_result =
restinio::utils::base64::try_decode( token68->value );
if( !unbase64_result )
return make_unexpected( extraction_error_t::token68_decode_error );
const std::string & username_password = *unbase64_result;
const auto first_colon = username_password.find( ':' );
if( std::string::npos == first_colon )
return make_unexpected(
extraction_error_t::invalid_username_password_pair );
if( 0u == first_colon )
return make_unexpected( extraction_error_t::empty_username );
return params_t{
username_password.substr( 0u, first_colon ),
username_password.substr( first_colon + 1u )
};
}
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 );
const 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 );
const auto & parsed_value = *field_value_parse_result;
if( "basic" != parsed_value.auth_scheme )
return make_unexpected( extraction_error_t::not_basic_auth_scheme );
return try_extract_params( 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 basic_auth */
} /* namespace http_field_parsers */
} /* namespace restinio */
authorization.hpp
Stuff related to value of Authorization HTTP-field.
base64.hpp
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
restinio::http_field_parsers::basic_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: basic_auth.hpp:215
restinio::http_field_parsers::basic_auth::extraction_error_t
extraction_error_t
Error codes for failures of extraction of basic authentification parameters.
Definition: basic_auth.hpp:66
restinio::http_field_parsers::basic_auth::extraction_error_t::not_basic_auth_scheme
@ not_basic_auth_scheme
Different authentification scheme found. Basic authentification scheme is expected.
restinio::http_field_parsers::basic_auth::extraction_error_t::token68_decode_error
@ token68_decode_error
Value of token68 parameter for basic authentification can't be decoded.
restinio::http_field_parsers::basic_auth::extraction_error_t::no_auth_http_field
@ no_auth_http_field
There is no HTTP field with authentification parameters.
restinio::http_field_parsers::basic_auth::extraction_error_t::empty_username
@ empty_username
Empty user name in username:password pair.
restinio::http_field_parsers::basic_auth::extraction_error_t::invalid_username_password_pair
@ invalid_username_password_pair
Wrong format for username:password in decoded token68 parameter. Maybe there is no colon symbol.
restinio::http_field_parsers::basic_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::basic_auth::extraction_error_t::invalid_basic_auth_param
@ invalid_basic_auth_param
Invalid value of parameter for basic authentification scheme. The single parameter in the form of tok...
restinio::http_field_parsers::basic_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: basic_auth.hpp:99
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::utils::base64::try_decode
expected_t< std::string, decoding_error_t > try_decode(string_view_t str)
Definition: base64.hpp:184
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::basic_auth::params_t
Parameters for basic authentification.
Definition: basic_auth.hpp:42
restinio::http_field_parsers::basic_auth::params_t::username
std::string username
Name of a user.
Definition: basic_auth.hpp:47
restinio::http_field_parsers::basic_auth::params_t::password
std::string password
Password for a user.
Definition: basic_auth.hpp:53