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 
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 //
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 
90 };
91 
98 inline string_view_t
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 
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(
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 
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 
297  const request_t & req,
299  string_view_t auth_field_name )
300 {
301  return try_extract_params( req.header(), auth_field_name );
302 }
303 
329  const http_header_fields_t & fields,
331  http_field_t auth_field_id )
332 {
334  fields.opt_value_of( auth_field_id ) );
335 }
336 
363  const request_t & req,
365  http_field_t auth_field_id )
366 {
367  return try_extract_params( req.header(), auth_field_id );
368 }
369 
370 } /* namespace basic_auth */
371 
372 } /* namespace http_field_parsers */
373 
374 } /* namespace restinio */
375 
RESTINIO_NODISCARD
#define RESTINIO_NODISCARD
Definition: compiler_features.hpp:33
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::utils::base64::try_decode
expected_t< std::string, decoding_error_t > try_decode(string_view_t str)
Definition: base64.hpp:184
restinio::http_field_parsers::basic_auth::params_t::password
std::string password
Password for a user.
Definition: basic_auth.hpp:53
request_handler.hpp
restinio::string_view_t
nonstd::string_view string_view_t
Definition: string_view.hpp:19
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_header_fields_t
Header fields map.
Definition: http_headers.hpp:703
restinio::http_field_t
http_field_t
C++ enum that repeats nodejs c-style enum.
Definition: http_headers.hpp:233
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
nonstd::optional_lite::optional
class optional
Definition: optional.hpp:839
restinio::expected_t
nonstd::expected< T, E > expected_t
Definition: expected.hpp:22
restinio::http_field_parsers::authorization_value_t
Tools for working with the value of Authorization HTTP-field.
Definition: authorization.hpp:133
restinio::request_t::header
const http_request_header_t & header() const noexcept
Get request header.
Definition: request_handler.hpp:92
restinio::http_field_parsers::basic_auth::params_t
Parameters for basic authentification.
Definition: basic_auth.hpp:42
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_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
Definition: asio_include.hpp:21
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
base64.hpp
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::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
authorization.hpp
Stuff related to value of Authorization HTTP-field.
expected.hpp
http_headers.hpp
restinio::request_t
HTTP Request data.
Definition: request_handler.hpp:44