08 - Alarming Authentication
In this module we will define our own way of decoding and decoding tokens in an Authentication class
Authentication is pretty straightforward. We want to make sure we protect our resources from unauthorized users. This is so we know who and what is using our API. Every method that requires authentication needs what is called a Json Web Token. These are encrypted strings of data that contain the necessary details to verify a user stored in the database
For our authentication, we will be using only one class that contains functions to abstract the jwt
module, and a decorator that will act as a gatekeeper
Add the following code to your shared/authentication
file
Let's take a look at these functions one at a time
auth required
- This is the decorator. We will use this on the views to verify that if a token was passed, it's the correct token. This decorator will use the next method in the class
decode_token
- This static method is responsible for sending tokens to the jwt
module and also check if the token has expired or not. This method will return the data back to the decorator to finish authentication
generate_token
- This method will take in a user id and hash out a payload for the token. This is information about the token including expiration, issued at, and the subscriber. Then the method will return an encoded token or present an error if anything happened incorrectly
Last updated
Was this helpful?