-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreat_user.py
More file actions
197 lines (161 loc) · 6.57 KB
/
Copy pathcreat_user.py
File metadata and controls
197 lines (161 loc) · 6.57 KB
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
import requests
import json
import pandas as pd
import configparser
import secrets
import string
import csv
# Read in config data
config = configparser.ConfigParser()
config.read('ms_graph.ini')
if 'CONNECTION' in config:
app_id = str(config.get('CONNECTION', 'APP_ID', fallback="There is no app id"))
client_secret = str(config.get('CONNECTION', 'CLIENT_SECRET', fallback="There is no client secret"))
tenant_id = str(config.get('CONNECTION', 'TENANT_ID', fallback="There is no tenant id"))
admin_mail = str(config.get('TEST_DATA', 'ADMIN_MAIL', fallback="There is no test admin mail"))
domain = str(config.get('TEST_DATA', 'DOMAIN', fallback="There is no test domain"))
invitor = str(config.get('TEST_DATA', 'INVITOR', fallback="There is no test invitor"))
csv_path = str(config.get('TEST_DATA', 'CSV_PATH', fallback="There is no path"))
else:
print('CONNECTION parameters are not found')
# Get token to sign in as application
token_url = 'https://login.microsoftonline.com/{}/oauth2/v2.0/token'.format(tenant_id)
token_data = {
'grant_type': 'client_credentials',
'client_id': app_id,
'client_secret': client_secret,
'scope':'https://graph.microsoft.com/.default',
}
token_r = requests.post(token_url, data=token_data)
token = token_r.json().get('access_token')
headers = {
'Authorization': 'Bearer {}'.format(token),
# needed for counting
'ConsistencyLevel' : 'eventual'
}
# Generate a secure password
def generatePassword():
possibleCharc = string.ascii_letters + string.digits + string.punctuation
# for a 20-character password
password = ''.join(secrets.choice(possibleCharc) for i in range(20))
return password
# Checks if a given name matches requierements and format it nicely
def checkName(name):
correctName = name.title()
return correctName
# Create a username
def createUserName(surname, givenname):
username = '{person_givenname}.{person_surname}'.format(person_givenname=givenname, person_surname=surname)
return username
# Creat UserPrincipalName
def create_UsP(surname, givenname, domain):
username = createUserName(surname, givenname)
lowerUsername = username.lower()
usp = '{uspPrefix}{domain}'.format(uspPrefix=lowerUsername, domain=domain)
return usp
# Check if username already exists
def uspExist(userPrincipalName):
# instead of using v1.0 in query you need to use beta to be able to count result
users_url = 'https://graph.microsoft.com/beta/users?$count=true&$filter=startswith(userPrincipalName, \'{}\' )'.format(userPrincipalName)
user_response_data = json.loads(requests.get(users_url, headers=headers).text)
# check if result is > 0
return user_response_data['@odata.count'] > 0
# Creat Azure AD User
def create_user(surname, givenname, domain, privateMail):
correctSur = checkName(surname)
correctGiven = checkName (givenname)
userPrincipalName = create_UsP(correctSur, correctGiven, domain)
if uspExist(userPrincipalName):
print("usp = {} already exists!".format(userPrincipalName))
else:
mailNickname = createUserName(correctSur, correctGiven)
createdPassword = generatePassword()
userDisplayName = "{person_given} {person_surname}".format(person_given=correctGiven, person_surname=correctSur)
user_information = {
"accountEnabled": True,
"surName" : correctSur,
"givenName" : correctGiven,
"displayName": userDisplayName,
"mailNickname": mailNickname,
"mail" : userPrincipalName,
"userPrincipalName": userPrincipalName,
"usageLocation": "DE",
"passwordProfile" : {
"forceChangePasswordNextSignIn": False,
"password": createdPassword
}
}
# Request to create user
graph_url = 'https://graph.microsoft.com/v1.0/users/'
create_result = requests.post(graph_url, headers=headers, json=user_information)
create_response = create_result.json()
print(create_response)
user_id = create_response['id']
print(user_id)
if assign_licence(user_id):
sendInvitationMail(privateMail, createdPassword, correctGiven, userPrincipalName)
else:
print("No Mail sended!")
# Assing licence to a user: It's important to set the usage location before
def assign_licence(user_id):
licence_information = {
"addLicenses": [
{
"disabledPlans": [],
"skuId": "4b585984-651b-448a-9e53-3b10f069cf7f"
}
],
"removeLicenses": []
}
licence_url = 'https://graph.microsoft.com/v1.0/users/{id}/assignLicense'.format(id=user_id)
licence_result = requests.post(licence_url, headers=headers, json=licence_information)
licence_response = licence_result.json()
print(licence_response)
return True
# The application sends an invitation mail from a specified account to the new account
def sendInvitationMail(privateMail, password, givenName, userPrincipalName):
htmlMail = """\
<html>
<body>
<p>Hallo {name},</p>
<p>wir dürfen dich herzlich bei uns willkommen heißen. Hier sind deine Zugangsdaten:</p>
<p> </p>
<p><strong>Benutzername</strong>: {username} </p>
<p><strong>Passwort</strong>: {userPwd} </p>
<p> </p>
<p>Du kannst dich hier anmelden: <a href="http://www.office.com">www.office.com</a></p>
<p> </p>
<p>Gruß,</p>
<p>Bereichsleitung IT</p>
</body>
</html>
""".format(name=givenName, username=userPrincipalName, userPwd=password)
mail = {
"message": {
"subject": "Testmail",
"body": {
"contentType": "html",
"content": htmlMail
},
"toRecipients": [
{
"emailAddress": {
"address": privateMail
}
}
],
},
"saveToSentItems": "false"
}
mailGraphUrl = 'https://graph.microsoft.com/v1.0/users/{}/sendMail'.format(invitor)
mailResult = requests.post(mailGraphUrl, headers=headers, json=mail)
print(mailResult)
# Test output
#create_user("test12", "Stefan", domain)
#uspExist('stefan.test02@M365x293953.onmicrosoft.com')
#print(uspExist('s'))
#print(uspExist('y'))
with open(csv_path, mode='r', encoding='utf-8-sig') as csv_file:
csv_reader = csv.DictReader(csv_file, delimiter=';')
for row in csv_reader:
create_user(row['surname'], row['givenname'], domain, row['privatemail'])