14 #include <restinio/helpers/http_field_parsers/authorization.hpp> 16 #include <restinio/utils/base64.hpp> 18 #include <restinio/http_headers.hpp> 19 #include <restinio/request_handler.hpp> 20 #include <restinio/expected.hpp> 101 string_view_t result{
"<unknown>" };
105 case extraction_error_t::no_auth_http_field:
106 result = string_view_t{
"no_auth_http_field" };
109 case extraction_error_t::illegal_http_field_value:
110 result = string_view_t{
"illegal_http_field_value" };
113 case extraction_error_t::not_basic_auth_scheme:
114 result = string_view_t{
"not_basic_auth_scheme" };
117 case extraction_error_t::invalid_basic_auth_param:
118 result = string_view_t{
"invalid_basic_auth_param" };
121 case extraction_error_t::token68_decode_error:
122 result = string_view_t{
"token68_decode_error" };
125 case extraction_error_t::invalid_username_password_pair:
126 result = string_view_t{
"invalid_username_password_pair" };
129 case extraction_error_t::empty_username:
130 result = string_view_t{
"empty_username" };
186 const auto * token68 = std::get_if<authorization_value_t::token68_t>(
187 &http_field.auth_param );
189 return make_unexpected( extraction_error_t::invalid_basic_auth_param );
191 const auto unbase64_result =
192 restinio::utils::base64::try_decode( token68->value );
193 if( !unbase64_result )
194 return make_unexpected( extraction_error_t::token68_decode_error );
196 const std::string & username_password = *unbase64_result;
197 const auto first_colon = username_password.find(
':' );
198 if( std::string::npos == first_colon )
199 return make_unexpected(
200 extraction_error_t::invalid_username_password_pair );
201 if( 0u == first_colon )
202 return make_unexpected( extraction_error_t::empty_username );
205 username_password.substr( 0u, first_colon ),
206 username_password.substr( first_colon + 1u )
218 if( !opt_field_value )
219 return make_unexpected( extraction_error_t::no_auth_http_field );
221 const auto field_value_parse_result = authorization_value_t::try_parse(
223 if( !field_value_parse_result )
224 return make_unexpected( extraction_error_t::illegal_http_field_value );
226 const auto & parsed_value = *field_value_parse_result;
227 if(
"basic" != parsed_value.auth_scheme )
228 return make_unexpected( extraction_error_t::not_basic_auth_scheme );
230 return try_extract_params( parsed_value );
267 return impl::perform_extraction_attempt(
268 fields.opt_value_of( auth_field_name ) );
292 template<
typename Extra_Data >
334 return impl::perform_extraction_attempt(
335 fields.opt_value_of( auth_field_id ) );
359 template<
typename Extra_Data >
There is no HTTP field with authentification parameters.
expected_t< std::optional< string_view_t >, not_found_t > find_first(const parameter_with_optional_value_container_t &where, string_view_t what)
A helper function to find the first occurence of a parameter with the specified value.
Invalid value of parameter for basic authentification scheme. The single parameter in the form of tok...
Wrong format for username:password in decoded token68 parameter. Maybe there is no colon symbol...
Value of token68 parameter for basic authentification can't be decoded.
std::string password
Password for a user.
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, std::optional< Value_Type > >::type opt_value(const Parameter_Container ¶ms, string_view_t key)
Gets the value of a parameter specified by key wrapped in std::optional<Value_Type> if parameter exis...
Empty user name in username:password pair.
extraction_error_t
Error codes for failures of extraction of basic authentification parameters.
The HTTP field with authentification parameters can't be parsed.
std::string username
Name of a user.
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...
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.
expected_t< params_t, extraction_error_t > perform_extraction_attempt(const std::optional< string_view_t > opt_field_value)
string_view_t to_string_view(extraction_error_t what) noexcept
Helper function to get a string name of extraction_error enum.
Different authentification scheme found. Basic authentification scheme is expected.
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.
Parameters for basic authentification.