-
Notifications
You must be signed in to change notification settings - Fork 2
/
authentication-tests.cpp
123 lines (100 loc) · 4.12 KB
/
authentication-tests.cpp
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
/**
Copyright 2009-2020 Red Anchor Trading Co. Ltd.
Distributed under the Boost Software License, Version 1.0.
See <http://www.boost.org/LICENSE_1_0.txt>
*/
#include "fost-inet-test.hpp"
#include <fost/http>
#include <fost/datetime>
using namespace fostlib;
FSL_TEST_SUITE(authentication);
FSL_TEST_FUNCTION(no_authentication) {
std::map<string, string> keys;
http::user_agent::request r("GET", fostlib::url());
http::server::request request(
r.method(), r.address().pathspec(),
std::make_unique<binary_body>(r.headers()));
{
http::fost_authn authn(http::fost_authentication(keys, request));
FSL_CHECK(!authn.authenticated());
FSL_CHECK(!authn.under_attack());
FSL_CHECK_EQ(authn.error().value(), "No authorization header");
}
request.data()->headers().set("Authorization", "Invalid");
{
http::fost_authn authn(http::fost_authentication(keys, request));
FSL_CHECK(!authn.authenticated());
FSL_CHECK(!authn.under_attack());
FSL_CHECK_EQ(
authn.error().value(),
"Non FOST authentication not implemented");
}
request.data()->headers().set("Authorization", "FOST invalid");
{
http::fost_authn authn(http::fost_authentication(keys, request));
FSL_CHECK(!authn.authenticated());
FSL_CHECK(!authn.under_attack());
FSL_CHECK_EQ(authn.error().value(), "No signed headers found");
}
request.data()->headers().set("X-FOST-Headers");
{
http::fost_authn authn(http::fost_authentication(keys, request));
FSL_CHECK(!authn.authenticated());
FSL_CHECK(!authn.under_attack());
FSL_CHECK_EQ(authn.error().value(), "No signed headers found");
}
request.data()->headers().set("X-FOST-Headers", "X-FOST-Headers");
{
http::fost_authn authn(http::fost_authentication(keys, request));
FSL_CHECK(!authn.authenticated());
FSL_CHECK(!authn.under_attack());
FSL_CHECK_EQ(authn.error().value(), "No X-FOST-Timestamp header found");
}
request.data()->headers().set("X-FOST-Timestamp", "2010-01-01 00:00:00");
{
http::fost_authn authn(http::fost_authentication(keys, request));
FSL_CHECK(!authn.authenticated());
FSL_CHECK(!authn.under_attack());
FSL_CHECK_EQ(authn.error().value(), "Clock skew is too high");
}
request.data()->headers().set(
"X-FOST-Timestamp", coerce<string>(timestamp::now()));
{
http::fost_authn authn(http::fost_authentication(keys, request));
FSL_CHECK(!authn.authenticated());
FSL_CHECK(!authn.under_attack());
FSL_CHECK_EQ(authn.error().value(), "No FOST key:signature pair found");
}
request.data()->headers().set("Authorization", "FOST invalid:invalid");
{
http::fost_authn authn(http::fost_authentication(keys, request));
FSL_CHECK(!authn.authenticated());
FSL_CHECK(authn.under_attack());
FSL_CHECK_EQ(authn.error().value(), "Key not found");
}
keys["key"] = "secret";
request.data()->headers().set("Authorization", "FOST key:invalid");
{
http::fost_authn authn(http::fost_authentication(keys, request));
FSL_CHECK(!authn.authenticated());
FSL_CHECK(authn.under_attack());
FSL_CHECK_EQ(authn.error().value(), "Signature mismatch");
}
}
FSL_TEST_FUNCTION(fost_authentication) {
std::map<string, string> keys;
keys["Not a key"] = "Not a secret";
http::user_agent::request r("GET", fostlib::url());
http::fost_authentication(
"Not a key", "Not a secret", std::set<string>(), r);
FSL_CHECK(r.headers().exists("Authorization"));
FSL_CHECK(r.headers().exists("X-FOST-Timestamp"));
FSL_CHECK_EQ(r.headers()["X-FOST-Headers"].value(), "X-FOST-Headers");
http::server::request request(
r.method(), r.address().pathspec(),
std::make_unique<binary_body>(r.headers()));
http::fost_authn authn(http::fost_authentication(keys, request));
FSL_CHECK(authn.authenticated());
FSL_CHECK(authn.under_attack());
FSL_CHECK(not authn.error());
}