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
Metadata
Release files for py-mssso 0.0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| py_mssso-0.0.2.tar.gz | 4.6 kB | Without provenance | Details for py_mssso-0.0.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| py_mssso-0.0.2-py3-none-any.whl | Python 3 | none | any | Details for py_mssso-0.0.2-py3-none-any.whl |
Total release size: 10.0 kB
Release files / py_mssso-0.0.2.tar.gz
| Download URL | py_mssso-0.0.2.tar.gz |
|---|---|
| Size | 4.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fca5caf358465e87171e0b6c5bdf24684c938be3f3a55779c5fadad2c575af72
|
|
BLAKE2b-256 checksum How to use checksums |
2ab3882152ea531a2bd79de109206d17014046dde8061c2d4b3936d6b088353f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.2
|
Release files / py_mssso-0.0.2-py3-none-any.whl
| Download URL | py_mssso-0.0.2-py3-none-any.whl |
|---|---|
| Size | 5.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
98ee668a665e9d110a5856c96eda13f6addb86a76c5aa932de00304aef4967b6
|
|
BLAKE2b-256 checksum How to use checksums |
0e027d9e2e12f51449124ad8887524d0809fa6085d44f56de0530981eb0a37c2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.12.2
|
Security for py-mssso 0.0.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.