|
10 | 10 | class AzureTokenRevoker(Responder):
|
11 | 11 | def __init__(self):
|
12 | 12 | Responder.__init__(self)
|
13 |
| - self.client_id = self.get_params('config.client_id', None, 'Azure AD Application ID/Client ID Missing') |
14 |
| - self.client_secret = self.get_params('config.client_secret', None, 'Azure AD Registered Application Client Secret Missing') |
15 |
| - self.redirect_uri = self.get_params('config.redirect_uri', None, 'Set a redirect URI in Azure AD Registered Application. (ex. https://logon.microsoftonline.<tenant id>/oauth2/token)') |
| 13 | + self.client_id = self.get_param('config.client_id', None, 'Azure AD Application ID/Client ID Missing') |
| 14 | + self.client_secret = self.get_param('config.client_secret', None, 'Azure AD Registered Application Client Secret Missing') |
| 15 | + self.tenant_id = self.get_param('config.tenant_id', None, 'Azure AD Tenant ID Mising') |
16 | 16 | self.time = ''
|
17 | 17 | def run(self):
|
18 |
| - try: |
19 |
| - self.user = self.get_params('data.data', None, 'No UPN supplied to revoke credentials for') |
20 |
| - if not self.user: |
21 |
| - self.error("No user supplied") |
22 |
| - base_resource = "https://graph.microsoft.com" |
| 18 | + Responder.run(self) |
23 | 19 |
|
24 |
| - token_data = { |
25 |
| - "grant_type": "client_credentials", |
26 |
| - 'client_id': self.client_id, |
27 |
| - 'client_secret': self.client_secret, |
28 |
| - 'resource': 'https://graph.microsoft.com', |
29 |
| - 'scope': 'https://graph.microsoft.com' |
30 |
| - } |
| 20 | + if self.get_param('data.dataType') == 'mail': |
| 21 | + try: |
| 22 | + self.user = self.get_param('data.data', None, 'No UPN supplied to revoke credentials for') |
| 23 | + if not self.user: |
| 24 | + self.error("No user supplied") |
| 25 | + base_resource = "https://graph.microsoft.com" |
31 | 26 |
|
| 27 | + token_data = { |
| 28 | + "grant_type": "client_credentials", |
| 29 | + 'client_id': self.client_id, |
| 30 | + 'client_secret': self.client_secret, |
| 31 | + 'resource': 'https://graph.microsoft.com', |
| 32 | + 'scope': 'https://graph.microsoft.com' |
| 33 | + } |
32 | 34 |
|
33 |
| - #Authenticate to the graph api |
34 | 35 |
|
35 |
| - token_r = requests.post(self.redirect_uri, data=token_data) |
36 |
| - token = token_r.json().get('access_token') |
| 36 | + #Authenticate to the graph api |
37 | 37 |
|
38 |
| - if token_r.status_code != 200: |
39 |
| - self.error('Failure to obtain azure access token: {}'.format(token_r.content)) |
| 38 | + redirect_uri = "https://login.microsoftonline.com/{}/oauth2/token".format(self.tenant_id) |
| 39 | + token_r = requests.post(redirect_uri, data=token_data) |
| 40 | + token = token_r.json().get('access_token') |
40 | 41 |
|
41 |
| - # Set headers for future requests |
42 |
| - headers = { |
43 |
| - 'Authorization': 'Bearer {}'.format(token) |
44 |
| - } |
| 42 | + if token_r.status_code != 200: |
| 43 | + self.error('Failure to obtain azure access token: {}'.format(token_r.content)) |
45 | 44 |
|
46 |
| - base_url = 'https://graph.microsoft.com/v1.0/' |
47 |
| - |
48 |
| - r = requests.post(base_url + 'users/{}/revokeSignInSessions'.format(self.user), headers=headers) |
| 45 | + # Set headers for future requests |
| 46 | + headers = { |
| 47 | + 'Authorization': 'Bearer {}'.format(token) |
| 48 | + } |
| 49 | + |
| 50 | + base_url = 'https://graph.microsoft.com/v1.0/' |
| 51 | + |
| 52 | + r = requests.post(base_url + 'users/{}/revokeSignInSessions'.format(self.user), headers=headers) |
49 | 53 |
|
50 |
| - if r.status_code != 200: |
51 |
| - self.error('Failure to revoke access tokens of user {}: {}'.format(self.user, r.content)) |
| 54 | + if r.status_code != 200: |
| 55 | + self.error('Failure to revoke access tokens of user {}: {}'.format(self.user, r.content)) |
| 56 | + |
| 57 | + else: |
| 58 | + #record time of successful auth token revokation |
| 59 | + self.time = datetime.datetime.utcnow() |
52 | 60 |
|
53 |
| - else: |
54 |
| - #record time of successful auth token revokation |
55 |
| - self.time = datetime.datetime.utcnow() |
56 |
| - |
57 |
| - except Exception as ex: |
58 |
| - self.error(traceback.format_exc()) |
59 |
| - # Build report to return to Cortex |
60 |
| - full_report = {"message": "User {} authentication tokens successfully revoked at {}".format(self.user, self.time)} |
61 |
| - self.report(full_report) |
| 61 | + except Exception as ex: |
| 62 | + self.error(traceback.format_exc()) |
| 63 | + # Build report to return to Cortex |
| 64 | + full_report = {"message": "User {} authentication tokens successfully revoked at {}".format(self.user, self.time)} |
| 65 | + self.report(full_report) |
| 66 | + else: |
| 67 | + self.error('Incorrect dataType. "mail" expected.') |
62 | 68 |
|
63 | 69 |
|
64 | 70 | if __name__ == '__main__':
|
|
0 commit comments