2
3
6
7
8
9
10
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>
37
38
39
40
60
61
62
63
64
93
94
95
96
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" };
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
187 &http_field.auth_param );
191 const auto unbase64_result =
192 restinio::utils::base64::try_decode( token68->value );
193 if( !unbase64_result )
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(
201 if( 0u == first_colon )
205 username_password.substr( 0u, first_colon ),
206 username_password.substr( first_colon + 1u )
216 const std::optional< string_view_t > opt_field_value )
218 if( !opt_field_value )
221 const auto field_value_parse_result = authorization_value_t::try_parse(
223 if( !field_value_parse_result )
226 const auto & parsed_value = *field_value_parse_result;
227 if(
"basic" != parsed_value.auth_scheme )
230 return try_extract_params( parsed_value );
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
265 string_view_t auth_field_name )
267 return impl::perform_extraction_attempt(
268 fields.opt_value_of( auth_field_name ) );
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292template<
typename Extra_Data >
300 string_view_t auth_field_name )
302 return try_extract_params( req.header(), auth_field_name );
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
334 return impl::perform_extraction_attempt(
335 fields.opt_value_of( auth_field_id ) );
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359template<
typename Extra_Data >
369 return try_extract_params( req.header(), auth_field_id );
expected_t< params_t, extraction_error_t > perform_extraction_attempt(const std::optional< string_view_t > opt_field_value)
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.
extraction_error_t
Error codes for failures of extraction of basic authentification parameters.
@ not_basic_auth_scheme
Different authentification scheme found. Basic authentification scheme is expected.
@ token68_decode_error
Value of token68 parameter for basic authentification can't be decoded.
@ no_auth_http_field
There is no HTTP field with authentification parameters.
@ empty_username
Empty user name in username:password pair.
@ invalid_username_password_pair
Wrong format for username:password in decoded token68 parameter. Maybe there is no colon symbol.
@ illegal_http_field_value
The HTTP field with authentification parameters can't be parsed.
@ invalid_basic_auth_param
Invalid value of parameter for basic authentification scheme. The single parameter in the form of tok...
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, string_view_t auth_field_name)
Helper function for getting parameters of basic authentification from a request.
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 basic authentification from a set of HTTP-fields.
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.
string_view_t to_string_view(extraction_error_t what) noexcept
Helper function to get a string name of extraction_error enum.
http_field_t
C++ enum that repeats nodejs c-style enum.
Tools for working with the value of Authorization HTTP-field.
authorization_details::token68_t token68_t
Type for holding a value of token68 from RFC7235.
Parameters for basic authentification.
std::string username
Name of a user.
std::string password
Password for a user.