-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth.ts
81 lines (72 loc) · 2.06 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
import { togglApiInst } from './main';
import { TogglApi } from './toggl';
/**
* @author Joshua Tzucker
* @file auth.ts
*/
// https://developers.google.com/datastudio/connector/reference#setcredentials
interface credentialRequestCb {
userPass: {
username: string;
password: string;
};
userToken: {
username: string;
token: string;
};
key: string;
}
/**
* @override - https://developers.google.com/datastudio/connector/auth#getauthtype
*/
function getAuthType() {
// Enum
let authTypes = cc.AuthType;
// Return auth AuthType
return cc.newAuthTypeResponse().setAuthType(authTypes.KEY).setHelpUrl(AUTH_HELP_URL).build();
}
/**
* @override - https://developers.google.com/datastudio/connector/auth#resetauth
*/
function resetAuth() {
PropertiesService.getUserProperties().deleteProperty(APIKEY_STORAGE);
}
function getUserApiKey() {
let key: string | null = PropertiesService.getUserProperties().getProperty(APIKEY_STORAGE);
return key;
}
/**
* @override - https://developers.google.com/datastudio/connector/auth#isauthvalid
*/
function isAuthValid() {
return validateKey(getUserApiKey());
}
function validateKey(authKey: string | null) {
if (authKey) {
Logger.log(authKey);
var tempInst = new TogglApi(authKey);
// Make a call to /me as simple test of auth authValidity
var response = tempInst.getBasicAcctInfo();
return response.success;
}
return false;
}
/**
* @override - https://developers.google.com/datastudio/connector/auth#setcredentials
* NOTE: This is very special func - is a callback that gets triggered once user saves credential through UI
*/
function setCredentials(request: credentialRequestCb) {
let key = request.key;
if (!validateKey(key)) {
return {
errorCode: 'INVALID_CREDENTIALS'
};
}
PropertiesService.getUserProperties().setProperty(APIKEY_STORAGE, key);
// Update global
togglApiInst.setAuthToken(key);
return {
errorCode: 'NONE'
};
}
export { getUserApiKey };