-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
73 lines (62 loc) · 2.6 KB
/
utils.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
import requests
import datetime
import requests_cache
import config
requests_cache.install_cache(backend="memory",allowable_methods=('GET', 'POST' ))
authExpiry = datetime.datetime.now()
authToken = ""
allusers=[]
def getUsers(url="https://graph.microsoft.com/beta/users/"):
global allusers
token = getAuth()
response = requests.get(url, headers={"Authorization": "Bearer " + token}).json()
allusers.extend(response["value"])
if '@odata.nextLink' in response:
getUsers(response['@odata.nextLink'])
def getAuth():
global authExpiry, authToken
expDelta = authExpiry - datetime.datetime.now()
if expDelta.total_seconds() < 0: # token expired or not yet generated
url = f"https://login.microsoftonline.com/{config.domain}/oauth2/v2.0/token"
payload = f"client_id={config.clientid}&scope=https%3A//graph.microsoft.com/.default%0A&client_secret={config.secret}&grant_type=client_credentials"
response = requests.post(url, headers={"Content-Type": "application/x-www-form-urlencoded"}, data=payload,).json()
authExpiry = datetime.datetime.now() + datetime.timedelta(seconds=int(response["expires_in"]))
authToken = response["access_token"]
return authToken
def getFolderName(userID, folderID):
token = getAuth()
url = f"https://graph.microsoft.com/beta/users/{userID}/mailFolders/{folderID}"
try:
response = requests.get(url, headers={"Authorization": "Bearer " + token}).json()["displayName"]
except KeyError:
response = "Folder does not exist"
return response
def getObjName(objID):
token = getAuth()
url = "https://graph.microsoft.com/v1.0/directoryObjects/getByIds"
payload={'ids':[objID]}
response=requests.post(url,json=payload,headers={"Authorization": "Bearer " + token})
return response.json()['value'][0]
class rulePrinter:
def __init__(self,rule,user):
self.rule=rule
self.user=user
self.output=""
def outputObj(self):
self.formatObj(self.rule)
return self.output
def formatObj(self,obj):
if isinstance(obj, str):
if len(obj)==120: # Assume folder ID
self.output += getFolderName(self.user, obj) + "\n"
else:
self.output += obj + "\n"
if isinstance(obj, bool) or isinstance(obj, int):
self.output += str(obj) + "\n"
if isinstance(obj, dict):
for k, v in obj.items():
self.output += k + " : "
self.formatObj(v)
if isinstance(obj, list):
for i in obj:
self.formatObj(i)