RESTinio

bearer_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/http_headers.hpp>
17#include <restinio/request_handler.hpp>
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//
57 enum class extraction_error_t
58{
60 no_auth_http_field,
61
63 illegal_http_field_value,
64
67 not_bearer_auth_scheme,
68
71 invalid_bearer_auth_param,
72};
73
79 RESTINIO_NODISCARD
80 inline string_view_t
81 to_string_view( extraction_error_t what ) noexcept
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
99 case extraction_error_t::invalid_bearer_auth_param:
100 result = string_view_t{ "invalid_bearer_auth_param" };
101 break;
102 }
103
104 return result;
105}
106
107//
108// try_extract_params
109//
150 RESTINIO_NODISCARD
151 inline expected_t< params_t, extraction_error_t >
152 try_extract_params(
153 const authorization_value_t & http_field )
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
212 RESTINIO_NODISCARD
213 inline expected_t< params_t, extraction_error_t >
214 try_extract_params(
215 authorization_value_t && http_field )
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
228 RESTINIO_NODISCARD
229 inline expected_t< params_t, extraction_error_t >
230 perform_extraction_attempt(
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//
273 RESTINIO_NODISCARD
274 inline expected_t< params_t, extraction_error_t >
275 try_extract_params(
277 const http_header_fields_t & fields,
279 string_view_t auth_field_name )
280{
281 return impl::perform_extraction_attempt(
282 fields.opt_value_of( auth_field_name ) );
283}
284
305 template< typename Extra_Data >
306 RESTINIO_NODISCARD
307 inline expected_t< params_t, extraction_error_t >
308 try_extract_params(
311 const generic_request_t< Extra_Data > & req,
313 string_view_t auth_field_name )
314{
315 return try_extract_params( req.header(), auth_field_name );
316}
317
338 RESTINIO_NODISCARD
339 inline expected_t< params_t, extraction_error_t >
340 try_extract_params(
342 const http_header_fields_t & fields,
344 http_field_t auth_field_id )
345{
346 return impl::perform_extraction_attempt(
347 fields.opt_value_of( auth_field_id ) );
348}
349
370 template< typename Extra_Data >
371 RESTINIO_NODISCARD
372 inline expected_t< params_t, extraction_error_t >
373 try_extract_params(
376 const generic_request_t< Extra_Data > & req,
378 http_field_t auth_field_id )
379{
380 return try_extract_params( req.header(), auth_field_id );
381}
382
383} /* namespace bearer_auth */
384
385} /* namespace http_field_parsers */
386
387} /* namespace restinio */
388
authorization.hpp
Stuff related to value of Authorization HTTP-field.
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
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:465
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::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
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::http_field_parsers::bearer_auth::extraction_error_t::no_auth_http_field
@ no_auth_http_field
There is no HTTP field with authentification parameters.
restinio::http_field_parsers::bearer_auth::extraction_error_t::not_bearer_auth_scheme
@ not_bearer_auth_scheme
Different authentification scheme found. bearer authentification scheme is expected.
restinio::http_field_parsers::bearer_auth::extraction_error_t::invalid_bearer_auth_param
@ invalid_bearer_auth_param
Invalid value of parameter for bearer authentification scheme. The single parameter in the form of b6...
restinio::http_field_parsers::bearer_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::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
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::bearer_auth::params_t
Parameters for bearer authentification.
Definition: bearer_auth.hpp:40
restinio::http_field_parsers::bearer_auth::params_t::token
std::string token
Access Token.
Definition: bearer_auth.hpp:45