-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenectecSecurityCenter.py
More file actions
326 lines (279 loc) · 11.4 KB
/
GenectecSecurityCenter.py
File metadata and controls
326 lines (279 loc) · 11.4 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
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
from datetime import datetime
import requests
from urllib.parse import unquote
class GenectecSecurityCenter():
def __init__(self, hostname: str, cookies: dict, verifySsl=True):
"""[summary]
Args:
hostname (str): Hostname of the web server
cookies (dict): Cookies
verifySsl (bool, optional): [description]. Defaults to True.
"""
self.hostname = hostname
self.cookies = cookies
self.baseUrl = "https://{}/securitycenter".format(hostname)
self.verifySsl = verifySsl
headers = {
'Host': self.hostname,
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json;charset=utf-8',
'X-XSRF-TOKEN': unquote(self.cookies['XSRF-TOKEN']),
'Cookie': 'webclient={};XSRF-TOKEN={}'.format(self.cookies['webclient'], self.cookies['XSRF-TOKEN'])
}
# Create session for API calls with default
# headers and SSL verification
self.session = requests.Session()
self.session.headers.update(headers)
self.session.verify = self.verifySsl
# Card management
def getCardUnassigned(self, Assigned=False, FederatedState=2, Name='', Page=1, PageSize=25):
"""Return all unassigned cards
Args:
Assigned (bool, optional): [description]. Defaults to False.
FederatedState (int, optional): [description]. Defaults to 2.
Name (str, optional): [description]. Defaults to ''.
Page (int, optional): [description]. Defaults to 1.
PageSize (int, optional): [description]. Defaults to 25.
Returns:
[type]: [description]
"""
url = self.baseUrl + '/Credentials/Entities'
query = {
"Assigned": Assigned,
"FederatedState": FederatedState,
"Name": Name,
"Page": Page,
"PageSize": PageSize
}
response = self.session.get(url=url, params=query)
if response.status_code == 200:
return response.json()
else:
return response.status_code
def getCards(self, Name='', NameOrder=0, Page=1, Pagesize=25):
""" Search for cards
Args:
Name (str, optional): [description]. Defaults to ''.
NameOrder (int, optional): [description]. Defaults to 0.
Page (int, optional): [description]. Defaults to 1.
Pagesize (int, optional): [description]. Defaults to 25.
Returns:
[type]: [description]
"""
url = self.baseUrl + '/Credentials'
query = {
"Name": Name,
"NameOrder": NameOrder,
"Page": Page,
"Pagesize": Pagesize
}
response = self.session.get(url, params=query)
if response.status_code == 200:
return response.json()
else:
return response.status_code
# Card holder management
def getCardHolder(self, guid: str):
"""[summary]
Args:
guid (str): guid of the card holder
Returns:
[type]: [description]
"""
url = self.baseUrl + '/Cardholders/{}'.format(guid)
response = self.session.get(url)
if response.status_code == 200:
return response.json()
else:
return response.status_code
def deleteCardHolder(self, guid: str):
"""Delete a card holder
Args:
guid (str): guid of the card holder
Returns:
[type]: [description]
"""
url = "{}/Cardholders/{}".format(self.baseUrl, guid)
response = self.session.delete(url)
if response.status_code == 200:
return response.json()
else:
return response.status_code
def searchCardHolder(
self,
FullName='',
IncludeAccessRules=False,
IncludeCardholderGroups=True,
IncludeCredentials=True,
IncludePartitions=False,
LastNameOrder=0,
MobilePhoneNumber='',
NameOrder=0,
Page=1,
Pagesize=25,
FirstNameOrder=0
):
"""Search for card holder
Args:
FullName (str, optional): [description]. Defaults to ''.
IncludeAccessRules (bool, optional): [description]. Defaults to False.
IncludeCardholderGroups (bool, optional): [description]. Defaults to True.
IncludeCredentials (bool, optional): [description]. Defaults to True.
IncludePartitions (bool, optional): [description]. Defaults to False.
LastNameOrder (int, optional): [description]. Defaults to 0.
MobilePhoneNumber (str, optional): [description]. Defaults to ''.
NameOrder (int, optional): [description]. Defaults to 0.
Page (int, optional): [description]. Defaults to 1.
Pagesize (int, optional): [description]. Defaults to 25.
FirstNameOrder (int, optional): [description]. Defaults to 0.
Returns:
[type]: [description]
"""
url = self.baseUrl + '/Cardholders'
query = {
'FullName': FullName,
'IncludeAccessRules': IncludeAccessRules,
'IncludeCardholderGroups': IncludeCardholderGroups,
'IncludeCredentials': IncludeCredentials,
'IncludePartitions': IncludePartitions,
'LastNameOrder': LastNameOrder,
'MobilePhoneNumber': MobilePhoneNumber,
'NameOrder': NameOrder,
'Page': Page,
'Pagesize': Pagesize,
'FirstNameOrder': FirstNameOrder
}
response = self.session.get(url, params=query)
if response.status_code == 200:
return response.json()
else:
return response.status_code
def setCardHolderCard(self, cardHolder: dict, card: dict):
"""Assign card to a card holder
Args:
cardHolder (dict): [description]
card (dict): [description]
Returns:
[type]: [description]
"""
url = '{}/Cardholders/{}'.format(self.baseUrl, cardHolder['id'])
cardHolder['credentials'] = {card['id']: card['name']}
response = self.session.put(url, json=cardHolder)
if response.status_code == 200:
return response.json
else:
return response.status_code
# Card holder group management
def searchGroups(self, Name='', FederatedState=2, Page=1, PageSize=25):
"""Search card holder groups
Args:
Name (str, optional): [description]. Defaults to ''.
FederatedState (int, optional): [description]. Defaults to 2.
Page (int, optional): [description]. Defaults to 1.
PageSize (int, optional): [description]. Defaults to 25.
Returns:
[type]: [description]
"""
url = '{}/CardholderGroups/Entities'.format(self.baseUrl)
query = {
Name: Name,
FederatedState: FederatedState,
Page: Page,
PageSize: PageSize
}
response = self.session.get(url, params=query)
if response.status_code == 200:
return response.json()
else:
return response.status_code
def addCardHolder(
self,
firstName: str,
lastName: str,
partitions: dict,
photo='',
emailAddress='',
cardholderGroups={},
useExtendedGrantTime=False,
canEscort=False,
antipassbackExemption=False,
credentials={},
customFields=[],
accessRules={},
accessPermissionLevel=7,
description='',
name='',
inheritAccessPermissionLevelFromGroup=True,
antipassbackExemptionIsInherited=True,
privileges=[],
expirationDate=None,
expirationDuration=1,
activationDate=datetime.utcnow().isoformat()[:-3] + 'Z',
entityType=7,
accessStatus=1,
activationMode=0,
expirationMode=1
):
"""Create a card holder
Args:
firstName (str): [description]
lastName (str): [description]
photo (str, optional): [description]. Defaults to ''.
emailAddress (str, optional): [description]. Defaults to ''.
cardholderGroups (dict, optional): [description]. Defaults to {}.
useExtendedGrantTime (bool, optional): [description]. Defaults to False.
canEscort (bool, optional): [description]. Defaults to False.
antipassbackExemption (bool, optional): [description]. Defaults to False.
credentials (dict, optional): [description]. Defaults to {}.
customFields (list, optional): [description]. Defaults to [].
accessRules (dict, optional): [description]. Defaults to {}.
accessPermissionLevel (int, optional): [description]. Defaults to 7.
partitions (dict, optional): [description]. Defaults to {}.
description (str, optional): [description]. Defaults to ''.
name (str, optional): [description]. Defaults to ''.
inheritAccessPermissionLevelFromGroup (bool, optional): [description]. Defaults to True.
antipassbackExemptionIsInherited (bool, optional): [description]. Defaults to True.
privileges (list, optional): [description]. Defaults to [].
expirationDate ([type], optional): [description]. Defaults to None.
expirationDuration (int, optional): [description]. Defaults to 1.
activationDate ([type], optional): [description]. Defaults to datetime.utcnow().isoformat()[:-3]+'Z'.
entityType (int, optional): [description]. Defaults to 7.
accessStatus (int, optional): [description]. Defaults to 1.
activationMode (int, optional): [description]. Defaults to 0.
expirationMode (int, optional): [description]. Defaults to 1.
Returns:
[type]: [description]
"""
url = '{}/Cardholders'.format(self.baseUrl)
payload = {
"firstName": firstName,
"lastName": lastName,
"photo": photo,
"emailAddress": emailAddress,
"cardholderGroups": cardholderGroups,
"useExtendedGrantTime": useExtendedGrantTime,
"canEscort": canEscort,
"antipassbackExemption": antipassbackExemption,
"credentials": credentials,
"customFields": customFields,
"accessRules": accessRules,
"accessPermissionLevel": accessPermissionLevel,
"partitions": partitions,
"description": description,
"name": name,
"inheritAccessPermissionLevelFromGroup": inheritAccessPermissionLevelFromGroup,
"antipassbackExemptionIsInherited": antipassbackExemptionIsInherited,
"privileges": privileges,
"expirationDate": expirationDate,
"expirationDuration": expirationDuration,
"activationDate": activationDate,
"entityType": entityType,
"accessStatus": accessStatus,
"activationMode": activationMode,
"expirationMode": expirationMode
}
response = self.session.post(url, json=payload)
if response.status_code == 200:
return response.json()
else:
return response.text