-
Notifications
You must be signed in to change notification settings - Fork 34
/
office_user.py
137 lines (118 loc) · 3.82 KB
/
office_user.py
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
import json
import secrets
import requests
from time import time
class OfficeUser:
def __init__(self, client_id: str, tenant_id: str, client_secret: str):
self._client_id = client_id
self._client_secret = client_secret
self._tenant_id = tenant_id
self._token = None
self._token_expires = None
@staticmethod
def _password_gen():
return secrets.token_urlsafe(8)
def _refresh_token(self):
if self._token is None or \
time() > self._token_expires:
self._get_token()
def _get_token(self):
r = requests.post(
url=f'https://login.microsoftonline.com/{self._tenant_id}/oauth2/v2.0/token',
headers={
'Content-Type': 'application/x-www-form-urlencoded'
},
data={
'grant_type': 'client_credentials',
'client_id': self._client_id,
'client_secret': self._client_secret,
'scope': 'https://graph.microsoft.com/.default'
}
)
data = r.json()
if r.status_code != 200 or \
'error' in data:
raise Exception(json.dumps(data))
self._token = data['access_token']
self._token_expires = int(data['expires_in']) + int(time())
def _assign_license(self, email: str, sku_id: str):
self._refresh_token()
r = requests.post(
url=f'https://graph.microsoft.com/v1.0/users/{email}/assignLicense',
headers={
'Authorization': f'Bearer {self._token}',
'Content-Type': 'application/json'
},
json={
'addLicenses': [{
'disabledPlans': [],
'skuId': sku_id
}],
'removeLicenses': []
}
)
data = r.json()
if r.status_code != 200 or \
'error' in data:
raise Exception(json.dumps(data))
def _create_user(
self,
display_name: str,
username: str,
password: str,
domain: str,
location: str = 'CN'
):
self._refresh_token()
r = requests.post(
url='https://graph.microsoft.com/v1.0/users',
headers={
'Authorization': f'Bearer {self._token}',
'Content-Type': 'application/json'
},
json={
'accountEnabled': True,
'displayName': display_name,
'mailNickname': username,
'passwordPolicies': 'DisablePasswordExpiration, DisableStrongPassword',
'passwordProfile': {
'password': password,
'forceChangePasswordNextSignIn': True
},
'userPrincipalName': username + domain,
'usageLocation': location
}
)
data = r.json()
if r.status_code != 201 or \
'error' in data:
raise Exception(json.dumps(data))
def create_account(
self,
username: str,
domain: str,
sku_id: str,
display_name: str = None,
password: str = None,
location: str = 'CN'
):
if display_name is None:
display_name = username
if password is None:
password = self._password_gen()
email = username + domain
self._create_user(
display_name=display_name,
username=username,
password=password,
domain=domain,
location=location
)
self._assign_license(
email=email,
sku_id=sku_id
)
return {
'email': email,
'password': password
}