RESTinio

basic_auth.hpp
Go to the documentation of this file.
1/*
2 * RESTinio
3 */
4
12#pragma once
13
14#include <restinio/helpers/http_field_parsers/authorization.hpp>
15
16#include <restinio/utils/base64.hpp>
17
18#include <restinio/http_headers.hpp>
19#include <restinio/request_handler.hpp>
20#include <restinio/expected.hpp>
21
22#include <iostream>
23
24 namespace restinio
25{
26
27 namespace http_field_parsers
28{
29
30 namespace basic_auth
31{
32
33//
34// params_t
35//
41 struct params_t
42{
44
47 std::string username;
48
50
53 std::string password;
54};
55
56//
57// extraction_error_t
58//
65 enum class extraction_error_t
66{
68 no_auth_http_field,
69
71 illegal_http_field_value,
72
75 not_basic_auth_scheme,
76
79 invalid_basic_auth_param,
80
82 token68_decode_error,
83
86 invalid_username_password_pair,
87
89 empty_username,
90};
91
97 RESTINIO_NODISCARD
98 inline string_view_t
99 to_string_view( extraction_error_t what ) noexcept
100{
101 string_view_t result{ "<unknown>" };
102
103 switch( what )
104 {
105 case extraction_error_t::no_auth_http_field:
106 result = string_view_t{ "no_auth_http_field" };
107 break;
108
109 case extraction_error_t::illegal_http_field_value:
110 result = string_view_t{ "illegal_http_field_value" };
111 break;
112
113 case extraction_error_t::not_basic_auth_scheme:
114 result = string_view_t{ "not_basic_auth_scheme" };
115 break;
116
117 case extraction_error_t::invalid_basic_auth_param:
118 result = string_view_t{ "invalid_basic_auth_param" };
119 break;
120
121 case extraction_error_t::token68_decode_error:
122 result = string_view_t{ "token68_decode_error" };
123 break;
124
125 case extraction_error_t::invalid_username_password_pair:
126 result = string_view_t{ "invalid_username_password_pair" };
127 break;
128
129 case extraction_error_t::empty_username:
130 result = string_view_t{ "empty_username" };
131 break;
132 }
133
134 return result;
135}
136
137//
138// try_extract_params
139//
181 RESTINIO_NODISCARD
182 inline expected_t< params_t, extraction_error_t >
183 try_extract_params(
184 const authorization_value_t & http_field )
185{
186 const auto * token68 = get_if<authorization_value_t::token68_t>(
187 &http_field.auth_param );
188 if( !token68 )
189 return make_unexpected( extraction_error_t::invalid_basic_auth_param );
190
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 );
195
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 );
203
204 return params_t{
205 username_password.substr( 0u, first_colon ),
206 username_password.substr( first_colon + 1u )
207 };
208}
209
210 namespace impl
211{
212
213 RESTINIO_NODISCARD
214 inline expected_t< params_t, extraction_error_t >
215 perform_extraction_attempt(
216 const optional_t< string_view_t > opt_field_value )
217{
218 if( !opt_field_value )
219 return make_unexpected( extraction_error_t::no_auth_http_field );
220
221 const auto field_value_parse_result = authorization_value_t::try_parse(
222 *opt_field_value );
223 if( !field_value_parse_result )
224 return make_unexpected( extraction_error_t::illegal_http_field_value );
225
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 );
229
230 return try_extract_params( parsed_value );
231}
232
233} /* namespace impl */
234
235//
236// try_extract_params
237//
259 RESTINIO_NODISCARD
260 inline expected_t< params_t, extraction_error_t >
261 try_extract_params(
263 const http_header_fields_t & fields,
265 string_view_t auth_field_name )
266{
267 return impl::perform_extraction_attempt(
268 fields.opt_value_of( auth_field_name ) );
269}
270
292 template< typename Extra_Data >
293 RESTINIO_NODISCARD
294 inline expected_t< params_t, extraction_error_t >
295 try_extract_params(
298 const generic_request_t< Extra_Data > & req,
300 string_view_t auth_field_name )
301{
302 return try_extract_params( req.header(), auth_field_name );
303}
304
326 RESTINIO_NODISCARD
327 inline expected_t< params_t, extraction_error_t >
328 try_extract_params(
330 const http_header_fields_t & fields,
332 http_field_t auth_field_id )
333{
334 return impl::perform_extraction_attempt(
335 fields.opt_value_of( auth_field_id ) );
336}
337
359 template< typename Extra_Data >
360 RESTINIO_NODISCARD
361 inline expected_t< params_t, extraction_error_t >
362 try_extract_params(
365 const generic_request_t< Extra_Data > & req,
367 http_field_t auth_field_id )
368{
369 return try_extract_params( req.header(), auth_field_id );
370}
371
372} /* namespace basic_auth */
373
374} /* namespace http_field_parsers */
375
376} /* namespace restinio */
377
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