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
22namespace restinio
23{
24
25namespace http_field_parsers
26{
27
28namespace bearer_auth
29{
30
31//
32// params_t
33//
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
68
72};
73
80inline string_view_t
82{
83 string_view_t result{ "<unknown>" };
84
85 switch( what )
86 {
88 result = string_view_t{ "no_auth_http_field" };
89 break;
90
92 result = string_view_t{ "illegal_http_field_value" };
93 break;
94
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
225namespace 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
305template< typename Extra_Data >
313 string_view_t auth_field_name )
314{
315 return try_extract_params( req.header(), auth_field_name );
316}
317
342 const http_header_fields_t & fields,
344 http_field_t auth_field_id )
345{
347 fields.opt_value_of( auth_field_id ) );
348}
349
370template< typename Extra_Data >
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
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)
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
extraction_error_t
Error codes for failures of extraction of bearer authentification parameters.
Definition: bearer_auth.hpp:58
@ no_auth_http_field
There is no HTTP field with authentification parameters.
@ not_bearer_auth_scheme
Different authentification scheme found. bearer authentification scheme is expected.
@ invalid_bearer_auth_param
Invalid value of parameter for bearer authentification scheme. The single parameter in the form of b6...
@ illegal_http_field_value
The HTTP field with authentification parameters can't be parsed.
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.
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 bearer authentification.
Definition: bearer_auth.hpp:40