RESTinio
bearer_auth.hpp
Go to the documentation of this file.
1 /*
2  * RESTinio
3  */
4 
12 #pragma once
13 
15 
18 #include <restinio/expected.hpp>
19 
20 #include <iostream>
21 
22 namespace restinio
23 {
24 
25 namespace http_field_parsers
26 {
27 
28 namespace bearer_auth
29 {
30 
31 //
32 // params_t
33 //
39 struct params_t
40 {
42 
45  std::string token;
46 };
47 
48 //
49 // extraction_error_t
50 //
58 {
60  no_auth_http_field,
61 
63  illegal_http_field_value,
64 
67  not_bearer_auth_scheme,
68 
72 };
73 
80 inline string_view_t
82 {
83  string_view_t result{ "<unknown>" };
84 
85  switch( what )
86  {
87  case extraction_error_t::no_auth_http_field:
88  result = string_view_t{ "no_auth_http_field" };
89  break;
90 
91  case extraction_error_t::illegal_http_field_value:
92  result = string_view_t{ "illegal_http_field_value" };
93  break;
94 
95  case extraction_error_t::not_bearer_auth_scheme:
96  result = string_view_t{ "not_bearer_auth_scheme" };
97  break;
98 
100  result = string_view_t{ "invalid_bearer_auth_param" };
101  break;
102  }
103 
104  return result;
105 }
106 
107 //
108 // try_extract_params
109 //
154 {
155  const auto * b64token = get_if<authorization_value_t::token68_t>(
156  &http_field.auth_param );
157  if( !b64token )
158  return make_unexpected( extraction_error_t::invalid_bearer_auth_param );
159 
160  return params_t{ b64token->value };
161 }
162 
216 {
217  auto * b64token = get_if<authorization_value_t::token68_t>(
218  &http_field.auth_param );
219  if( !b64token )
220  return make_unexpected( extraction_error_t::invalid_bearer_auth_param );
221 
222  return params_t{ std::move(b64token->value) };
223 }
224 
225 namespace impl
226 {
227 
231  const optional_t< string_view_t > opt_field_value )
232 {
233  if( !opt_field_value )
234  return make_unexpected( extraction_error_t::no_auth_http_field );
235 
236  auto field_value_parse_result = authorization_value_t::try_parse(
237  *opt_field_value );
238  if( !field_value_parse_result )
239  return make_unexpected( extraction_error_t::illegal_http_field_value );
240 
241  auto & parsed_value = *field_value_parse_result;
242  if( "bearer" != parsed_value.auth_scheme )
243  return make_unexpected( extraction_error_t::not_bearer_auth_scheme );
244 
245  return try_extract_params( std::move(parsed_value) );
246 }
247 
248 } /* namespace impl */
249 
250 //
251 // try_extract_params
252 //
277  const http_header_fields_t & fields,
279  string_view_t auth_field_name )
280 {
282  fields.opt_value_of( auth_field_name ) );
283 }
284 
310  const request_t & req,
312  string_view_t auth_field_name )
313 {
314  return try_extract_params( req.header(), auth_field_name );
315 }
316 
341  const http_header_fields_t & fields,
343  http_field_t auth_field_id )
344 {
346  fields.opt_value_of( auth_field_id ) );
347 }
348 
374  const request_t & req,
376  http_field_t auth_field_id )
377 {
378  return try_extract_params( req.header(), auth_field_id );
379 }
380 
381 } /* namespace bearer_auth */
382 
383 } /* namespace http_field_parsers */
384 
385 } /* namespace restinio */
386 
RESTINIO_NODISCARD
#define RESTINIO_NODISCARD
Definition: compiler_features.hpp:33
restinio::http_field_parsers::bearer_auth::extraction_error_t::no_auth_http_field
@ no_auth_http_field
There is no HTTP field with authentification parameters.
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:421
request_handler.hpp
restinio::http_field_parsers::bearer_auth::params_t::token
std::string token
Access Token.
Definition: bearer_auth.hpp:45
restinio::http_field_parsers::bearer_auth::params_t
Parameters for bearer authentification.
Definition: bearer_auth.hpp:40
restinio::http_field_parsers::bearer_auth::extraction_error_t
extraction_error_t
Error codes for failures of extraction of bearer authentification parameters.
Definition: bearer_auth.hpp:58
restinio::string_view_t
nonstd::string_view string_view_t
Definition: string_view.hpp:19
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
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::bearer_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 bearer authentification from an already parsed HTTP-field.
Definition: bearer_auth.hpp:152
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::bearer_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: bearer_auth.hpp:230
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.
restinio::http_field_parsers::bearer_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: bearer_auth.hpp:81
expected.hpp
http_headers.hpp
restinio::request_t
HTTP Request data.
Definition: request_handler.hpp:44