Authenticate requests and manage Passage users with Python.
To use the Passage Python SDK, you'll need your Passage App ID. You can create a new Passage App in the console.
The Passage Python SDK is designed for Flask or Django apps and supports Python 3.6 and higher.
It can be installed from the command line: pip install passage-identity
Authenticating a Request
A Flask server can easily authenticate an HTTP request using the passageidentity package, available via pip. The example below shows a Flask middleware that can be used to authenticate users.
from passageidentity import Passageimport osPASSAGE_APP_ID = os.environ.get("PASSAGE_APP_ID")classAuthenticationMiddleware(object):def__init__(self,app): self.app = appdef__call__(self,environ,start_response): request =Request(environ) psg =Passage(PASSAGE_APP_ID)try: user = psg.authenticateRequest(request)except: ret =Response(u'Authorization failed', mimetype='text/plain', status=401)returnret(environ, start_response) environ['user']= userreturn self.app(environ, start_response)
By default, Passage looks for the user JWT from a cookie that is set by the Passage Element ( psg_auth_token ). If your application uses Authorization headers instead, you can pass the following option to Passage.
It is important to remember that psg.authenticateRequest() validates that a request is properly authenticated, but an additional authorization check is often required.
The Passage SDK provides a way to retrieve information about an app.
from passageidentity import Passagepsg =Passage(PASSAGE_APP_ID)passageAppInfo = psg.getApp()print(passageAppInfo)
User Management
In addition to authenticating requests, the Passage SDK also provides a way to securely manage your users. These functions require authentication using a Passage API key. API keys can be managed in the Passage Console.
Passage API Keys are sensitive! You should store them securely along with your other application secrets.
The functionality currently available on a user is:
Get a user's information (including any defined user metadata)
Activate or deactivate a user (a deactivated user will not be able to log in)
Update a user's information (email address or phone number)
Delete a user
Create a user
from passageidentity import Passageimport osPASSAGE_APPID = os.environ.get("PASSAGE_APPID")PASSAGE_API_KEY = os.environ.get("PASSAGE_API_KEY")psg =Passage(PASSAGE_APPID, PASSAGE_API_KEY)def exampleFlaskMiddleware(request): g.user = psg.authenticateRequest(request)@app.route('/home')def authenticatedEndpoint(): user = psg.getUser(g.user)print(user.email)
from passageidentity import Passageimport osPASSAGE_APP_ID = os.environ.get("PASSAGE_APP_ID")PASSAGE_API_KEY = os.environ.get("PASSAGE_API_KEY")psg =Passage(PASSAGE_APP_ID, PASSAGE_API_KEY)# get Passage UserID from database# ...# deactivate this userpsg.deactivateUser(user_id)# activate this userpsg.activateUser(user_id)
from passageidentity import Passageimport osPASSAGE_APP_ID = os.environ.get("PASSAGE_APP_ID")PASSAGE_API_KEY = os.environ.get("PASSAGE_API_KEY")psg =Passage(PASSAGE_APP_ID, PASSAGE_API_KEY)# get Passage UserID from database# ...#activate or deactivate this useruser = psg.updateUser(user_id, {"email": "[email protected]","phone": "+15005550006"})
from passageidentity import Passageimport osPASSAGE_APP_ID = os.environ.get("PASSAGE_APP_ID")PASSAGE_API_KEY = os.environ.get("PASSAGE_API_KEY")psg =Passage(PASSAGE_APP_ID, PASSAGE_API_KEY)# get Passage UserID from database# ...# delete userdeleted_user = psg.deleteUser(user_id)print(deleted_user)# true
from passageidentity import Passageimport osPASSAGE_APP_ID = os.environ.get("PASSAGE_APP_ID")PASSAGE_API_KEY = os.environ.get("PASSAGE_API_KEY")psg =Passage(PASSAGE_APP_ID, PASSAGE_API_KEY)# get Passage UserID from database# ...# create user via emailcreated_user1 = psg.createUser({"email": "[email protected]"})print(created_user1)# Passage UserObject# create user via phonecreated_user2 = psg.createUser({"phone": "+15005550007"})print(created_user2)# Passage UserObject
All fields available in a Passage User object are listed below:
The Python SDK can be used to generate custom magic links for users, that can be embedded into any content medium. To learn more, see our full guide on Embedded Magic Links.
from passageidentity import Passagepsg =Passage(PASSAGE_APP_ID, PASSAGE_API_KEY)# create a magic linkmagicLinkAttributes={"email":"<[email protected]>","redirect_url":"/custom-path/1234"}magicLink = psg.createMagicLink(magicLinkAttributes)# use the 'url' parameterprint(magicLink.url)