1.1.3 • Published 3 years ago

typescript-rest-jwt-guard v1.1.3

Weekly downloads
21
License
MIT
Repository
github
Last release
3 years ago

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.

1.1.3

3 years ago

1.1.2

3 years ago

1.1.1

3 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago