/
Code.gs
168 lines (159 loc) · 5.18 KB
/
Code.gs
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
var authyKey = 'YourAuthyAPIKeyGoesHere';
var userSession = Session.getTemporaryActiveUserKey();
var userProperties = PropertiesService.getUserProperties();
function addNewUser(formData) {
var addUserURL = "https://api.authy.com/protected/json/users/new";
var userPayload = {
"send_install_link_via_sms": false,
"user[email]" : formData.email,
"user[cellphone]" : formData.phoneNumber,
"user[country_code]" : formData.country
};
var addUserOptions = {
"method" : "POST",
"payload" : userPayload,
"muteHttpExceptions": true
};
addUserOptions.headers = {
"X-Authy-API-Key" : authyKey
};
var newUser = UrlFetchApp.fetch(addUserURL, addUserOptions);
var newUserResponse = newUser.getContentText();
var userResponse = JSON.parse(newUserResponse);
if (newUser.getResponseCode() == 200) {
if (userResponse["success"] == true) {
var authyID = JSON.stringify(userResponse["user"]["id"]);
userProperties.setProperty(userSession, authyID);
var newProperties = {};
newProperties[authyID] = JSON.stringify({
userLoggedIn: '',
pushAuthUuid: ''
});
userProperties.setProperties(newProperties);
return 'Registered Successfully!';
} else {
return 'Something went wrong :(';
}
} else {
return 'Something went wrong :(';
}
}
function verifyTOTP(token) {
var authyID = userProperties.getProperty(userSession);
if (authyID !== null) {
var verifyTOTPURL = "https://api.authy.com/protected/json/verify/" + token + "/" + authyID;
var varifyTOTPOptions = {
"method" : "GET",
"muteHttpExceptions": true
};
varifyTOTPOptions.headers = {
"X-Authy-API-Key" : authyKey
};
var verifyTOTP = UrlFetchApp.fetch(verifyTOTPURL, varifyTOTPOptions);
var verifyTOTPResponse = verifyTOTP.getContentText();
var TOTPResponse = JSON.parse(verifyTOTPResponse);
if (verifyTOTP.getResponseCode() == 200) {
if (TOTPResponse["success"] == "true" && TOTPResponse["token"] == "is valid" && TOTPResponse["message"] == "Token is valid.") {
var updateProperties = JSON.stringify({
userLoggedIn: true,
pushAuthUuid: ''
});
userProperties.setProperty(authyID, updateProperties);
return 'Logging you in!';
} else {
return 'Something went wrong :(';
}
} else {
return 'Something went wrong :(';
}
} else {
return 'Please sign-up first to login.'
}
}
function pushAuth() {
var authyID = userProperties.getProperty(userSession);
if (authyID !== null) {
var pushAuthURL = "https://api.authy.com/onetouch/json/users/" + authyID + "/approval_requests";
var pushAuthPayload = {
"message": "Login requested from Google Apps Script."
};
var pushAuthOptions = {
"method" : "POST",
"payload" : pushAuthPayload,
"muteHttpExceptions": true
};
pushAuthOptions.headers = {
"X-Authy-API-Key" : authyKey
};
var newPushAuthReq = UrlFetchApp.fetch(pushAuthURL, pushAuthOptions);
var newPushAuthResponse = newPushAuthReq.getContentText();
var pushAuthResponse = JSON.parse(newPushAuthResponse);
if (newPushAuthReq.getResponseCode() == 200) {
if (pushAuthResponse["success"] == true) {
var pushAuthUuid = pushAuthResponse["approval_request"]["uuid"];
var updateProperties = JSON.stringify({
userLoggedIn: '',
pushAuthUuid: pushAuthUuid
});
userProperties.setProperty(authyID, updateProperties);
return 'Please check your phone...';
} else {
return 'Something went wrong :(';
}
} else {
return 'Something went wrong :(';
}
} else {
return 'Please sign-up first to login.'
}
}
function doGet(e) {
var htmlFile;
var title;
if (loginStatus()) {
htmlFile = 'Dashboard';
title = 'Login Using Authy!'
} else {
htmlFile = 'Index';
title = 'Login Using Authy!'
}
return HtmlService.createHtmlOutputFromFile(htmlFile).setTitle(title);
}
function doPost(e) {
if (JSON.parse(e.postData.contents).callback_action == "approval_request_status") {
var pushAuthParams = JSON.parse(e.postData.contents);
var pushAuthCallbackUuid = pushAuthParams.uuid;
var authyID = pushAuthParams.authy_id;
if (JSON.parse(userProperties.getProperty(authyID)).pushAuthUuid == pushAuthCallbackUuid) {
if (pushAuthParams.status == "approved") {
var updateProperties = JSON.stringify({
userLoggedIn: true,
pushAuthUuid: pushAuthCallbackUuid
});
userProperties.setProperty(authyID, updateProperties);
}
}
}
}
function loginStatus() {
var loginStatus = false;
var authyID = userProperties.getProperty(userSession);
if (authyID !== null) {
var loginStatus = JSON.parse(userProperties.getProperty(authyID)).userLoggedIn;
}
return loginStatus;
}
function webAppURL(linkAddr) {
var linkAddr = ScriptApp.getService().getUrl();
return linkAddr;
}
function userLogout() {
var authyID = userProperties.getProperty(userSession);
if (authyID !== null) {
userProperties.deleteProperty(authyID);
userProperties.deleteProperty(userSession)
return true;
} else {
return false;
}
}