-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBasicAuthTests.cs
More file actions
344 lines (308 loc) · 13.9 KB
/
Copy pathBasicAuthTests.cs
File metadata and controls
344 lines (308 loc) · 13.9 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
namespace NpgsqlRestTests;
public static partial class Database
{
public static void BasicAuthTests()
{
script.Append("""
create function get_basic_auth_user(
_user_name text = null --mapped to name claim
)
returns text
language sql as $$
select _user_name;
$$;
-- ❯ dotnet run -- hash my_password
-- Myb55+6lW6iiUOI3opLkysOaS8J0NNIuQ+qE2SGaKs3r62ngDJROrhX75+zmLC7t
comment on function get_basic_auth_user(text) is '
basic_auth my_name Myb55+6lW6iiUOI3opLkysOaS8J0NNIuQ+qE2SGaKs3r62ngDJROrhX75+zmLC7t
user_params
';
create function get_basic_auth_no_creds(
_user_name text = null --mapped to name claim
)
returns text
language sql as $$
select _user_name;
$$;
comment on function get_basic_auth_no_creds(text) is '
basic_auth
user_params
';
create function get_basic_auth_challenge_command(
_user_claims json -- mapped to claims
)
returns text
language sql as $$
select _user_claims;
$$;
comment on function get_basic_auth_challenge_command(json) is '
basic_auth
challenge_command = select * from auth_challenge_command($1, $2, $3, $4, $5)
user_params
';
create function get_basic_auth_challenge_command_pass(
_user_claims json -- mapped to claims
)
returns text
language sql as $$
select _user_claims;
$$;
comment on function get_basic_auth_challenge_command_pass(json) is '
basic_auth my_name Myb55+6lW6iiUOI3opLkysOaS8J0NNIuQ+qE2SGaKs3r62ngDJROrhX75+zmLC7t
challenge_command = select * from auth_challenge_command($1, $2, $3, $4, $5)
user_params
';
create function auth_challenge_command(
_user text,
_password text,
_valid boolean,
_realm text,
_path text
)
returns table (
name_identifier int,
name text,
password text,
valid boolean,
realm text,
path text
)
language sql as $$
select 1, _user, _password, _valid, _realm, _path;
$$;
create function get_basic_auth_challenge_command_failed(
_user_claims json -- mapped to claims
)
returns text
language sql as $$
select _user_claims;
$$;
comment on function get_basic_auth_challenge_command_failed(json) is '
basic_auth
challenge_command = select * from auth_challenge_command_failed($1, $2, $3, $4, $5)
user_params
';
create function auth_challenge_command_failed(
_user text,
_password text,
_valid boolean,
_realm text,
_path text
)
returns table (
status boolean,
name_identifier int,
name text,
password text,
valid boolean,
realm text,
path text
)
language sql as $$
select false, 1, _user, _password, _valid, _realm, _path;
$$;
create function get_basic_auth_multiple_users(
_user_name text = null --mapped to name claim
)
returns text
language sql as $$
select _user_name;
$$;
-- user1/pass1 = um4K594nL6pBQx2el0lcbKKLADof1k9atRYKy+G14f6BQPtSCkwO6qz1wJ1d9Tx/
-- user2/pass2 = TIDVxenk9gSqApyI82XDuqUaigQ5OdBIecfRtq7wFWtHT3Ffx2s+noIjvFCAw90z
comment on function get_basic_auth_multiple_users(text) is '
basic_auth user1 um4K594nL6pBQx2el0lcbKKLADof1k9atRYKy+G14f6BQPtSCkwO6qz1wJ1d9Tx/
basic_auth user2 TIDVxenk9gSqApyI82XDuqUaigQ5OdBIecfRtq7wFWtHT3Ffx2s+noIjvFCAw90z
user_params
';
""");
}
}
[Collection("TestFixture")]
public class BasicAuthTests(TestFixture test)
{
[Fact]
public async Task Test_get_basic_auth_user_test1()
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/get-basic-auth-user");
// ❯ npgsqlrest basic_auth my_name my_password
// Authorization: Basic bXlfbmFtZTpteV9wYXNzd29yZA==
request.Headers.Add("Authorization", "Basic bXlfbmFtZTpteV9wYXNzd29yZA==");
using var result = await test.Client.SendAsync(request);
var response = await result.Content.ReadAsStringAsync();
result.StatusCode.Should().Be(HttpStatusCode.OK);
response.Should().Be("my_name");
}
[Fact]
public async Task Test_get_basic_auth_user_test2()
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/get-basic-auth-user");
request.Headers.Add("Authorization", "Basic XXX");
using var result = await test.Client.SendAsync(request);
result.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
var content = await result.Content.ReadAsStringAsync();
content.Should().Contain("\"status\":401");
content.Should().Contain("\"title\":\"Unauthorized\"");
}
[Fact]
public async Task Test_get_basic_auth_user_test3()
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/get-basic-auth-user");
using var result = await test.Client.SendAsync(request);
result.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
var content = await result.Content.ReadAsStringAsync();
content.Should().Contain("\"status\":401");
content.Should().Contain("\"title\":\"Unauthorized\"");
}
[Fact]
public async Task Test_get_basic_auth_user_test4()
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/get-basic-auth-user");
request.Headers.Add("Authorization", "Basic eHh4"); // xxx
using var result = await test.Client.SendAsync(request);
result.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
var content = await result.Content.ReadAsStringAsync();
content.Should().Contain("\"status\":401");
content.Should().Contain("\"title\":\"Unauthorized\"");
}
[Fact]
public async Task Test_get_basic_auth_invalid_username()
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/get-basic-auth-user");
request.Headers.Add("Authorization", "Basic eHh4Onl5eQ=="); // xxx:yyy
using var result = await test.Client.SendAsync(request);
result.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
var content = await result.Content.ReadAsStringAsync();
content.Should().Contain("\"status\":401");
content.Should().Contain("\"title\":\"Unauthorized\"");
}
[Fact]
public async Task Test_get_basic_auth_invalid_password()
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/get-basic-auth-user");
request.Headers.Add("Authorization", "Basic bXlfbmFtZTp5eXk="); // my_name:yyy
using var result = await test.Client.SendAsync(request);
result.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
var content = await result.Content.ReadAsStringAsync();
content.Should().Contain("\"status\":401");
content.Should().Contain("\"title\":\"Unauthorized\"");
}
[Fact]
public async Task Test_get_basic_auth_no_creds()
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/get-basic-auth-no-creds");
// ❯ npgsqlrest basic_auth my_name my_password
// Authorization: Basic bXlfbmFtZTpteV9wYXNzd29yZA==
request.Headers.Add("Authorization", "Basic bXlfbmFtZTpteV9wYXNzd29yZA=="); // valid creds but no creds set in function
using var result = await test.Client.SendAsync(request);
result.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
var content = await result.Content.ReadAsStringAsync();
content.Should().Contain("\"status\":401");
content.Should().Contain("\"title\":\"Unauthorized\"");
}
[Fact]
public async Task Test_get_basic_auth_challenge_command_invalid_request1()
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/get-basic-auth-challenge-command");
using var result = await test.Client.SendAsync(request);
var response = await result.Content.ReadAsStringAsync();
result.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
response.Should().Contain("\"status\":401");
response.Should().Contain("\"title\":\"Unauthorized\"");
}
[Fact]
public async Task Test_get_basic_auth_challenge_command_invalid_request2()
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/get-basic-auth-challenge-command");
request.Headers.Add("Authorization", "Basic eHh4"); // xxx
using var result = await test.Client.SendAsync(request);
var response = await result.Content.ReadAsStringAsync();
result.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
response.Should().Contain("\"status\":401");
response.Should().Contain("\"title\":\"Unauthorized\"");
}
[Fact]
public async Task Test_get_basic_auth_challenge_command_valid_request()
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/get-basic-auth-challenge-command");
request.Headers.Add("Authorization", "Basic eHh4Onl5eQ=="); // xxx:yyy
using var result = await test.Client.SendAsync(request);
var response = await result.Content.ReadAsStringAsync();
result.StatusCode.Should().Be(HttpStatusCode.OK);
response.Should().Be("{\"name_identifier\":\"1\",\"name\":\"xxx\",\"password\":\"yyy\",\"valid\":null,\"realm\":\"NpgsqlRest\",\"path\":\"/api/get-basic-auth-challenge-command\"}");
}
[Fact]
public async Task Test_get_basic_auth_challenge_command_valid_request_valid_pass()
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/get-basic-auth-challenge-command-pass");
request.Headers.Add("Authorization", "Basic bXlfbmFtZTpteV9wYXNzd29yZA=="); // xxx:yyy
using var result = await test.Client.SendAsync(request);
var response = await result.Content.ReadAsStringAsync();
result.StatusCode.Should().Be(HttpStatusCode.OK);
response.Should().Be("{\"name_identifier\":\"1\",\"name\":\"my_name\",\"password\":\"my_password\",\"valid\":\"True\",\"realm\":\"NpgsqlRest\",\"path\":\"/api/get-basic-auth-challenge-command-pass\"}");
}
[Fact]
public async Task Test_get_basic_auth_challenge_command_failed()
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/get-basic-auth-challenge-command-failed");
request.Headers.Add("Authorization", "Basic eHh4Onl5eQ=="); // xxx:yyy
using var result = await test.Client.SendAsync(request);
result.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
var content = await result.Content.ReadAsStringAsync();
content.Should().Contain("\"status\":401");
content.Should().Contain("\"title\":\"Unauthorized\"");
}
[Fact]
public async Task Test_get_basic_auth_multiple_users_test1()
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/get-basic-auth-multiple-users");
request.Headers.Add("Authorization", "Basic dXNlcjE6cGFzczE="); // user1:pass1
using var result = await test.Client.SendAsync(request);
var response = await result.Content.ReadAsStringAsync();
result.StatusCode.Should().Be(HttpStatusCode.OK);
response.Should().Be("user1");
}
[Fact]
public async Task Test_get_basic_auth_multiple_users_test2()
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/get-basic-auth-multiple-users");
request.Headers.Add("Authorization", "Basic dXNlcjI6cGFzczI="); // user2:pass2
using var result = await test.Client.SendAsync(request);
var response = await result.Content.ReadAsStringAsync();
result.StatusCode.Should().Be(HttpStatusCode.OK);
response.Should().Be("user2");
}
[Fact]
public async Task Test_get_basic_auth_multiple_users_test3()
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/get-basic-auth-multiple-users");
request.Headers.Add("Authorization", "Basic dXNlcjM6cGFzczM="); // user3:pass3
using var result = await test.Client.SendAsync(request);
result.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
var content = await result.Content.ReadAsStringAsync();
content.Should().Contain("\"status\":401");
content.Should().Contain("\"title\":\"Unauthorized\"");
}
[Fact]
public async Task Test_get_basic_auth_multiple_users_test4()
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/get-basic-auth-multiple-users");
request.Headers.Add("Authorization", "Basic dXNlcjI6cGFzczM="); // user2:pass3
using var result = await test.Client.SendAsync(request);
result.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
var content = await result.Content.ReadAsStringAsync();
content.Should().Contain("\"status\":401");
content.Should().Contain("\"title\":\"Unauthorized\"");
}
[Fact]
public async Task Test_get_basic_auth_multiple_users_test5()
{
using var request = new HttpRequestMessage(HttpMethod.Get, "/api/get-basic-auth-multiple-users");
request.Headers.Add("Authorization", "Basic dXNlcjM6cGFzczI="); // user3:pass2
using var result = await test.Client.SendAsync(request);
result.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
var content = await result.Content.ReadAsStringAsync();
content.Should().Contain("\"status\":401");
content.Should().Contain("\"title\":\"Unauthorized\"");
}
}