-
Notifications
You must be signed in to change notification settings - Fork 901
/
rest.spec.ts
257 lines (237 loc) · 9.4 KB
/
rest.spec.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
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
import { expect } from "chai";
import { expectStatusCode, registerTenant, registerUser } from "./helpers";
import { describeAuthEmulator, PROJECT_ID } from "./setup";
describeAuthEmulator("REST API mapping", ({ authApi }) => {
it("should respond to status checks", async () => {
await authApi()
.get("/")
.then((res) => {
expectStatusCode(200, res);
expect(res.body.authEmulator).to.be.an("object");
});
});
it("should allow cross-origin requests", async () => {
await authApi()
.options("/")
.set("Origin", "example.com")
.set("Access-Control-Request-Headers", "Authorization,X-Client-Version,X-Whatever-Header")
.set("Access-Control-Request-Private-Network", "true")
.then((res) => {
expectStatusCode(204, res);
// Some clients (including older browsers and jsdom) won't accept '*' as a
// wildcard, so we need to reflect Origin and Access-Control-Request-Headers.
// https://github.com/firebase/firebase-tools/issues/3200
expect(res.header["access-control-allow-origin"]).to.eql("example.com");
expect((res.header["access-control-allow-headers"] as string).split(",")).to.have.members([
"Authorization",
"X-Client-Version",
"X-Whatever-Header",
]);
// Check that access-control-allow-private-network = true
// Enables accessing locahost when site is exposed via tunnel see https://github.com/firebase/firebase-tools/issues/4227
expect(res.header["access-control-allow-private-network"]).to.eql("true");
});
});
it("should handle integer values for enums", async () => {
// Proto integer value for "EMAIL_SIGNIN". Android client SDK sends this.
const requestType = 6;
await authApi()
.post("/identitytoolkit.googleapis.com/v1/accounts:sendOobCode")
.set("Authorization", "Bearer owner")
.send({ email: "bob@example.com", requestType, returnOobLink: true })
.then((res) => {
expectStatusCode(200, res);
expect(res.body.oobLink).to.include("mode=signIn");
});
});
it("should handle integer values for enums (legacy API path)", async () => {
// Proto integer value for "EMAIL_SIGNIN". Android client SDK sends this.
const requestType = 6;
await authApi()
.post("/www.googleapis.com/identitytoolkit/v3/relyingparty/getOobConfirmationCode")
.set("Authorization", "Bearer owner")
.send({ email: "bob@example.com", requestType, returnOobLink: true })
.then((res) => {
expectStatusCode(200, res);
expect(res.body.oobLink).to.include("mode=signIn");
});
});
it("should convert numbers to strings for type:string fields", async () => {
// validSince should be an int64-formatted string, but Node.js Admin SDK
// sends it as a plain number (without quotes).
const validSince = 1611780718;
await authApi()
.post("/identitytoolkit.googleapis.com/v1/accounts:update")
.set("Authorization", "Bearer owner")
.send({ localId: "nosuch", validSince })
.then((res) => {
expectStatusCode(400, res);
// It should pass JSON schema validation and get into handler logic.
expect(res.body.error.message).to.equal("USER_NOT_FOUND");
});
});
});
describeAuthEmulator("authentication", ({ authApi }) => {
it("should throw 403 if API key is not provided", async () => {
await authApi()
.post("/identitytoolkit.googleapis.com/v1/accounts:signUp")
.query({
/* no API "key" */
})
.send({ returnSecureToken: true })
.then((res) => {
expectStatusCode(403, res);
expect(res.body.error).to.have.property("status").equal("PERMISSION_DENIED");
});
});
it("should accept API key as a query parameter", async () => {
await authApi()
.post("/identitytoolkit.googleapis.com/v1/accounts:signUp")
.query({ key: "fake-api-key" })
.send({})
.then((res) => {
expectStatusCode(200, res);
expect(res.body).not.to.have.property("error");
});
});
it("should accept API key in HTTP Header x-goog-api-key", async () => {
await authApi()
.post("/identitytoolkit.googleapis.com/v1/accounts:signUp")
.set("x-goog-api-key", "fake-api-key")
.send({})
.then((res) => {
expectStatusCode(200, res);
expect(res.body).not.to.have.property("error");
});
});
it("should ignore non-Bearer Authorization headers", async () => {
await authApi()
.post("/identitytoolkit.googleapis.com/v1/accounts:signUp")
// This has no effect on the request handling, since it is not Bearer.
.set("Authorization", "Basic YWxhZGRpbjpvcGVuc2VzYW1l")
.query({
/* no API "key" */
})
.send({ returnSecureToken: true })
.then((res) => {
expectStatusCode(403, res);
expect(res.body.error).to.have.property("status").equal("PERMISSION_DENIED");
});
});
it("should treat Bearer owner as authenticated to project", async () => {
await authApi()
.post("/identitytoolkit.googleapis.com/v1/accounts:signUp")
// This authenticates as valid OAuth 2 credentials, no API key needed.
.set("Authorization", "Bearer owner")
.send({
// This field requires OAuth 2 and should work correctly.
targetProjectId: "example2",
})
.then((res) => {
expectStatusCode(200, res);
expect(res.body).not.to.have.property("error");
});
});
it("should ignore casing of Bearer / owner in Authorization header", async () => {
await authApi()
.post("/identitytoolkit.googleapis.com/v1/accounts:signUp")
// This authenticates as valid OAuth 2 credentials, no API key needed.
.set("Authorization", "bEArEr OWNER")
.send({
// This field requires OAuth 2 and should work correctly.
targetProjectId: "example2",
})
.then((res) => {
expectStatusCode(200, res);
expect(res.body).not.to.have.property("error");
});
});
it("should treat production service account as authenticated to project", async () => {
await authApi()
.post("/identitytoolkit.googleapis.com/v1/accounts:signUp")
// This authenticates as owner of the default projectId. The exact value
// and expiry don't matter -- the Emulator only checks for the format.
.set(
"Authorization",
// Not an actual token. Breaking it down to avoid linter false positives.
"Bearer ya" + "29.AHES0ZZZZZ0fff" + "ff0XXXX0mmmm0wwwww0-LL_l-0bb0b0bbbbbb"
)
.send({
// This field requires OAuth 2 and should work correctly.
targetProjectId: "example2",
})
.then((res) => {
expectStatusCode(200, res);
expect(res.body).not.to.have.property("error");
});
});
it("should deny requests with targetProjectId but without OAuth 2", async () => {
await authApi()
.post("/identitytoolkit.googleapis.com/v1/accounts:signUp")
.query({ key: "fake-api-key" })
.send({
// Specifying this field requires OAuth 2. API key is not sufficient.
targetProjectId: "example2",
})
.then((res) => {
expectStatusCode(400, res);
expect(res.body.error)
.to.have.property("message")
.equals(
"INSUFFICIENT_PERMISSION : Only authenticated requests can specify target_project_id."
);
});
});
it("should deny requests where tenant IDs do not match in the request body and path", async () => {
await authApi()
.post(
"/identitytoolkit.googleapis.com/v1/projects/project-id/tenants/tenant-id/accounts:delete"
)
.set("Authorization", "Bearer owner")
.send({ localId: "local-id", tenantId: "mismatching-tenant-id" })
.then((res) => {
expectStatusCode(400, res);
expect(res.body.error).to.have.property("message").equals("TENANT_ID_MISMATCH");
});
});
it("should deny requests where tenant IDs do not match in the ID token and path", async () => {
const tenant = await registerTenant(authApi(), PROJECT_ID, {
disableAuth: false,
allowPasswordSignup: true,
});
const { idToken } = await registerUser(authApi(), {
email: "alice@example.com",
password: "notasecret",
tenantId: tenant.tenantId,
});
await authApi()
.post(
`/identitytoolkit.googleapis.com/v1/projects/${PROJECT_ID}/tenants/not-matching-tenant-id/accounts:lookup`
)
.send({ idToken })
.set("Authorization", "Bearer owner")
.then((res) => {
expectStatusCode(400, res);
expect(res.body.error).to.have.property("message").equals("TENANT_ID_MISMATCH");
});
});
it("should deny requests where tenant IDs do not match in the ID token and request body", async () => {
const tenant = await registerTenant(authApi(), PROJECT_ID, {
disableAuth: false,
allowPasswordSignup: true,
});
const { idToken } = await registerUser(authApi(), {
email: "alice@example.com",
password: "notasecret",
tenantId: tenant.tenantId,
});
await authApi()
.post(`/identitytoolkit.googleapis.com/v1/projects/${PROJECT_ID}/accounts:lookup`)
.send({ idToken, tenantId: "not-matching-tenant-id" })
.set("Authorization", "Bearer owner")
.then((res) => {
expectStatusCode(400, res);
expect(res.body.error).to.have.property("message").equals("TENANT_ID_MISMATCH");
});
});
});