This version fixes a mistake introduced in v.0.6.7.1 and brings some new helpers:
Implementation of extraction of Bearer authentification parameters fixed. Now it looks like:
#include <restinio/all.hpp>
#include <restinio/http_field_parser/bearer_auth.hpp>
...
auto on_request(const restinio::request_handle_t & req) {
using namespace restinio::http_field_parsers::bearer_auth;
const auto auth_params = try_extract_params(*req,
restinio::http_field::authorization);
if(auth_params) { // Parameters successfully extracted.
if(is_valid_user(auth_params->token)) {
...
}
}
...
}
-----
New helper function try_parse_field for simplification of HTTP-fields parsing added:
#include <restinio/all.hpp>
#include <restinio/helpers/http_fields_parsers/try_parse_field.hpp>
#include <restinio/helpers/http_fields_parsers/accept.hpp>
...
auto on_request(const restinio::request_handle_t & req) {
using namespace restinio::http_field_parsers;
// Try to get and parse the value of `Accept` header.
const auto parse_result = try_parse_field<accept_value_t>(
req, restinio::http_field::accept);
if(const auto * value =
restinio::get_if<accept_value_t>(&parse_result)) {
// Value of the field is successfully parsed.
... // Some usage of parsed value.
}
}
-----
Several new overloads for try_extract_params functions from restinio::http_field_parsers::basic_auth and restinio::http_field_parsers::bearer_auth namespaces. They allow to work with several authentication schemes after the parsing of Authorization (Proxy-Authorization) field:
auto on_request(const restinio::request_handle_t & req) {
using namespace restinio::http_field_parsers;
const auto field = try_parse_field<authorization_value_t>(
req, restinio::http_field::authorization);
if(const auto * auth = restinio::get_if<authorization_value_t>(field)) {
// We have valid Authorization field value.
if("basic" == auth->auth_scheme) {
// Basic authentification scheme should be used.
using namespace restinio::http_field_parsers::basic_auth;
const auto params = try_extract_params(auth->auth_params);
if(params) { // Parameters successfully extracted.
if(is_valid_user(params->username, params->password)) {
...
}
}
...
}
else if("bearer" == auth->auth_scheme) {
// Bearer authentification scheme should be used.
using namespace restinio::http_field_parsers::bearer_auth;
const auto params = try_extract_params(auth->auth_params);
if(auth_params) { // Parameters successfully extracted.
if(is_valid_user(auth_params->token)) {
...
}
}
...
}
else {
... // Handling of different schemes.
}
}
}