-
-
Notifications
You must be signed in to change notification settings - Fork 229
/
Copy pathOwidGoogleAuth.ts
55 lines (51 loc) · 2.13 KB
/
OwidGoogleAuth.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
import {
GDOCS_CLIENT_EMAIL,
GDOCS_CLIENT_ID,
GDOCS_PRIVATE_KEY,
} from "../settings/serverSettings.js"
import { google, Auth, docs_v1 } from "googleapis"
export class OwidGoogleAuth {
static cachedGoogleReadonlyAuth?: Auth.GoogleAuth
static cachedGoogleReadWriteAuth?: Auth.GoogleAuth
static cachedGoogleClient?: docs_v1.Docs
static areGdocAuthKeysSet(): boolean {
return !!(GDOCS_PRIVATE_KEY && GDOCS_CLIENT_EMAIL && GDOCS_CLIENT_ID)
}
static getGoogleReadWriteAuth(): Auth.GoogleAuth {
if (!OwidGoogleAuth.cachedGoogleReadWriteAuth) {
OwidGoogleAuth.cachedGoogleReadWriteAuth =
new google.auth.GoogleAuth({
credentials: {
type: "service_account",
private_key: GDOCS_PRIVATE_KEY.split("\\n").join("\n"),
client_email: GDOCS_CLIENT_EMAIL,
client_id: GDOCS_CLIENT_ID,
},
scopes: [
"https://www.googleapis.com/auth/documents",
"https://www.googleapis.com/auth/drive.file",
],
})
}
return OwidGoogleAuth.cachedGoogleReadWriteAuth
}
static getGoogleReadonlyAuth(): Auth.GoogleAuth {
if (!OwidGoogleAuth.cachedGoogleReadonlyAuth) {
OwidGoogleAuth.cachedGoogleReadonlyAuth =
new google.auth.GoogleAuth({
credentials: {
type: "service_account",
private_key: GDOCS_PRIVATE_KEY.split("\\n").join("\n"),
client_email: GDOCS_CLIENT_EMAIL,
client_id: GDOCS_CLIENT_ID,
},
// Scopes can be specified either as an array or as a single, space-delimited string.
scopes: [
"https://www.googleapis.com/auth/documents.readonly",
"https://www.googleapis.com/auth/drive.readonly",
],
})
}
return OwidGoogleAuth.cachedGoogleReadonlyAuth
}
}