@villedemontreal/jwt-validator
Version:
Module to validate JWT (JSON Web Tokens)
40 lines • 1.93 kB
TypeScript
import { Identity } from '../models/identities';
/**
* creates a specific type of Identity from the submitted JWT.
* @param jwt specifies the JWT issued by TokenAPI
* @returns a specific type of Identity
* @description The purpose of this function is to classify the type of user behind the access token and select the right claim
* for the unique ID of the account, as we need the most stable ID as possible.
*
* There is a displayName attribute when you need to display the identity in a GUI.
*
* Also, we provide a toString() function to format a verbose representation of the identity, used to audit as much information as possible.
* @example
* const identity = createIdentityFromJwt(req.jwt);
* product.name = newName;
* product.modifiedBy = identity.id; // udoejo3
* @example
* const identity = createIdentityFromJwt(req.jwt);
* console.log(`Order created by "${identity}"`); // Order created by "user:employee:udoejo3:John DOE:john.doe@montreal.ca:100674051:421408000000:vdm"
* @example
* const identity = createIdentityFromJwt(req.jwt);
* res.send({ message: `Welcome back ${identity.displayName}`}); // { message: "Welcome back John DOE" }
* @example
* const identity = createIdentityFromJwt(req.jwt);
* if (identity.type === 'user' && identity.attributes.type === 'employee') {
* const employeeRecord = await employeeAPI.findEmployee(identity.attributes.registrationNumber);
* } else {
* throw new Error(`Expected an employee but received "${identity}"`);
* }
* @example
* const identity = createIdentityFromJwt(req.jwt);
* if (identity.type === 'user') {
* if (identity.attributes.email) {
* emailService.send(identity.attributes.email, 'Welcome', 'Bla bla...');
* } else {
* throw new Error(`User "${identity}" has no email`);
* }
* }
*/
export declare function createIdentityFromJwt(jwt: any): Identity;
//# sourceMappingURL=createIdentityFromJwt.d.ts.map