-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
322 lines (294 loc) · 9.25 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
const querystring = require('querystring');
const { Provider } = require('suqin');
const config = {
baseUrl: 'chinacloudapi.cn',
name: 'AAD',
};
module.exports = class AzureAD extends Provider {
/**
* 构造函数
* @param {Object} opts 更多配置项
*/
constructor(opts = { baseUrl: config.baseUrl }) {
super(opts);
if (typeof opts.tenlentId !== 'string') throw Error('Params Error: opts.tenlentId must be a String.');
if (typeof opts.clientId !== 'string') throw Error('Params Error: opts.clientId must be a String.');
if (typeof opts.userName !== 'string') throw Error('Params Error: opts.userName must be a String.');
if (typeof opts.userPassword !== 'string') throw Error('Params Error: opts.userPassword must be a String.');
// 在suqin体系中的代号/名称/key
this.name = opts.name || config.name;
// Root Host
this._baseUrl = opts.baseUrl || config.baseUrl;
// Auth API Host
this._authHost = `https://login.${this._baseUrl}`;
// Graph API Host
this._graphHost = `https://graph.${this._baseUrl}`;
// 租户ID
this._tenlentId = opts.tenlentId;
// 客户端ID
this._clientId = opts.clientId;
// Azure AD 管理员账号
this._userName = opts.userName;
// Azure AD 管理员密码
this._userPassword = opts.userPassword;
// 缓存token
this._token = {
value: null,
expires: null,
};
}
get token() {
const token = this._token;
if (!token.expires || token.expires < +new Date()) {
return this.getToken();
}
return token.value;
}
set token(val) {
this._token = val;
return this._token;
}
/**
* 获取Token
*/
async getToken() {
const authUrl = `${this._authHost}/${this._tenlentId}/oauth2/token?api-version=1.0`;
return this.fetch({
method: 'post',
url: authUrl,
data: querystring.stringify({
grant_type: 'password',
resource: this._graphHost,
client_id: this._clientId,
username: this._userName,
password: this._userPassword,
}),
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
}).then(res => {
const token = res.data;
this.token = {
value: token.access_token,
// 提前 5 分钟是失效 token
expires: (token.expires_on - 300) * 1000,
};
return token.access_token;
});
}
/**
* 请求参数构造器
* 将基础参数进行封装, 用于适配suqin.fetch()
* @param {Object} opts 基础参数
*/
async fetchConfGenerator(opts = {}) {
const token = await this.token;
return {
method: opts.method ? opts.method.toLowerCase() : 'get',
url: `${opts.url}?${querystring.stringify(Object.assign({
'api-version': '1.5',
}, opts.query))}`,
headers: Object.assign({
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
}, opts.headers),
data: opts.data,
};
}
/* eslint-disable class-methods-use-this */
get readAPIs() {
return {
/**
* 查询成员列表
* @param {Object} opts 其余参数
*/
async readMembers(opts = {}) {
return this.fetch(await this.fetchConfGenerator({
url: `${this._graphHost}/${this._tenlentId}/users`,
query: opts,
}));
},
/**
* 查询成员详情
* @param {String} principalName 成员主要名字 / 邮箱
* @param {Object} opts 其余参数
*/
async readMember(principalName, opts = {}) {
return this.fetch(await this.fetchConfGenerator({
url: `${this._graphHost}/${this._tenlentId}/users/${principalName}`,
query: opts,
}));
},
/**
* 查询群组列表
* @param {Object} opts 其余参数
*/
async readGroups(opts = {}) {
return this.fetch(await this.fetchConfGenerator({
url: `${this._graphHost}/${this._tenlentId}/groups`,
query: opts,
}));
},
/**
* 查询群组详情
* @param {Object} opts 其余参数
*/
async readGroup(id, opts = {}) {
return this.fetch(await this.fetchConfGenerator({
url: `${this._graphHost}/${this._tenlentId}/groups/${id}`,
query: opts,
}));
},
/**
* 查询群组成员
* @param {String} id 群组id
*/
async readGroupMembers(id, opts = {}) {
return this.fetch(await this.fetchConfGenerator({
url: `${this._graphHost}/${this._tenlentId}/groups/${id}/$links/members`,
query: opts,
}));
},
/**
* 查询指定用户所属部门
* Get a user's group and directory role memberships
* @param {String} userId 用户ID
*/
async readMemberBelong(userId, opts = {}) {
return this.fetch(await this.fetchConfGenerator({
url: `${this._graphHost}/${this._tenlentId}/users/${userId}/$links/memberOf`,
query: opts,
}));
},
/**
* 校验用户是否存在
* @param {String} name 用户主邮箱 principalName
* @param {String} pwd 用户密码
* @return {Bolean} true: 存在, false: 不存在
*/
async verifiyMember(name, pwd) {
const authUrl = `${this._authHost}/${this._tenlentId}/oauth2/token?api-version=1.0`;
return this.fetch({
method: 'post',
url: authUrl,
data: querystring.stringify({
grant_type: 'password',
resource: this._graphHost,
client_id: this._clientId,
username: name,
password: pwd,
}),
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
})
.then(res => !!res.data.access_token);
},
};
}
get writeAPIs() {
return {
/**
* 创建成员
* @param {Object} member 成员
* @param {Object} opts 其余参数
*/
async createMember(member, opts = {}) {
return this.fetch(await this.fetchConfGenerator({
method: 'post',
url: `${this._graphHost}/${this._tenlentId}/users`,
query: opts,
data: member,
}));
},
/**
* 删除成员
* @param {String} principalName 成员主要名字 / 邮箱
* @param {Object} opts 其余参数
*/
async deleteMember(principalName, opts = {}) {
return this.fetch(await this.fetchConfGenerator({
method: 'delete',
url: `${this._graphHost}/${this._tenlentId}/users/${principalName}`,
query: opts,
}));
},
/**
* 修改成员
* @param {String} principalName 成员主要名字 / 邮箱 / objectId
* @param {Object} member 成员
* @param {Object} opts 其余参数
*/
async updateMember(principalName, member, opts = {}) {
return this.fetch(await this.fetchConfGenerator({
method: 'patch',
url: `${this._graphHost}/${this._tenlentId}/users/${principalName}`,
query: opts,
data: member,
}));
},
/**
* 创建群组
* @param {Object} group 群组
* @param {Object} opts 其余参数
*/
async createGroup(group, opts = {}) {
return this.fetch(await this.fetchConfGenerator({
method: 'post',
url: `${this._graphHost}/${this._tenlentId}/groups`,
query: opts,
data: group,
}));
},
/**
* 删除群组
* @param {String} id 群组ID
* @param {Object} opts 其余参数
*/
async deleteGroup(id, opts = {}) {
return this.fetch(await this.fetchConfGenerator({
method: 'delete',
url: `${this._graphHost}/${this._tenlentId}/groups/${id}`,
query: opts,
}));
},
/**
* 修改群组
* @param {Object} id 群组ID
* @param {Object} group 群组
* @param {Object} opts 其余参数
*/
async updateGroup(id, group, opts = {}) {
return this.fetch(await this.fetchConfGenerator({
method: 'patch',
url: `${this._graphHost}/${this._tenlentId}/groups/${id}`,
query: opts,
data: group,
}));
},
/**
* 添加群组成员
* @param {String} groupId 群组id
* @param {String} memberId 成员id
*/
async addGroupMember(groupId, memberId, opts = {}) {
const data = { url: `${this._graphHost}/${this._tenlentId}/directoryObjects/${memberId}` };
return this.fetch(await this.fetchConfGenerator({
method: 'post',
url: `${this._graphHost}/${this._tenlentId}/groups/${groupId}/$links/members`,
query: opts,
data,
}));
},
/**
* 删除群组成员
* @param {String} groupId 群组id
* @param {String} memberId 成员id
*/
async deleteGroupMember(groupId, memberId, opts = {}) {
return this.fetch(await this.fetchConfGenerator({
method: 'delete',
url: `${this._graphHost}/${this._tenlentId}/groups/${groupId}/$links/members/${memberId}`,
query: opts,
}));
},
};
}
/* eslint-enable class-methods-use-this */
};