typescript-rest-jwt-guard
v1.1.3
Published
JWT-checking decorator for typescript-rest
Downloads
2
Readme
JwtGuard for typescript-rest
JwtGuard is a library to use in conjunction with typescript-rest. The library provides a set of decorators to use them with controllers' methods. The purpose of the decorators is JWT access tokens availability control. There are several decorators depending on what token is supposed to be passed with.
Installation
Using npm:
$ npm i typescript-rest-jwt-guard
Or using yarn:
$ yarn add typescript-rest-jwt-guard
Preconditions
As soon as the decorators retrieve tokens from request context, you have to provide context as dependency injection to your wrapped methods:
@JwtCookieGuard('BEARER')
@GET
public myMethod(@Context _context: ServiceContext) {
return { message: 'Ok' };
}
You must also ensure that process.env.JWT_SECRET environment variable contains JWT secret.
Usage with cookies
If access token is supposed to be passed with cookies, you must provide cookie parser:
app.use(cookieParser());
Now you can use decorators in your methods:
import { Path, GET, Context, ServiceContext } from 'typescript-rest';
import { Inject } from 'typescript-ioc';
import { JwtCookieGuard } from 'typescript-rest-jwt-guard';
@Path('/test')
export default class DemoController {
@JwtCookieGuard('BEARER')
@Path('/cookie-jwt')
@GET
public getHealthStatusSecured(@Context _context: ServiceContext) {
return { message: 'Ok' };
}
}
Now requests to that route must provide a cookie with name BEARER and the JWT token as its value.
Usage with authorization header
If access token is supposed to be passed with authorization header, you can use JwtHeaderGuard like this:
import { Path, GET, Context, ServiceContext } from 'typescript-rest';
import { Inject } from 'typescript-ioc';
import { JwtHeaderGuard } from 'typescript-rest-jwt-guard';
@Path('/test')
export default class DemoController {
@JwtHeaderGuard('Bearer')
@Path('/header-jwt')
@GET
public getHealthStatusSecured(@Context _context: ServiceContext) {
return { message: 'Ok' };
}
}
Now all requests to that route must provide authorization header with value 'Bearer X' where X is the JWT access token.
Usage with POST request body data
If access token is supposed to be passed with POST body data, you must provide body parser:
app.use(bodyParser.json());
Now you can use decorators in your methods:
import { Path, POST, Context, ServiceContext } from 'typescript-rest';
import { Inject } from 'typescript-ioc';
import { JwtBodyGuard } from 'typescript-rest-jwt-guard';
@Path('/test')
export default class DemoController {
@JwtBodyGuard('accessToken')
@Path('/body-jwt')
@POST
public getHealthStatusSecured(@Context _context: ServiceContext) {
return { message: 'Ok' };
}
}
Now requests to that route must provide accessToken key inside their bodies with the JWT token as value. Also you can take a look at demo.