forked from realByg/office-user-auto-create
-
Notifications
You must be signed in to change notification settings - Fork 5
/
worker.js
286 lines (256 loc) · 8.56 KB
/
worker.js
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
const site_config = {
'notice': '', //公告内容,留空则不显示
'code_store_link': '', //卖激活码的发卡店链接
'line1': '欢迎使用 Office',
'line2': '你可在这里创作、沟通、协作并完成重要工作。',
'code_api_link': ''
};
const ms_config = {
'tenant_id': '',
'client_id': '',
'client_secret': '',
'subscriptions': [{
'name': '', //eg. Office 365 A1 for faculty
'skuId': ''
}, {
'name': '',
'skuId': ''
}],
'domains': ['domain.com', 'domain.net']
};
const recaptcha_config = {
'site_key': '',
'secret_key': ''
};
function enQuery(data) {
const ret = [];
for (let d in data) {
ret.push(encodeURIComponent(d) + "=" + encodeURIComponent(data[d]));
}
return ret.join("&");
}
function password_gen() {
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz";
var string_length = 8;
var randomstring = '';
var charCount = 0;
var numCount = 0;
for (var i = 0; i < string_length; i++) {
// If random bit is 0, there are less than 3 digits already saved, and there are not already 5 characters saved, generate a numeric value.
if ((Math.floor(Math.random() * 2) == 0) && numCount < 3 || charCount >= 5) {
var rnum = Math.floor(Math.random() * 10);
randomstring += rnum;
numCount += 1;
} else {
// If any of the above criteria fail, go ahead and generate an alpha character from the chars string
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum, rnum + 1);
charCount += 1;
}
}
return randomstring;
}
async function validateRecaptcha(token) {
const url = 'https://www.google.com/recaptcha/api/siteverify';
const post_data = {
secret: recaptcha_config.secret_key,
response: token
};
const reqOpt = {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: enQuery(post_data)
};
const response = await fetch(url, reqOpt);
const results = await response.json();
console.log('validateRecaptcha ' + results.success);
return results.success;
}
async function get_ms_token() {
const url = 'https://login.microsoftonline.com/' + ms_config.tenant_id + '/oauth2/v2.0/token';
const scope = 'https://graph.microsoft.com/.default';
const post_data = {
'grant_type': 'client_credentials',
'client_id': ms_config.client_id,
'client_secret': ms_config.client_secret,
'scope': scope
};
const reqOpt = {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: enQuery(post_data)
};
const response = await fetch(url, reqOpt);
const results = await response.json();
console.log(results);
return results.access_token;
}
async function assignLicense(userEmail, access_token, requestBody) {
const url = 'https://graph.microsoft.com/v1.0/users/' + userEmail + '/assignLicense';
var skuId = '';
for (var subscription of ms_config.subscriptions) {
if (subscription.name == requestBody.subscription) {
skuId = subscription.skuId;
break;
}
}
const post_data = {
"addLicenses": [{
"disabledPlans": [],
"skuId": skuId
}],
"removeLicenses": []
};
const reqOpt = {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + access_token,
'Content-Type': 'application/json'
},
body: JSON.stringify(post_data)
};
const response = await fetch(url, reqOpt);
const results = await response.json();
console.log('assignLicense ' + results);
if (results.error == undefined) {
return true;
} else {
return false;
}
}
async function createUser(requestBody, access_token) {
const url = 'https://graph.microsoft.com/v1.0/users';
const password = password_gen();
const userEmail = requestBody.username + '@' + requestBody.domain
const post_data = {
"accountEnabled": true,
"displayName": requestBody.displayname,
"mailNickname": requestBody.username,
"passwordPolicies": "DisablePasswordExpiration, DisableStrongPassword",
"passwordProfile": {
"password": password,
"forceChangePasswordNextSignIn": true
},
"userPrincipalName": userEmail,
"usageLocation": "CN"
};
console.log('createUser data ' + post_data);
const reqOpt = {
method: 'POST',
headers: {
'content-type': 'application/json',
'Authorization': 'Bearer ' + access_token
},
body: JSON.stringify(post_data)
};
const response = await fetch(url, reqOpt);
const results = await response.json();
console.log(results);
if (results.error != undefined) {
if (results.error.message == 'Another object with the same value for property userPrincipalName already exists.') {
return {
stat: 'username exists'
};
}
return results;
}
if (await assignLicense(userEmail, access_token, requestBody)) {
const account = {
stat: 'success',
email: userEmail,
password: password
};
return account;
} else {
const account = {
stat: JSON.stringify(assign_results)
};
return account;
}
}
async function validateCode(code) {
const response = await fetch(site_config.code_api_link, {
method: 'POST',
body: JSON.stringify({
'action': 'checkCode',
'code': code
})
});
return await response.json();
}
async function deleteCode(code) {
const response = await fetch(site_config.code_api_link, {
method: 'POST',
body: JSON.stringify({
'action': 'delCode',
'code': code
})
});
return await response.text();
}
function myInterpolate(params, text) {
for (key of Object.keys(params)) {
text = text.replace('${' + key + '}', params[key])
}
return text;
}
async function handleRequest(request) {
var requestUrl = new URL(request.url);
var requestPath = requestUrl.pathname;
switch (requestPath) {
case '/mscreate':
if (request.method == 'POST') {
const requestBody = await request.json();
if (!await validateRecaptcha(requestBody.grecaptcha_token)) {
return new Response(JSON.stringify({
stat: 'wrong recaptcha'
}));
}
if (await validateCode(requestBody.code)) {
const ms_token = await get_ms_token();
const account = await createUser(requestBody, ms_token);
if (account.stat == 'success') {
await deleteCode(requestBody.code);
}
return new Response(JSON.stringify(account));
} else {
return new Response(JSON.stringify({
'stat': 'invalid code'
}));
}
}
break;
default:
var resposne = await fetch('https://cdn.jsdelivr.net/gh/zayabighead/msautocreate@master/worker.html');
var html = await resposne.text();
var html_subscriptions = '';
for (var subscription of ms_config.subscriptions) {
html_subscriptions += `<option value="${subscription.name}">${subscription.name}</option>`;
}
var html_domains = '';
for (var domain of ms_config.domains) {
html_domains += `<option value="${domain}">@ ${domain}</option>`;
}
return new Response(myInterpolate({
notice: site_config.notice,
line1: site_config.line1,
line2: site_config.line2,
subscriptions: html_subscriptions,
domains: html_domains,
code_store_link: site_config.code_store_link,
recaptcha_sitekey: recaptcha_config.site_key
}, html), {
status: 200,
headers: {
"Content-Type": "text/html; charset=utf-8"
}
});
}
}
addEventListener('fetch', event => {
return event.respondWith(handleRequest(event.request))
})