Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat: add baseline class to get users' information from azure
  • Loading branch information
Angeluz-07 committed Jun 2, 2020
commit 8a1e36df3dc9e572cbb0c0e0a74201c8163d2032
7 changes: 7 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ export DATABASE_MASTER_KEY=<db_master_key>
# export COSMOS_DATABASE_URI=AccountEndpoint=<ACCOUNT_URI>;AccountKey=<ACCOUNT_KEY>
## Also specify the database name
export DATABASE_NAME=<db_name>

## For Azure Users interaction
export MSAL_AUTHORITY=
export MSAL_CLIENT_ID=
export MSAL_SCOPE=
export MSAL_SECRET=
export MSAL_ENDPOINT=
53 changes: 53 additions & 0 deletions utils/azure_users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import msal
import os
import requests


class MSALConfig:
MSAL_CLIENT_ID = os.environ.get('MSAL_CLIENT_ID')
MSAL_AUTHORITY = os.environ.get('MSAL_AUTHORITY')
MSAL_SECRET = os.environ.get('MSAL_SECRET')
MSAL_SCOPE = os.environ.get('MSAL_SCOPE')
MSAL_ENDPOINT = os.environ.get('MSAL_ENDPOINT')
"""
TODO : Add validation to ensure variables are set
"""


class AzureUsers:
def __init__(self, config=MSALConfig):
self.client = msal.ConfidentialClientApplication(
config.MSAL_CLIENT_ID,
authority=config.MSAL_AUTHORITY,
client_credential=config.MSAL_SECRET,
)
self.config = config
self.set_token()

def set_token(self):
response = self.client.acquire_token_for_client(
scopes=self.config.MSAL_SCOPE
)
if "access_token" in response:
# Call a protected API with the access token.
# print(response["access_token"])
self.access_token = response['access_token']
else:
print(response.get("error"))
print(response.get("error_description"))
print(
response.get("correlation_id")
) # You might need this when reporting a bug

def get_user_info_by_id(self, id):
endpoint = f"{self.config.MSAL_ENDPOINT}/users/{id}?api-version=1.6&$select=displayName,otherMails"
print(endpoint)
http_headers = {
'Authorization': f'Bearer {self.access_token}',
'Accept': 'application/json',
'Content-Type': 'application/json',
}
data = requests.get(
endpoint, headers=http_headers, stream=False
).json()
return data