galaxis-utrait-commons

Common module for galaxis utrait apps

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
galaxis-utrait-commons
1.2.22 years ago2 years agoMinified + gzip package size for galaxis-utrait-commons in KB

Readme

galaxis-utrait-commons
!npm packagenpm-imgnpm-url !Build Statusbuild-imgbuild-url !Downloadsdownloads-imgdownloads-url !Issuesissues-imgissues-url !Semantic Releasesemantic-release-imgsemantic-release-url
Galaxis Utrait Common Module

Install

npm install galaxis-utrait-commons
or
yarn add galaxis-utrait-commons

Usage

Initialize

import { init } from 'galaxis-utrait-commons';

init(initParams);

Init Params
const initParams = {
    jwtKey?: string;
    jwtKeyUrl?: string;
    cacheTtlMinutes?: number;
};

  • jwtKey : use this if the jwt publickey is set in the app .env
  • jwtKeyUrl : API url to get jwt publickey
  • cacheTtlMinutes : Timeout in minutes for caching api key from jwtKeyUrl. When cache expires, the next authentication request will refetch the key. Defaults to 10 minutes

Initialize with jwtKey
const initParams = {
  jwtKey: 'RSA_PUBLIC_KEY',
};

init(initParams);

Initialize with jwtKeyUrl
const initParams = {
  jwtKeyUrl: 'https://utility-trait-app/api/auth/public_key',
  cacheTtlMinutes: 60,
};

init(initParams);

Auth Middleware
import { auth } from 'galaxis-utrait-commons';

// Protected Route
app.post('/admin', auth);

If a route has auth middleware, it will check the request header Authorization for bearer token. Requests shall fail if authorization header is not found.
Request Headers
...
Accept: ...
Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIweGM5Yjk1OGUwZjdlOGFiZTQ1YTk5Y2NjYmUwNmMzMWRjNDZkOGM5OTUiLCJpYXQiOjE2NjEyMzgyMzcsImV4cCI6MTY
....

Expected JWT Token Payload
const payload {
  sub: string;
  iat: number;
  exp: number;
  type: string;
}

  • sub : User Wallet Address
  • iat : Issued time in unix
  • exp : Exp time in unix
  • type : The token type should be "microservice_access". Any other type will be rejected and the auth will fail

After a successful JWT auth, payload.sub will be assigned to req.user
// Controller Class
const adminDoSomething = async (req: Request, res: Response) => {
    const userWalletAddress = req.user;
    ....
    // do something
}