0.1.3 • Published 4 years ago

nestjs-guard-grpc v0.1.3

Weekly downloads
3
License
ISC
Repository
github
Last release
4 years ago

Nestjs - GrpcAuthGuard

GrpcAuthGuard is an agnostic guard for NestJS optimized for grpc scope. You can inject you personalized auth service to customize it. This guard read from metadatas on a grpc call.

The library contains also a decorator, called GRPCUser, that inject the user loaded into your service.

Installation

npm i --save nestjs-guard-grpc

Usage

On you controller use the guard GrpcAuthGuard

@Controller()
export class UserController {
  @UseGuards(GrpcAuthGuard)
  @GrpcMethod('UserService', 'FindAll')
  findAll(@GRPCUser() user: User, metadata: any) {
    console.log('User injected', user);
    return [];
  }
}

@GRPCUser() is a decorator that inject the user loaded from the authentication.

Now you need to build your own auth service that implement the IAuthService interface. For example if you want to use a jwt token you can use the follow service:

import { Injectable } from '@nestjs/common';
import { UserInterface, IAuthService } from 'nestjs-guard-grpc';
import * as jwt from 'jsonwebtoken';
import { ConfigService } from '@nestjs/config';
const jwksClient = require('jwks-rsa');

@Injectable()
export class GrpcJwtService implements IAuthService {
  client: any;

  constructor(private configService: ConfigService) {
    this.client = jwksClient({
      jwksUri: configService.get<string>('auth.jwks_uri'),
      issuer: configService.get<string>('auth.iss'),
      audience: configService.get<string>('auth.aud'),
    });
  }

  async verify(params: any): Promise<UserInterface | undefined> {
    let token = params;
    let self = this;

    return new Promise(function (resolve, reject) {
      jwt.verify(token, getKey, {}, (err, decoded) => {
        if (err) reject(err);

        resolve(decoded);
      });
    }).then((user) => user);

    function getKey(header, callback) {
      self.client.getSigningKey(header.kid, function (err, key) {
        var signingKey = key.publicKey || key.rsaPublicKey;
        callback(null, signingKey);
      });
    }
  }
}

Finally inject your own service into GrpcAuthGuard

@Module({
  controllers: [UserController],
  providers: [
    GrpcJwtService,
    {
      provide: 'IAuthService',
      useClass: GrpcJwtService,
    },
  ],
})
export class UsersModule {}

Testing

The following code is a base grpc client. How you can see the token jwt is part of the metadata field.

const echoService = new UserServiceClient('http://localhost:8080', null, null);

const request = new Empty();

let jwt = '{your_jwt_token}';

const call = echoService.findAll(
  request,
  { Authorization: 'Bearer ' + jwt },
  (err: grpcWeb.Error, response: UserList) => {
    console.log(err);
    console.log(response);
  },
);
call.on('status', (status: grpcWeb.Status) => {
  console.log('Status', status);
});

References

  • https://docs.nestjs.com/guards
  • https://grpc.io/
0.1.3

4 years ago

0.1.2

4 years ago

0.1.1

4 years ago

0.0.30

4 years ago

0.0.27

4 years ago

0.0.28

4 years ago

0.0.29

4 years ago

0.0.23

4 years ago

0.0.24

4 years ago

0.0.25

4 years ago

0.0.26

4 years ago

0.0.20

4 years ago

0.0.21

4 years ago

0.0.22

4 years ago

0.0.18

4 years ago

0.0.19

4 years ago

0.0.15

4 years ago

0.0.16

4 years ago

0.0.17

4 years ago

0.0.14

4 years ago

0.0.13

4 years ago

0.0.11

4 years ago

0.0.12

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.6

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago