RESTinio
Loading...
Searching...
No Matches
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//
73
80inline string_view_t
81to_string_view( extraction_error_t what ) noexcept
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{
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{
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
232{
233 if( !opt_field_value )
234 return make_unexpected( extraction_error_t::no_auth_http_field );
235
239 return make_unexpected( extraction_error_t::illegal_http_field_value );
240
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//
284
305template< typename Extra_Data >
317
349
370template< typename Extra_Data >
382
383} /* namespace bearer_auth */
384
385} /* namespace http_field_parsers */
386
387} /* namespace restinio */
388
Stuff related to value of Authorization HTTP-field.
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)
extraction_error_t
Error codes for failures of extraction of bearer authentification parameters.
@ 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.
run_on_this_thread_settings_t< Traits > on_this_thread()
A special marker for the case when http_server must be run on the context of the current thread.
nonstd::string_view string_view_t
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.