tide-jwt
Simple implementation of JWT Authorization Middleware for the tide web framework. This makes use of the jsonwebtoken crate for the encoding/decoding. It will only return Unauthorized in the case where an Authorization header is found and it is not valid. If no authorization header is found the middleware will continue to run. It is up to the implementation to make sure to check if the request is actually authenticated to prevent downstream middleware from running and to return the appropriate response.
Features
[x] Read "Authorization" header [x] Validate "Bearer" token with generic claims and jsonwebtoken [x] Add helper functions for encoding (from secret base64, chosen algorithm, claims) [x] Support Send + Sync + 'static, Serializable/Deserialize (serde) claims used for jsonwebtoken [] Possibly read jwt cookie if configured/present [] Support for non jwt (jose spec)
Examples
Implementation with the tide web framework is as simple as using the .with function to include the middleware. This functions as technically a Before middleware in that it reads from the Request before continuing the rest of the middleware. It will make use of the set_ext function to add the ability to get the <Claims> object with any other middleware.
use ;
use State;
use ;
use LogMiddleware;
use JwtAuthenticationDecoder;
async
The above example will allow enable the middleware to properly decode the Claims from the request and set the object on the request as an extention. This allows us to later grab it in an endpoint or another middleware function.
pub async
Assuming you have properly authenticated a request, you can use the encode utility functions to encode a token with the proper claims.
pub async
Claims should be built to allow some uniqueness (such as the exp or the iat) so that encryption/signing is performed with uniqueness each time. It should be noted that it is generally preferred that authentication for a web application is done with Sessions and that JWT is typically reserved for backend api services (or if you are looking at serverless type production systems).