-
Notifications
You must be signed in to change notification settings - Fork 318
/
Copy pathtoken.ts
60 lines (56 loc) · 2 KB
/
token.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
import dayjs from 'dayjs';
import { apis } from '../apis';
import * as LocalStorage from '../local-storage';
import { Mutex } from '../utilities';
// #if BROWSER === 'chrome'
const OAUTH2_CLIENT_ID = '304167046827-aqukv3fe891j0f9cu94i5aljhsecgpen.apps.googleusercontent.com';
// #else
const OAUTH2_CLIENT_ID = '304167046827-a53p7d9jopn9nvbo7e183t966rfcp9d1.apps.googleusercontent.com';
// #endif
const OAUTH2_SCOPE = 'https://www.googleapis.com/auth/drive.file';
const mutex = new Mutex();
export async function requestToken(interactive: boolean): Promise<string> {
// #if BROWSER === 'chrome'
try {
// #endif
return await mutex.lock(async () => {
const { tokenCache } = await LocalStorage.load('tokenCache');
if (tokenCache && dayjs().isBefore(dayjs(tokenCache.expirationDate))) {
return tokenCache.token;
}
const authURL =
'https://accounts.google.com/o/oauth2/auth' +
`?client_id=${OAUTH2_CLIENT_ID}` +
'&response_type=token' +
`&redirect_uri=${encodeURIComponent(apis.identity.getRedirectURL())}` +
`&scope=${encodeURIComponent(OAUTH2_SCOPE)}`;
const redirectURL = await apis.identity.launchWebAuthFlow({ interactive, url: authURL });
const params = new URLSearchParams(new URL(redirectURL).hash.slice(1));
if (params.has('error')) {
throw new Error(params.get('error')!);
}
const token = params.get('access_token')!;
const expirationDate = dayjs()
.add(Number(params.get('expires_in')!), 'second')
.toISOString();
await LocalStorage.store({ tokenCache: { token, expirationDate } });
return token;
});
// #if BROWSER === 'chrome'
} catch (e) {
if (interactive) {
throw e;
}
try {
return await apis.identity.getAuthToken({ interactive: false });
} catch {
throw e;
}
}
// #endif
}
export async function clearTokenCache(): Promise<void> {
await mutex.lock(async () => {
await LocalStorage.store({ tokenCache: null });
});
}