Python SDK built for IAM
Project description
iam-python-sdk
IAM's SDK for Python will allow you to easily connect your application to the IAM authentication system without having to implement it from scratch.
iam-python-sdk is available on PyPI:
$ pip install iam
IAM SDK is simple to use. We will show you the steps below.
Step1. Init Config
Initialization requires 5 parameters, which are all str type:
| Name (in order) | Must | Description |
|---|---|---|
| endpoint | Yes | IAM Server Url, such as http://localhost:8000 |
| client_id | Yes | Application.client_id |
| client_secret | Yes | Application.client_secret |
| certificate | Yes | Same as IAM certificate |
| org_name | Yes | Organization name |
| application_name | Yes | Application name |
from iam import IAMSDK
certificate = b'''-----BEGIN CERTIFICATE-----
MIIE+TCCAuGgAwIBAgIDAeJAMA0GCSqGSIb3DQEBCwUAMDYxHTAbBgNVBAoTFENh
...
-----END CERTIFICATE-----'''
sdk = IAMSDK(
endpoint,
client_id,
client_secret,
certificate,
org_name,
application_name,
)
OR use async version
from iam import AsyncIAMSDK
certificate = b'''-----BEGIN CERTIFICATE-----
MIIE+TCCAuGgAwIBAgIDAeJAMA0GCSqGSIb3DQEBCwUAMDYxHTAbBgNVBAoTFENh
...
-----END CERTIFICATE-----'''
sdk = AsyncIAMSDK(
endpoint,
client_id,
client_secret,
certificate,
org_name,
application_name,
)
Step2. Authorize with the IAM server
At this point, we should use some ways to verify with the IAM server.
To start, we want you understand clearly the verification process of IAM.
The following paragraphs will mention your app that wants to use IAM as a means
of verification as APP, and IAM as IAM.
-
APPwill send a request toIAM.
SinceIAMis a UI-based OAuth provider, you cannot use request management service like Postman to send a URL with parameters and get back a JSON file. -
The simplest way to try it out is to type the URL in your browser (in which JavaScript can be executed to run the UI).
-
Type in the URL in your browser in this format:
endpoint/login/oauth/authorize?client_id=xxx&response_type=code&redirect_uri=xxx&scope=read&state=xxx
In this URL theendpointis your IAM's location, as mentioned in Step1; then thexxxneed to be filled out by yourself.
Hints:
-
redirect_uriis the URL that yourAPPis configured to listen to the response fromIAM. For example, if yourredirect_uriishttps://forum.casbin.com/callback, then IAM will send a request to this URL along with two parameterscodeandstate, which will be used in later steps for authentication. -
stateis usually your Application's name, you can find it under theApplicationstab inIAM, and the leftmostNamecolumn gives each application's name. -
Of course you want your
APPto be able to send the URL. For example you should have something like a button, and it carries this URL. So when you click the button, you should be redirected toIAMfor verification. For now you are typing it in the browser simply for testing.
Step3. Get token and parse
After IAM verification passed, it will be redirected to your application with code and state as said in Step2, like https://forum.casbin.com/callback?code=xxx&state=yyyy.
Your web application can get the code and call get_oauth_token(code=code), then parse out jwt token.
The general process is as follows:
token = sdk.get_oauth_token(code=code)
access_token = token.get("access_token")
decoded_msg = sdk.parse_jwt_token(access_token) # or sdk.parse_jwt_token(access_token, kwargs)
decoded_msg is the JSON data decoded from the access_token, which contains user info and other useful stuff.
Step4. Interact with the users
iam-python-sdk support basic user operations, like:
get_user(user_id: str), get one user by user name.get_users(), get all users.modify_user(method: str, user: User)/add_user(user: User)/update_user(user: User)/delete_user(user: User), write user to database.refresh_token_request(refresh_token: str, scope: str), refresh access tokenenforce(self, permission_model_name: str, sub: str, obj: str, act: str, v3: Optional[str], v4: Optional[str], v5: Optional[str]), check permission from modelbatch_enforce(self, permission_model_name: str, permission_rules: list[list[str]]), batch check permission from modelget_user_count(is_online: bool = None), get user count.
Resource Owner Password Credentials Grant
If your application doesn't have a frontend that redirects users to IAM and you have Password Credentials Grant enabled, then you may get access token like this:
token = sdk.get_oauth_token(username=username, password=password)
access_token = token.get("access_token")
decoded_msg = sdk.parse_jwt_token(access_token) # or sdk.parse_jwt_token(access_token, kwargs)
decoded_msg is the JSON data decoded from the access_token, which contains user info and other useful stuff.
Client Credentials Grant
You can also use Client Credentials Grant when your application does not have a frontend. It is important to note that the AccessToken obtained in this way differs from other in that it corresponds to the application rather than to the user.
token = sdk.get_oauth_token()
access_token = token.get("access_token")
decoded_msg = sdk.parse_jwt_token(access_token) # or sdk.parse_jwt_token(access_token, kwargs)
decoded_msg is the JSON data decoded from the access_token.
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
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 hanzo_iam-1.29.0.tar.gz.
File metadata
- Download URL: hanzo_iam-1.29.0.tar.gz
- Upload date:
- Size: 25.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5979db89b791be181c259d103822424f389be5d82bff677bee9fad3f213f2578
|
|
| MD5 |
438e01bc13cfe1d0a25be9851e514c03
|
|
| BLAKE2b-256 |
3e4b11d440a3a99e5b7967ae1a9d56f4bee7c9879f7e2a56a9a1398a3d931064
|
File details
Details for the file hanzo_iam-1.29.0-py2.py3-none-any.whl.
File metadata
- Download URL: hanzo_iam-1.29.0-py2.py3-none-any.whl
- Upload date:
- Size: 47.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22aba50d91d642843570fd73853783cc26bad7ac618c778d2df49e54bd43bed9
|
|
| MD5 |
de28d7945c06195794f32184aebcf707
|
|
| BLAKE2b-256 |
e126bc5dbd90e5fd0f2666646c2b4831cc4fef6867bbad8091da496851fe600c
|