Repository navigation
Expand file tree
/
Copy pathconsumer_v3_auth.cpp
More file actions
218 lines (198 loc) · 9.44 KB
/
Copy pathconsumer_v3_auth.cpp
File metadata and controls
218 lines (198 loc) · 9.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
This file is part of libhttpserver
Copyright (C) 2011-2026 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see the file LICENSE in
the distribution; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Public headers compile without internal macros; compiled behavior uses library linkage.
#include <chrono>
#include <cstdint>
#include <string>
#include <type_traits>
#include <utility>
#include <httpserver/auth/basic_auth.hpp>
#include <httpserver/auth/digest_auth.hpp>
#include <httpserver/concurrency/executor.hpp>
#include <httpserver/http/fields.hpp>
#include <httpserver/http/outcome.hpp>
#include <httpserver/http/request_head.hpp>
#include <httpserver/http/status.hpp>
#include <httpserver/server/routes.hpp>
namespace {
using httpserver::auth::basic_auth_policy;
using httpserver::auth::basic_auth_result;
using httpserver::auth::basic_auth_validator;
using httpserver::auth::basic_auth_verdict;
using httpserver::auth::make_basic_guard;
namespace http = httpserver::http;
namespace srv = httpserver::server;
// The classification taxonomy: four distinct values packed in one
// byte.
static_assert(basic_auth_result::authenticated
!= basic_auth_result::no_credentials);
static_assert(basic_auth_result::no_credentials
!= basic_auth_result::malformed_credentials);
static_assert(basic_auth_result::malformed_credentials
!= basic_auth_result::credentials_rejected);
static_assert(std::is_same_v<std::underlying_type_t<basic_auth_result>,
std::uint8_t>);
// The verdict is a plain value type carrying the classification and
// the decoded pair; its helpers are const-correct.
static_assert(std::is_default_constructible_v<basic_auth_verdict>);
static_assert(std::is_move_constructible_v<basic_auth_verdict>);
static_assert(std::is_copy_constructible_v<basic_auth_verdict>);
static_assert(std::is_same_v<
decltype(std::declval<const basic_auth_verdict&>().allowed()),
bool>);
static_assert(std::is_same_v<
decltype(std::declval<const basic_auth_verdict&>()
.challenge_status()),
http::status>);
static_assert(std::is_same_v<
decltype(std::declval<const basic_auth_verdict&>()
.challenge_fields()),
http::fields>);
static_assert(std::is_same_v<
decltype(std::declval<const basic_auth_verdict&>().result),
basic_auth_result>);
// The policy: move-only (its validator is a unique_function), with
// the two factory forms overloads and a const check(). Disambiguated
// through explicit function-pointer aliases (the &X::create
// overloads are otherwise ambiguous in decltype).
static_assert(!std::is_copy_constructible_v<basic_auth_policy>);
static_assert(std::is_move_constructible_v<basic_auth_policy>);
static_assert(std::is_nothrow_move_assignable_v<basic_auth_policy>);
using fixed_create = http::outcome (*)(std::string, std::string,
std::string, basic_auth_policy&);
using validator_create = http::outcome (*)(std::string,
basic_auth_validator,
basic_auth_policy&);
static_assert(std::is_same_v<decltype(static_cast<fixed_create>(
&basic_auth_policy::create)),
fixed_create>);
static_assert(std::is_same_v<decltype(static_cast<validator_create>(
&basic_auth_policy::create)),
validator_create>);
static_assert(std::is_same_v<
decltype(std::declval<const basic_auth_policy&>().check(
std::declval<const http::request_head&>())),
basic_auth_verdict>);
static_assert(std::is_same_v<
decltype(std::declval<const basic_auth_policy&>().realm()),
const std::string&>);
static_assert(noexcept(std::declval<const basic_auth_policy&>().realm()));
// The guard adapter wraps a route handler into a route handler.
static_assert(std::is_same_v<decltype(make_basic_guard),
srv::route_handler(basic_auth_policy,
srv::route_handler)>);
// The Digest taxonomy: seven distinct values packed in one byte.
using httpserver::auth::digest_algorithm;
using httpserver::auth::digest_auth_options;
using httpserver::auth::digest_auth_policy;
using httpserver::auth::digest_auth_result;
using httpserver::auth::digest_auth_verdict;
using httpserver::auth::digest_ha1_source;
using httpserver::auth::make_digest_guard;
static_assert(digest_auth_result::authenticated
!= digest_auth_result::no_credentials);
static_assert(digest_auth_result::no_credentials
!= digest_auth_result::malformed_credentials);
static_assert(digest_auth_result::malformed_credentials
!= digest_auth_result::credentials_rejected);
static_assert(digest_auth_result::credentials_rejected
!= digest_auth_result::replayed_nonce);
static_assert(digest_auth_result::replayed_nonce
!= digest_auth_result::stale_nonce);
static_assert(digest_auth_result::stale_nonce
!= digest_auth_result::nonce_unavailable);
static_assert(std::is_same_v<std::underlying_type_t<digest_auth_result>,
std::uint8_t>);
// The verdict is a plain value type whose helpers are const-correct.
static_assert(std::is_default_constructible_v<digest_auth_verdict>);
static_assert(std::is_move_constructible_v<digest_auth_verdict>);
static_assert(std::is_copy_constructible_v<digest_auth_verdict>);
static_assert(std::is_same_v<
decltype(std::declval<const digest_auth_verdict&>().allowed()),
bool>);
static_assert(std::is_same_v<
decltype(std::declval<const digest_auth_verdict&>().stale()),
bool>);
static_assert(std::is_same_v<
decltype(std::declval<const digest_auth_verdict&>()
.challenge_status()),
http::status>);
static_assert(std::is_same_v<
decltype(std::declval<const digest_auth_verdict&>()
.challenge_fields()),
http::fields>);
// The options default to the v2 posture: MD5, a 300-second nonce TTL,
// an unbounded 1024-slot ledger.
static_assert(std::is_same_v<
decltype(digest_auth_options{}.algorithm),
digest_algorithm>);
static_assert(std::is_same_v<
decltype(digest_auth_options{}.nonce_ttl),
std::chrono::seconds>);
static_assert(digest_auth_options{}.nonce_ttl
== std::chrono::seconds{300});
static_assert(digest_auth_options{}.max_nc == 0);
static_assert(digest_auth_options{}.ledger_capacity == 1024);
static_assert(digest_algorithm::md5 != digest_algorithm::sha_256);
// The HA1 source is the unique_function callback shape; the v2
// check_digest_auth_digest analogue.
static_assert(!std::is_copy_constructible_v<digest_ha1_source>);
static_assert(std::is_move_constructible_v<digest_ha1_source>);
// The policy: move-only (its HA1 source is a unique_function), with
// the two factory-form overloads and a const check(). Disambiguated
// through explicit function-pointer aliases.
static_assert(!std::is_copy_constructible_v<digest_auth_policy>);
static_assert(std::is_move_constructible_v<digest_auth_policy>);
static_assert(std::is_nothrow_move_assignable_v<digest_auth_policy>);
static_assert(std::is_nothrow_move_constructible_v<digest_auth_policy>);
using digest_fixed_create = http::outcome (*)(std::string, std::string,
std::string,
digest_auth_options,
digest_auth_policy&);
using digest_source_create = http::outcome (*)(
std::string, digest_ha1_source, digest_auth_options,
digest_auth_policy&);
static_assert(std::is_same_v<
decltype(static_cast<digest_fixed_create>(
&digest_auth_policy::create)),
digest_fixed_create>);
static_assert(std::is_same_v<
decltype(static_cast<digest_source_create>(
&digest_auth_policy::create)),
digest_source_create>);
static_assert(std::is_same_v<
decltype(std::declval<const digest_auth_policy&>().check(
std::declval<const http::request_head&>())),
digest_auth_verdict>);
static_assert(std::is_same_v<
decltype(std::declval<const digest_auth_policy&>().realm()),
const std::string&>);
static_assert(noexcept(std::declval<const digest_auth_policy&>().realm()));
static_assert(std::is_same_v<
decltype(std::declval<const digest_auth_policy&>().algorithm()),
digest_algorithm>);
static_assert(noexcept(
std::declval<const digest_auth_policy&>().algorithm()));
// The Digest guard adapter wraps a route handler into a route handler.
static_assert(std::is_same_v<decltype(make_digest_guard),
srv::route_handler(digest_auth_policy,
srv::route_handler)>);
} // namespace
int main() {
// Nothing runs: compiling and linking is the contract.
return 0;
}