galaxis-utrait-commons
v1.2.2
Published
Common module for galaxis utrait apps
Downloads
2
Maintainers
Readme
galaxis-utrait-commons
Galaxis Utrait Common Module
Install
npm install galaxis-utrait-commons
or
yarn add galaxis-utrait-commonsUsage
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 .envjwtKeyUrl: API url to get jwt public_keycacheTtlMinutes: Timeout in minutes for caching api key fromjwtKeyUrl. 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 Addressiat: Issued time in unixexp: Exp time in unixtype: 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
}