forked from chenyang12315/msautocreate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
343 lines (303 loc) · 13 KB
/
index.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
const tenant_id = '';
const client_id = '';
const client_secret = '';
const skuId = '';
const domain = '@example.com';
var html = `
<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>微软全局子号自助开通</title>
<link rel="stylesheet" href="https://cdnjs.loli.net/ajax/libs/mdui/0.4.3/css/mdui.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fullPage.js/3.0.8/fullpage.min.css">
<link href="https://i.loli.net/2020/01/25/mHyKw936UxpEM1Y.png" rel="icon" type="image/png">
<style>
.intro {
background: url('https://i.loli.net/2020/01/25/KEScJXCBfAzaIjW.png') no-repeat;
background-size: cover
}
</style>
<style>
.create-wait {
display: none;
position: absolute;
z-index: 999;
width: 100%;
height: 100vh;
background-color: rgba(0, 0, 0, 0.5);
top: 0
}
.create-wait .mdui-valign {
width: 100%;
height: 100vh;
}
</style>
</head>
<body>
<a href="https://github.com/zayabighead/msautocreate"><img width="149" height="149" src="https://github.blog/wp-content/uploads/2008/12/forkme_right_darkblue_121621.png?resize=149%2C149" class="attachment-full size-full" alt="Fork me on GitHub" data-recalc-dims="1" style="position: absolute; top: 0;z-index: 999999;right: 0"></a>
<div id="fullpage">
<div class="section intro">
<div class="mdui-appbar mdui-appbar-fixed mdui-color-white">
<div class="mdui-toolbar">
<a href="javascript:;" class="mdui-typo-headline">微软全局子号自助开通</a>
<div class="mdui-toolbar-spacer"></div>
</div>
</div>
<div class="mdui-text-color-white-text" align="center" style="width: 70%;margin: auto">
<img src="https://i.loli.net/2020/01/25/LF2q4bBN3rCMcDt.png" style="width: 250px">
<h1 style="font-size: 40px;font-weight: 500">微软全局子号自助开通</h1>
<h1 style="font-size: 30px;font-weight: 500">此全局订阅为A1</h1>
<h1 style="font-size: 30px;font-weight: 500">(5TB Onedrive + 在线版Office)</h1>
<br><br><br><br>
<a href="#form"><button class="mdui-btn mdui-ripple mdui-color-red mdui-m-a-2" style="width: 140px;height: 50px">立即获取</button></a>
<a href="https://www.office.com/login"><button class="mdui-m-a-2 mdui-btn mdui-ripple mdui-color-white" style="width: 140px;height: 50px">登录</button></a>
</div>
</div>
<div class="section form">
<div class="create-wait">
<div class="mdui-valign">
<div class="mdui-spinner mdui-spinner-colorful mdui-center"></div>
</div>
</div>
<div class="mdui-container">
<div class="mdui-row">
<h2 style="font-size: 40px;font-weight: 500" id="aform">微软全局子号自助开通</h2>
<form id="aform">
<div class="mdui-textfield mdui-textfield-floating-label">
<label class="mdui-textfield-label">姓/Lastname *</label>
<input class="mdui-textfield-input" type="text" name="lastname" pattern="[A-z0-9]{1,10}" required>
<div class="mdui-textfield-error">英文/拼音</div>
</div>
<div class="mdui-textfield mdui-textfield-floating-label">
<label class="mdui-textfield-label">名/Firstname *</label>
<input class="mdui-textfield-input" type="text" name="firstname" pattern="[A-z0-9]{1,10}" required>
<div class="mdui-textfield-error">英文/拼音</div>
</div>
<div class="mdui-textfield mdui-textfield-floating-label">
<label class="mdui-textfield-label">邮箱前缀 *</label>
<input class="mdui-textfield-input" type="text" name="username" pattern="[A-z0-9]{1,50}" required>
<div class="mdui-textfield-error">@ 前面的字符,允许字母/数字</div>
</div>
<input type="submit" class="mdui-btn mdui-ripple mdui-color-yellow" value="提交">
<div style="margin-top: 20px; color: gray">* 提交后账户信息将显示在网页上,请勿刷新</div>
</form>
<div class="ainfo" style="display: none">
<h2 style="font-size: 30px;font-weight: 500">微软全局子号创建成功!</h2>
<label class="mdui-textfield-label">邮箱</label>
<input class="mdui-textfield-input cremail" type="text" value="">
<label class="mdui-textfield-label">初始密码</label>
<input class="mdui-textfield-input crpass" type="text" value="">
<a href="https://www.office.com/login" target="_blank"><button class="mdui-m-t-4 mdui-btn mdui-ripple mdui-color-red" style="width: 140px;height: 50px">前往登录</button></a>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdnjs.loli.net/ajax/libs/mdui/0.4.3/js/mdui.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fullPage.js/3.0.8/vendors/scrolloverflow.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/fullPage.js/3.0.8/fullpage.min.js"></script>
<script>
$(document).ready(function() {
$('#fullpage').fullpage({
anchors: ['intro', 'form'],
scrollOverflow: true,
verticalCentered: true
});
});
</script>
<script>
$('form').submit(function(e) {
e.preventDefault();
mdui.dialog({
title: '创建账号',
content: '点击确认后开始创建账号,请耐心等待,勿刷新网页',
buttons: [{
text: '取消'
}, {
text: '确认',
onClick: function(inst) {
$('.create-wait').show();
$.ajax({
type: "POST",
url: "/mscript",
data: JSON.stringify({
lastname: e.target['lastname'].value.trim(),
firstname: e.target['firstname'].value.trim(),
username: e.target['username'].value.trim()
}),
success: function(res) {
$('.create-wait').hide();
var response = JSON.parse(res);
if (response.stat == 'success') {
$('.cremail').val(response.email);
$('.crpass').val(response.password);
$('form').hide();
$('.mdui-row>h2').hide();
$('.ainfo').show();
} else if (response.stat == 'username exists') {
mdui.alert('创建失败,前缀已被占用');
} else {
JSON.stringify(response);
}
},
contentType: "application/json"
});
}
}]
});
});
</script>
</body>
</html>
`;
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 get_ms_token() {
const url = 'https://login.microsoftonline.com/' + tenant_id + '/oauth2/v2.0/token';
const scope = 'https://graph.microsoft.com/.default';
const post_data = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': 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(email, token) {
const url = 'https://graph.microsoft.com/v1.0/users/' + email + '/assignLicense';
const post_data = {
"addLicenses": [{
"disabledPlans": [],
"skuId": skuId
}],
"removeLicenses": []
};
const reqOpt = {
method: 'POST',
headers: {
'Authorization': 'Bearer ' + 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 post_data = {
"accountEnabled": true,
"displayName": requestBody.firstname + ' ' + requestBody.lastname,
"mailNickname": requestBody.username,
"passwordPolicies": "DisablePasswordExpiration, DisableStrongPassword",
"passwordProfile": {
"password": password,
"forceChangePasswordNextSignIn": true
},
"userPrincipalName": requestBody.username + domain,
"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;
}
const assign_results = await assignLicense(requestBody.username + domain, access_token);
if (assign_results) {
const account = {
stat: 'success',
email: requestBody.username + domain,
password: password
};
return account;
} else {
const account = {
stat: JSON.stringify(assign_results)
};
return account;
}
}
async function handleRequest(request) {
let url = new URL(request.url);
let path = url.pathname;
switch (path) {
case '/mscript':
const requestBody = await request.json();
const ms_token = await get_ms_token();
const account = await createUser(requestBody, ms_token);
return new Response(JSON.stringify(account));
break;
default:
return new Response(html, {
status: 200,
headers: {
"Content-Type": "text/html; charset=utf-8",
"Access-Control-Allow-Origin": "*"
}
});
}
}
addEventListener('fetch', event => {
return event.respondWith(handleRequest(event.request))
})