-
-
Notifications
You must be signed in to change notification settings - Fork 96
/
01_auth.ts
162 lines (150 loc) Β· 3.58 KB
/
01_auth.ts
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
import {
cleanUsername,
clientFirstMessageBare,
HI,
passwordDigest,
} from "../../src/auth/mod.ts";
import { MongoClient } from "../../src/client.ts";
import { testWithClient } from "../common.ts";
import { assert, assertEquals } from "../test.deps.ts";
const hostname = "127.0.0.1";
interface PasswordValid {
username: string;
password: string;
digest: string;
}
const passwordValdis: PasswordValid[] = [
{
username: "user",
password: "pencil",
digest: "1c33006ec1ffd90f9cadcbcc0e118200",
},
{
username: "test",
password: "test",
digest: "a6de521abefc2fed4f5876855a3484f5",
},
];
export default function authTests() {
passwordValdis.forEach(({ username, password, digest }) => {
Deno.test({
name: `passwordDigest:${username}:${password}`,
fn() {
const digestRes: string = passwordDigest(username, password);
assertEquals(digestRes, digest);
},
});
});
Deno.test({
name: "clientFirstMessageBare",
fn() {
const username = "1234";
const nonce = new TextEncoder().encode("qwer");
const result: Uint8Array = clientFirstMessageBare(username, nonce);
const expected: Uint8Array = Uint8Array.from(
[
110,
61,
49,
50,
51,
52,
44,
114,
61,
99,
88,
100,
108,
99,
103,
61,
61,
],
);
assertEquals(expected, result);
},
});
Deno.test({
name: "cleanUsername",
fn() {
const username: string = "first=12,last=34";
const expected: string = "first=3D12=2Clast=34";
const result: string = cleanUsername(username);
assertEquals(expected, result);
},
});
const salt = "rQ9ZY3MntBeuP3E1TDVC4w";
const iter = 10000;
const data = "1c33006ec1ffd90f9cadcbcc0e118200";
Deno.test({
name: "HI",
fn() {
const saltedPassword = HI(
data,
(new TextEncoder()).encode(salt),
iter,
"sha1",
);
assertEquals(
saltedPassword,
[
72,
84,
156,
182,
17,
64,
30,
116,
86,
233,
7,
39,
65,
137,
142,
164,
0,
110,
78,
230,
],
);
},
});
testWithClient("createUser", async (client) => {
const db = client.database("test");
await db.createUser("user1", "y3mq3mpZ3J6PGfgg");
await db.createUser("user2", "Qa6WkQSuXF425sWZ");
});
Deno.test("connect authorization test 1 - test db", async () => {
var username = "user1";
var password = "y3mq3mpZ3J6PGfgg";
const client = new MongoClient();
await client.connect(
`mongodb://${username}:${password}@${hostname}:27017/test`,
);
const names = await client.listDatabases();
assert(names instanceof Array);
assert(names.length > 0);
client.close();
});
Deno.test("connect authorization test 2 - test db", async () => {
var username = "user2";
var password = "Qa6WkQSuXF425sWZ";
const client = new MongoClient();
await client.connect(
`mongodb://${username}:${password}@${hostname}:27017/test`,
);
const names = await client.listDatabases();
assert(names instanceof Array);
assert(names.length > 0);
client.close();
});
testWithClient("dropUser", async (client) => {
const db = client.database("test");
await db.dropUser("user1");
await db.dropUser("user2");
});
}