RESTinio
Loading...
Searching...
No Matches
authorization.hpp
Go to the documentation of this file.
1/*
2 * RESTinio
3 */
4
12#pragma once
13
15
16#include <iostream>
17#include <variant>
18
19namespace restinio
20{
21
22namespace http_field_parsers
23{
24
25namespace authorization_details
26{
27
29
30//
31// is_token68_char_predicate_t
32//
41{
43
44 [[nodiscard]]
45 bool
46 operator()( const char actual ) const noexcept
47 {
49 || '-' == actual
50 || '.' == actual
51 || '_' == actual
52 || '~' == actual
53 || '+' == actual
54 || '/' == actual
55 ;
56 }
57};
58
59//
60// token68_symbol_p
61//
62[[nodiscard]]
63inline auto
69
70//
71// token68_t
72//
82{
83 std::string value;
84};
85
86inline std::ostream &
87operator<<( std::ostream & to, const token68_t & v )
88{
89 return (to << v.value);
90}
91
92//
93// token68_p
94//
95[[nodiscard]]
96inline auto
98{
102 repeat( 0, N, symbol_p('=') >> to_container() )
103 ) >> &token68_t::value
104 );
105}
106
107} /* authorization_details */
108
109//
110// authorization_value_t
111//
132{
134 enum class value_form_t
135 {
137 token,
140 };
141
144 {
146 std::string value;
149 };
150
152 struct param_t
153 {
155 std::string name;
158 };
159
161 using param_container_t = std::vector< param_t >;
162
165
167 using auth_param_t = std::variant< token68_t, param_container_t >;
168
170 std::string auth_scheme;
172
177
183 [[nodiscard]]
184 static auto
186 {
187 using namespace authorization_details;
188
189 auto token_to_v = []( std::string v ) -> param_value_t {
190 return { std::move(v), value_form_t::token };
191 };
192 auto qstring_to_v = []( std::string v ) -> param_value_t {
193 return { std::move(v), value_form_t::quoted_string };
194 };
195
196 // NOTE: token68 should consume all input.
197 // So there should not be any symbols after the value.
198 auto token68_seq = sequence(
199 token68_p() >> as_result(),
200 not_clause( any_symbol_p() >> skip() ) );
201 // Parameters list can be empty.
204 token_p() >> to_lower() >> &param_t::name,
205 ows(),
206 symbol('='),
207 ows(),
210 token_p() >> convert( token_to_v ) >> as_result(),
212 >> as_result()
213 )
214 ) >> &param_t::value
215 )
216 ) >> as_result();
217
220 maybe(
221 repeat( 1, N, space() ),
225 )
226 );
227 }
228
234 [[nodiscard]]
235 static expected_t<
242};
243
244//
245// Various helpers for dumping values to std::ostream.
246//
247inline std::ostream &
249 std::ostream & to,
251{
253 to << v.value;
254 else
255 to << '"' << v.value << '"';
256 return to;
257}
258
259inline std::ostream &
261 std::ostream & to,
263{
264 return (to << v.name << '=' << v.value);
265}
266
267inline std::ostream &
269 std::ostream & to,
271{
272 struct printer_t
273 {
274 std::ostream & to;
275
276 void
277 operator()( const authorization_value_t::token68_t & t ) const
278 {
279 to << t;
280 }
281
282 void
283 operator()( const authorization_value_t::param_container_t & c ) const
284 {
285 bool first = true;
286 to << '{';
287 for( const auto & param : c )
288 {
289 if( !first )
290 to << ", ";
291 else
292 first = false;
293
294 to << param;
295 }
296 to << '}';
297 }
298 };
299
300 std::visit( printer_t{ to }, p );
301
302 return to;
303}
304
305inline std::ostream &
307 std::ostream & to,
308 const authorization_value_t & v )
309{
310 return (to << v.auth_scheme << ' ' << v.auth_param);
311}
312
313} /* namespace http_field_parsers */
314
315} /* namespace restinio */
316
Utilities for parsing values of http-fields.
A template for producer of charachers that satisfy some predicate.
Information about parsing error.
auto space() noexcept
A factory function to create a clause that expects a space, extracts it and then skips it.
auto to_container()
A factory function to create a to_container_consumer.
auto any_symbol_p() noexcept
A factory function to create an any_symbol_producer.
auto symbol(char expected) noexcept
A factory function to create a clause that expects the speficied symbol, extracts it and then skips i...
expected_t< typename Producer::result_type, parse_error_t > try_parse(string_view_t from, Producer producer)
Perform the parsing of the specified content by using specified value producer.
auto as_result() noexcept
A factory function to create a as_result_consumer.
auto maybe(Clauses &&... clauses)
A factory function to create an optional clause.
auto skip() noexcept
A factory function to create a skip_consumer.
auto symbol_p(char expected) noexcept
A factory function to create a symbol_producer.
auto to_lower() noexcept
A factory function to create a to_lower_transformer.
auto alternatives(Clauses &&... clauses)
A factory function to create an alternatives clause.
auto convert(Converter &&converter)
A factory function to create convert_transformer.
auto not_clause(Clauses &&... clauses)
A factory function to create a not_clause.
constexpr std::size_t N
A special marker that means infinite repetitions.
auto repeat(std::size_t min_occurences, std::size_t max_occurences, Clauses &&... clauses)
A factory function to create repetitor of subclauses.
auto sequence(Clauses &&... clauses)
A factory function to create a sequence of subclauses.
std::ostream & operator<<(std::ostream &to, const token68_t &v)
std::ostream & operator<<(std::ostream &to, const authorization_value_t::param_value_t &v)
auto token_p() noexcept
A factory function to create a token_producer.
Definition basics.hpp:987
auto ows() noexcept
A factory function to create an OWS clause.
Definition basics.hpp:941
auto quoted_string_p() noexcept
A factory function to create a quoted_string_producer.
Definition basics.hpp:1014
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.
std::string_view string_view_t
nonstd::expected< T, E > expected_t
Definition expected.hpp:18
A preducate for symbol_producer_template that checks that a symbol can be used inside token68 from RF...
A structure for holding a value of token68 from RFC7235.
A storage for a parameter with a name and a value.
value_form_t form
How this value was represented: as a token, or a quoted string?
Tools for working with the value of Authorization HTTP-field.
auth_param_t auth_param
A parameter for authorization.
std::string auth_scheme
A value of auth-scheme.
static auto make_parser()
A factory function for a parser of Authorization value.
static expected_t< authorization_value_t, restinio::easy_parser::parse_error_t > try_parse(string_view_t what)
An attempt to parse Authorization HTTP-field.
std::variant< token68_t, param_container_t > auth_param_t
Type for holding a parameter for authorization.
value_form_t
An indicator of the source form of the value of a parameter.
@ token
The value of a parameter was specified as token.
@ quoted_string
The value of a parameter was specified as quoted_string.
std::vector< param_t > param_container_t
Type of container for holding parameters.
A preducate for symbol_producer_template that checks that a symbol is an alpha or numeric.
Definition basics.hpp:300
bool operator()(const char actual) const noexcept
Definition basics.hpp:303