MS SSO Helper for Python
Project description
Installation
Use pip:
pip install py-mssso
or
pip install git+https://github.com/shineum/py_mssso.git
Prerequisites
Create Azure App with MS Azure Protal "App registrations". These information from Azure App are required.
- Directory (tenant) ID (Overview)
- Application (client) ID (Overview)
- Client secret (Manage - Certificates & secrets)
- scopes (Manage - API permissions)
- redirect url (Manage - Authentication - Web Redirect URIs)
Getting Started
from py_mssso import MSSSOHelper
...
MSSSOHelper.add(
**{
"tenant_type": MS_SSO_TENANT_TYPE,
"tenant_id": MS_SSO_TENANT_ID,
"client_id": MS_SSO_CLIENT_ID,
"client_secret": MS_SSO_CLIENT_SECRET,
"scopes": MS_SSO_SCOPES,
"redirect_url": MS_SSO_REDIRECT_URL,
}
)
tenant_type
"SINGLE", "MULTI" or "PUBLIC" - default: "SINGLE"
scopes
["User.Read"]: Read user data only for login user
["User.Read.All"]: when you need to read other user's information (ex: manager of login user)
redirect_url
url must be set in Web Redirect URIs in Azure App
login url
django example
# view.py
from py_mssso import MSSSOHelper
from django.http import HttpResponseRedirect
...
# sso login
def sso_login(request):
if not request.session.session_key:
request.session.create()
msal_flow = MSSSOHelper.get().get_auth_code_flow()
request.session["msal_flow"] = msal_flow
request.session.save()
return HttpResponseRedirect(msal_flow.get("auth_uri"))
callback url
django example
# view.py
import requests
from django.contrib.auth import get_user_model
from django.contrib.auth import login
from django.http import HttpResponseRedirect
from py_mssso import MSSSOHelper
_LOGIN_FAIL_URL = "/"
_LOGIN_SUCCESS_URL = "/"
_MSGRAPH_BASE_URI = "https://graph.microsoft.com/v1.0"
_MSGRAPH_QUERY_SELECT_ITEMS_FOR_USER = ",".join(
[
"displayName",
"givenName",
"surname",
"mail",
"mobilePhone",
"officeLocation",
"userPrincipalName",
"jobTitle",
"department",
"companyName",
"onPremisesSamAccountName",
]
)
...
# django login helper
def _login(request, username):
try:
user = get_user_model().objects.get(username=username)
user.backend = "django.contrib.auth.backends.ModelBackend"
login(request, user)
except:
raise Exception(f"Login Failed - user not exist: [{username}]")
# sso callback
def sso_login_callback(request):
msal_flow = request.session.get("msal_flow")
try:
# get token
token = MSSSOHelper.get().get_token(
auth_code_flow=msal_flow,
auth_res=request.GET,
)
# set request header
headers = {"Authorization": f"Bearer {token}"}
# get user data with MS Graph API
res_user = requests.get(
f"{_MSGRAPH_BASE_URI}/me?$select={_MSGRAPH_QUERY_SELECT_ITEMS_FOR_USER}",
headers=headers,
)
# get manager data with MS Graph API (Optional, "User.Read.All" permission is required)
res_manager = requests.get(
f"{_MSGRAPH_BASE_URI}/me/manager?$select={_MSGRAPH_QUERY_SELECT_ITEMS_FOR_USER}",
headers=headers,
)
##############################################
# Run some processes with user / manager data
# ex) create (django) user if not exist
# or update profile if necessary
##############################################
# parse user object
ms_user_obj = res_user.json()
# set username - it could be "userPrincipalName", "onPremisesSamAccountName" or any other values depending on your settings
username = ms_user_obj.get("userPrincipalName")
if not username:
raise Exception("Invalid User")
# create or update django user
get_user_model().objects.update_or_create(
username=username,
defaults={
"first_name": ms_user_obj.get("givenName"),
"last_name": ms_user_obj.get("surname"),
"email": ms_user_obj.get("mail"),
"is_active": True,
},
)
# django login
_login(request, username)
except:
return HttpResponseRedirect(_LOGIN_FAIL_URL)
return HttpResponseRedirect(_LOGIN_SUCCESS_URL)
sample project
https://github.com/shineum/py_mssso_sample
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
py_mssso-0.0.2.tar.gz
(4.6 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py_mssso-0.0.2.tar.gz.
File metadata
- Download URL: py_mssso-0.0.2.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fca5caf358465e87171e0b6c5bdf24684c938be3f3a55779c5fadad2c575af72
|
|
| MD5 |
87848a84bcaff7aaef7a38693b605a4b
|
|
| BLAKE2b-256 |
2ab3882152ea531a2bd79de109206d17014046dde8061c2d4b3936d6b088353f
|
File details
Details for the file py_mssso-0.0.2-py3-none-any.whl.
File metadata
- Download URL: py_mssso-0.0.2-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
98ee668a665e9d110a5856c96eda13f6addb86a76c5aa932de00304aef4967b6
|
|
| MD5 |
b160917129f19f2d8f1f6ddfba586027
|
|
| BLAKE2b-256 |
0e027d9e2e12f51449124ad8887524d0809fa6085d44f56de0530981eb0a37c2
|