RESTinio
basic_auth.hpp
Go to the documentation of this file.
1/*
2 * RESTinio
3 */
4
12#pragma once
13
15
17
20#include <restinio/expected.hpp>
21
22#include <iostream>
23
24namespace restinio
25{
26
27namespace http_field_parsers
28{
29
30namespace basic_auth
31{
32
33//
34// params_t
35//
42{
44
47 std::string username;
48
50
53 std::string password;
54};
55
56//
57// extraction_error_t
58//
66{
69
72
76
80
83
87
90};
91
98inline string_view_t
100{
101 string_view_t result{ "<unknown>" };
102
103 switch( what )
104 {
106 result = string_view_t{ "no_auth_http_field" };
107 break;
108
110 result = string_view_t{ "illegal_http_field_value" };
111 break;
112
114 result = string_view_t{ "not_basic_auth_scheme" };
115 break;
116
118 result = string_view_t{ "invalid_basic_auth_param" };
119 break;
120
122 result = string_view_t{ "token68_decode_error" };
123 break;
124
126 result = string_view_t{ "invalid_username_password_pair" };
127 break;
128
130 result = string_view_t{ "empty_username" };
131 break;
132 }
133
134 return result;
135}
136
137//
138// try_extract_params
139//
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(
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
210namespace impl
211{
212
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//
263 const http_header_fields_t & fields,
265 string_view_t auth_field_name )
266{
268 fields.opt_value_of( auth_field_name ) );
269}
270
292template< typename Extra_Data >
300 string_view_t auth_field_name )
301{
302 return try_extract_params( req.header(), auth_field_name );
303}
304
330 const http_header_fields_t & fields,
332 http_field_t auth_field_id )
333{
335 fields.opt_value_of( auth_field_id ) );
336}
337
359template< typename Extra_Data >
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
Stuff related to value of Authorization HTTP-field.
const http_request_header_t & header() const noexcept
Get request header.
optional_t< string_view_t > opt_value_of(string_view_t name) const noexcept
Get optional value of a field.
#define RESTINIO_NODISCARD
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
extraction_error_t
Error codes for failures of extraction of basic authentification parameters.
Definition: basic_auth.hpp:66
@ 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...
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_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
expected_t< std::string, decoding_error_t > try_decode(string_view_t str)
Definition: base64.hpp:184
nonstd::string_view string_view_t
Definition: string_view.hpp:19
http_field_t
C++ enum that repeats nodejs c-style enum.
nonstd::expected< T, E > expected_t
Definition: expected.hpp:22
Tools for working with the value of Authorization HTTP-field.
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.
Parameters for basic authentification.
Definition: basic_auth.hpp:42
std::string password
Password for a user.
Definition: basic_auth.hpp:53