Python

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 Passage
import os

PASSAGE_APP_ID = os.environ.get("PASSAGE_APP_ID")

class AuthenticationMiddleware(object):
    def __init__(self, app):
        self.app = app

    def __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)
            return ret(environ, start_response)
        environ['user'] = user
        return 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.

psg = Passage(PASSAGE_APP_ID, authStrategy=Passage.HEADER_AUTH)

Authorizing a User

It is important to remember that psg.authenticateRequest() validates that a request is properly authenticated, but an additional authorization check is often required.

app = Flask(__name__)
app.wsgi_app = AuthenticationMiddleware(app.wsgi_app)

@app.route('/', methods=['GET', 'POST'])
def authenticatedRoute():
    passageID = environ['user']
    authorized = authorizationCheck(passageID)
    if authorized:
    # Successful authentication and authorization. Proceed ...

App Information

The Passage SDK provides a way to retrieve information about an app.

from passageidentity import Passage

psg = 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 Passage
import os

PASSAGE_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)

All fields available in a Passage User object are listed below:

Field

Type

id

string

email

string

phone

string

active

boolean

email_verified

boolean

created_at

datetime

last_login_at

datetime

webauthn

boolean

user_metadata

dictionary

webauthn_devices

array of strings (e.g. "Mac OS X")

recent_events

array of PassageEvents

User Device Management

The functionality currently available is:

  • List all devices for a user

  • Revoke a particular device from a user

from passageidentity import Passage
import os

PASSAGE_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():
    devices = psg.listUserDevices(g.user)
    print(devices)

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 Passage

psg = Passage(PASSAGE_APP_ID, PASSAGE_API_KEY)

# create a magic link
magicLinkAttributes={
    "email": "<[email protected]>", 
    "redirect_url": "/custom-path/1234"}
magicLink = psg.createMagicLink(magicLinkAttributes)
# use the 'url' parameter
print(magicLink.url)

Last updated

Change request #337: react native