A Library for Portier with asyncio
Originally published at https://www.viktorroytman.com/blog/2017/02/15/a-library -for-portier-with-asyncio/
In a previous post, I said:
Down the line, there will be client-side Portier libraries (or, at least, other demo implementations) for various languages. Until then, you’ll need to do some of the heavy lifting yourself.
Well, I decided to self-fulfill this prophecy. Introducing… the asyncio-portier Python library!
Usage
The asyncio-portier library works with Python 3.5 and up.
There’s a functioning demo included with the library which you can use as a template. I’ll go into more detail about it here.
If you’ve read the previous post, this should seem pretty familiar. There are a few important differences though; I’ll point them out along the way.
The login endpoint
Assuming that
cache
is a Redis connection object as from redis-pyself.audience
is the URL of your application (such ashttps://www.example.com
)broker_url
is the URL of the Portier Broker,https://broker.portier.io
The primary difference here is in what we set in the Redis cache. We prepend
the string 'portier:nonce:'
to the nonce value used for the key, and we set
the value to the next
URL query parameter. With this, if the user visits a
page that requires authentication, the application can redirect them directly
to that page after they have logged in.
The verify endpoint
Assuming that
- You have a template for an error page named
'error.html'
The get_verified_email
function now takes a number of explicit parameters
instead of reading from a global SETTINGS
object. It can raise ValueError
instead of RuntimeError
. It also now returns a next_page
in addition to an
email
which, as mentioned before, allows the application to redirect the user
directly to the requested page.
Wrapping up
This library should make things a little bit easier for anyone using Portier and asyncio. Please let me know if you run into any issues!