Search…
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 apps and supports Python 3.6 and higher.

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.
1
from passageidentity import Passage
2
import os
3
​
4
PASSAGE_APP_ID = os.environ.get("PASSAGE_APP_ID")
5
​
6
class AuthenticationMiddleware(object):
7
def __init__(self, app):
8
self.app = app
9
​
10
def __call__(self, environ, start_response):
11
request = Request(environ)
12
psg = Passage(PASSAGE_APP_ID)
13
try:
14
user = psg.authenticateRequest(request)
15
except:
16
ret = Response(u'Authorization failed', mimetype='text/plain', status=401)
17
return ret(environ, start_response)
18
environ['user'] = user
19
return self.app(environ, start_response)
Copied!
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.
1
psg = Passage(PASSAGE_APP_ID, authStrategy=Passage.HEADER_AUTH)
Copied!

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.
1
app = Flask(__name__)
2
app.wsgi_app = AuthenticationMiddleware(app.wsgi_app)
3
​
4
@app.route('/', methods=['GET', 'POST'])
5
def authenticatedRoute():
6
passageID = environ['user']
7
authorized = authorizationCheck(passageID)
8
if authorized:
9
# Successful authentication and authorization. Proceed ...
Copied!

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
  • 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)
Get User
Deactivate User
Update User
1
from passageidentity import Passage
2
import os
3
​
4
PASSAGE_APPID = os.environ.get("PASSAGE_APPID")
5
PASSAGE_API_KEY = os.environ.get("PASSAGE_API_KEY")
6
psg = Passage(PASSAGE_APPID, PASSAGE_API_KEY)
7
​
8
def exampleFlaskMiddleware(request):
9
g.user = psg.authenticateRequest(request)
10
​
11
@app.route('/home')
12
def authenticatedEndpoint():
13
user = psg.getUser(g.user)
14
print(user.email)
Copied!
1
from passageidentity import Passage
2
import os
3
​
4
PASSAGE_APP_ID = os.environ.get("PASSAGE_APP_ID")
5
PASSAGE_API_KEY = os.environ.get("PASSAGE_API_KEY")
6
psg = Passage(PASSAGE_APP_ID, PASSAGE_API_KEY)
7
​
8
# get Passage UserID from database
9
# ...
10
​
11
#activate or deactivate this user
12
psg.deactivateUser(user_id)
Copied!
1
from passageidentity import Passage
2
import os
3
​
4
PASSAGE_APP_ID = os.environ.get("PASSAGE_APP_ID")
5
PASSAGE_API_KEY = os.environ.get("PASSAGE_API_KEY")
6
psg = Passage(PASSAGE_APP_ID, PASSAGE_API_KEY)
7
​
8
# get Passage UserID from database
9
# ...
10
​
11
#activate or deactivate this user
12
user = psg.updateUser(user_id, {
13
"email": "[email protected]",
14
"phone": "+15005550006"
15
})
Copied!
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
webauthn_devices
array of string (e.g. "Mac OS X")
recent_events
array of PassageEvents
Last modified 28d ago