galaxis-utrait-commons
TypeScript icon, indicating that this package has built-in type declarations

1.2.2 • Public • Published

galaxis-utrait-commons

npm package Build Status Downloads Issues Semantic Release

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 public_key is set in the app .env
  • jwtKeyUrl : API url to get jwt public_key
  • 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
}

Package Sidebar

Install

npm i galaxis-utrait-commons

Weekly Downloads

0

Version

1.2.2

License

MIT

Unpacked Size

21.1 kB

Total Files

15

Last publish

Collaborators

  • kinsun852