-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathauth.ts
295 lines (261 loc) · 7.86 KB
/
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
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
/** Load service account information from a `.env` if present and export the
* service account information for connecting to the Google Datastore.
*
* @module
*/
import { type Context } from "acorn";
import {
Datastore,
DatastoreError,
entityToObject,
objectSetKey,
objectToEntity,
} from "google_datastore";
import { S3Bucket } from "s3";
import { SQSQueue } from "sqs";
import { load } from "std/dotenv/mod.ts";
import { kinds, ROOT_SYMBOL } from "./consts.ts";
/** The service account keys used when connecting to the Google Datastore. */
export let keys: {
client_email: string;
private_key: string;
private_key_id: string;
project_id: string;
};
/** Algolia credentials required to upload docNodes to algolia. */
export let algoliaKeys: { appId: string; apiKey: string; searchApiKey: string };
export let awsKeys: {
region: string;
accessKeyID: string;
secretKey: string;
sessionToken?: string;
endpointURL?: string;
};
let readyResolve: (value?: unknown) => void;
/** A promise that is resolved when auth keys are properly set. This allows for
* async resolution of environment variables. */
export const readyPromise = new Promise((res) => readyResolve = res);
(async () => {
await load({ export: true, examplePath: "" });
readyResolve!();
const privateKey = Deno.env.get("GOOGLE_PRIVATE_KEY") ?? "";
keys = {
client_email: Deno.env.get("GOOGLE_CLIENT_EMAIL") ?? "",
private_key:
(privateKey.startsWith(`"`)
? JSON.parse(privateKey)
: privateKey) as string,
private_key_id: Deno.env.get("GOOGLE_PRIVATE_KEY_ID") ?? "",
project_id: Deno.env.get("GOOGLE_PROJECT_ID") ?? "",
};
algoliaKeys = {
appId: Deno.env.get("ALGOLIA_APP_ID") ?? "",
apiKey: Deno.env.get("ALGOLIA_API_KEY") ?? "",
searchApiKey: Deno.env.get("ALGOLIA_SEARCH_API_KEY") ?? "",
};
awsKeys = {
region: Deno.env.get("AWS_REGION") ?? "",
accessKeyID: Deno.env.get("AWS_ACCESS_KEY_ID") ?? "",
secretKey: Deno.env.get("AWS_SECRET_ACCESS_KEY") ?? "",
sessionToken: Deno.env.get("AWS_SESSION_TOKEN"),
endpointURL: Deno.env.get("S3_ENDPOINT_URL"),
};
})();
interface TokenManagerJson {
data: Record<string, { token: string; created: Date | string }>;
}
class TokenManager {
#dirty = false;
#tokens = new Map<string, string>();
#ids = new Map<string, { token: string; created: Date }>();
get dirty(): boolean {
return this.#dirty;
}
constructor(init?: TokenManagerJson) {
if (init) {
for (const [id, { token, created }] of Object.entries(init.data)) {
this.#ids.set(id.toLocaleLowerCase(), {
token,
created: typeof created === "string" ? new Date(created) : created,
});
this.#tokens.set(token, id.toLocaleLowerCase());
}
}
}
create(id: string): string {
const token = crypto.randomUUID();
this.#dirty = true;
this.#ids.set(id.toLocaleLowerCase(), { token, created: new Date() });
this.#tokens.set(token, id.toLocaleLowerCase());
return btoa(token);
}
has(id: string): Date | undefined {
const item = this.#ids.get(id);
return item && new Date(item.created);
}
lookup(
token: string | undefined | null,
): { id: string; created: Date } | undefined {
if (!token) {
return undefined;
}
const matches = token.match(/^bearer\s(\S+)/i);
if (matches) {
[, token] = matches;
}
const id = this.#tokens.get(atob(token));
if (id) {
const { created } = this.#ids.get(id)!;
return { id, created: new Date(created) };
}
}
revoke(tokenOrId: string): void {
this.#dirty = true;
const id = this.#tokens.get(atob(tokenOrId));
const { token } = this.#ids.get(tokenOrId) ?? {};
if (id) {
this.#tokens.delete(atob(tokenOrId));
this.#ids.delete(id);
}
if (token) {
this.#tokens.delete(token);
this.#ids.delete(tokenOrId);
}
}
validate(token: string | undefined | null): boolean {
if (!token) {
return false;
}
const matches = token.match(/^bearer\s(\S+)/i);
if (matches) {
[, token] = matches;
}
return this.#tokens.has(atob(token));
}
toJSON(): TokenManagerJson {
this.#dirty = false;
return { data: Object.fromEntries(this.#ids) };
}
*[Symbol.iterator](): IterableIterator<[string, Date]> {
for (const [string, { created }] of this.#ids) {
yield [string, created];
}
}
}
let datastore: Datastore | undefined;
/** Return an instance of the datastore configured to be authorized using the
* environmental configuration. */
export async function getDatastore(): Promise<Datastore> {
if (datastore) {
return datastore;
}
await readyPromise;
return datastore = new Datastore(keys);
}
let s3Bucket: S3Bucket | undefined;
/** Return an instance of the s3 bucket configured to be authorized using the
* environmental configuration. */
export async function getS3Bucket(): Promise<S3Bucket> {
if (s3Bucket) {
return s3Bucket;
}
await readyPromise;
return s3Bucket = new S3Bucket({
bucket: Deno.env.get("STORAGE_BUCKET")!,
...awsKeys,
});
}
let moderationS3Bucket: S3Bucket | undefined;
/** Return an instance of the s3 moderation bucket configured to be authorized
* using the environmental configuration. */
export async function getModerationS3Bucket(): Promise<S3Bucket | undefined> {
const moderationbucket = Deno.env.get("MODERATION_BUCKET");
if (moderationS3Bucket || !moderationbucket) {
return moderationS3Bucket;
}
await readyPromise;
return moderationS3Bucket = new S3Bucket({
bucket: moderationbucket,
...awsKeys,
});
}
let sqsQueue: SQSQueue | undefined;
/** Return an instance of the s3 moderation bucket configured to be authorized
* using the environmental configuration. */
export async function getSQSQueue(): Promise<SQSQueue> {
if (sqsQueue) {
return sqsQueue;
}
await readyPromise;
return sqsQueue = new SQSQueue({
queueURL: Deno.env.get("BUILD_QUEUE")!,
...awsKeys,
});
}
/** Load API tokens from the datastore. */
export async function loadTokens(): Promise<TokenManager> {
const datastore = await getDatastore();
const result = await datastore.lookup(
datastore.key([kinds.CONFIG_KIND, ROOT_SYMBOL]),
);
let init: TokenManagerJson | undefined;
if (result && result.found && result.found.length === 1) {
console.log(
"%cLoaded%c tokens from datastore.",
"color:green",
"color:none",
);
init = entityToObject(result.found[0].entity);
}
return new TokenManager(init);
}
export async function saveTokens(tokenManager: TokenManager): Promise<void> {
if (!tokenManager.dirty) {
console.log(
"%cSkipping%c saving tokens, not changed.",
"color:yellow",
"color:none",
);
return;
}
const datastore = await getDatastore();
const config = tokenManager.toJSON();
objectSetKey(config, datastore.key([kinds.CONFIG_KIND, ROOT_SYMBOL]));
const entity = objectToEntity(config);
try {
for await (
const _result of datastore.commit(
[{ upsert: entity }],
{ transactional: false },
)
) {
console.log(
"%cSaved%c tokens to datastore.",
"color:green",
"color:none",
);
}
} catch (error) {
if (error instanceof DatastoreError) {
console.error(
`Datastore Error: ${error.status} ${error.message}\n\n${error.statusInfo}`,
);
} else {
console.error("Unexpected error saving tokens.\n", error);
}
}
}
let tokenManager: TokenManager | undefined;
async function getTokenManager(): Promise<TokenManager> {
if (tokenManager) {
return tokenManager;
}
return tokenManager = await loadTokens();
}
export const endpointAuth = {
async authorize(ctx: Context) {
const tokens = await getTokenManager();
return tokens.validate(ctx.request.headers.get("authorization"));
},
};