Asyncio wrapper for verify_token method in google-auth library for backend usage
Project description
aiohttp_google_auth_backend
The Google Authentication Library for python, google-auth, provides verify_token (and verify_oauth2_token) methods, which can be used by backend servers to verify the token provided by the web/mobile application and return decoded profile fields. However, python google-auth does not yet provide the support for asyncio.
The aiohttp_google_auth_backend library provides async wrapper for these methods.
How to use it
aiohttp_google_auth_backend package provides JSAioGoogleTokenVerifier class to asynchronously handle the token verification.
The library uses the asynchronous task to fetch and cache the GOOGLE OAUTH2 Certificates in the background using aiohttp Client API.
- Create an instance of JSAioGoogleTokenVerifier, along with the aiohttp web application instance, during the startup.
- Register on_startup method of the instance with on_startup of web application to fetch the certificates for first rime and then start the background thread to re-fetch the certificates.
- Register on_cleanup method of the instance with on_cleanup of web application to cancel the background thread when the process is being stopped.
- Constructor for the JSAioGoogleTokenVerifier class provides following parameters to customize re-fetching of certificates.
- By default, library uses the "Expires" header to identify when the certificates need to be re-fetched.
- The ok_renew_interval parameter can be used to specify interval to re-fetch certificates (e.g. every hour).
- If the library fails to fetch the certificates, it will repeatedly try to re-fetch certificates until successful.
- Library starts with initial delay of min_error_renew_interval (default: 1 second) and exponentially backoff the interval for each sub-sequent fetch till the delay reaches max_error_renew_interval.
- Token fields to be returned are identified by parameter profile_fields (default: email)
- By default, library uses the "Expires" header to identify when the certificates need to be re-fetched.
Following code sample assume that token to be verified is passed as parameter idtoken.
from aiohttp import web
from aiohttp_google_auth_backend import JSAioGoogleTokenVerifier
SAMPLE_GOOGLE_CLIENT_ID = "YOUR GOOGLE APPID"
async def handleLogin(request):
data = await request.json()
status, res = await request.app['verifyGoogleToken'].verify_token(data["idtoken"], SAMPLE_GOOGLE_CLIENT_ID)
if status == 200:
return web.json_response(res, status=status)
else:
return web.json_response(dict( error=str(res['error'])), status=status)
async def app_startup(app):
app['JSAioGoogleTokenVerifier'] = JSAioGoogleTokenVerifier()
await app['JSAioGoogleTokenVerifier'].on_startup()
async def app_cleanup(app):
await app['JSAioGoogleTokenVerifier'].on_cleanup()
def app_run():
app = web.Application()
app.on_startup.append(app_startup)
app.on_cleanup.append(app_cleanup)
app.add_routes([web.post('/login', handleLogin)])
web.run_app(app)
if __name__ == '__main__':
app_run()
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
File details
Details for the file aiohttp_google_auth_backend-0.5.1.tar.gz
.
File metadata
- Download URL: aiohttp_google_auth_backend-0.5.1.tar.gz
- Upload date:
- Size: 4.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 358ef6113e31a013693363bf51773ec8b73fbddb5b2b4b76d512b95638fbd4f3 |
|
MD5 | d890f3ec566bdcc880edf77966d929b5 |
|
BLAKE2b-256 | 52d63631b026c5f7b2293a763d4f5b94461eed53e7008b8e883a2dc0bbc58d64 |
File details
Details for the file aiohttp_google_auth_backend-0.5.1-py3-none-any.whl
.
File metadata
- Download URL: aiohttp_google_auth_backend-0.5.1-py3-none-any.whl
- Upload date:
- Size: 16.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1996e04ed68df57e6170b942b291bd1e9e01a68c6d8cec72898c796e2686abba |
|
MD5 | 6b9fedd3460a839544e35bf35d8e3ddc |
|
BLAKE2b-256 | 0ddfbf72ce9a8bcbf318a7b2b1dc19cfdebed48d4501a3bb43815197d4268fc5 |