Python
Integrating with sgID in a Python application
pip install sgid-client
If you are using one of the Python frameworks below, you may refer to our in-depth guide:
If not, read on for a framework-agnostic Quick Start in the next section.
If you have not already obtained your client credentials via registration, please register your client before proceeding.
sgid_client = SgidClient(
client_id=os.getenv("SGID_CLIENT_ID"),
client_secret=os.getenv("SGID_CLIENT_SECRET"),
private_key=os.getenv("SGID_PRIVATE_KEY"),
redirect_uri=os.getenv("SGID_REDIRECT_URI"),
)
Load your sgID credentials in a secure way using environment variables instead of hard-coding them into your app.
You should generate a new pair for each authorization request, and store the code verifier somewhere you can retrieve it when your user returns from logging into sgID (e.g. in the user's session).
from sgid_client import generate_pkce_pair
code_verifier, code_challenge = generate_pkce_pair()
url, nonce = sgid_client.authorization_url(
code_challenge=pkce_pair["code_challenge"]
)
You should store the nonce somewhere you can retrieve it when your user returns from logging into sgID, likely in the same place that you store the code verifier.
After your user logs in to sgID, they will be redirected to your redirect URI with the authorization code (
code
) in the query parameters. At this point, you should retrieve the code verifier and nonce from where they were stored in steps 2 and 3 respectively.sub, access_token = sgid_client.callback(
code=code, code_verifier=code_verifier, nonce=nonce
)
The "sub" refers to your user's unique sgID identifier, and is used together with the access token in the next step to retrieve the user's data.
The final step is to retrieve the data, which will contain the scopes requested in step 3.
sub, data = sgid_client.userinfo(sub=sub, access_token=access_token)
Last modified 1d ago