{"id":20364189,"url":"https://github.com/mabuonomo/nestjs-guard-grpc","last_synced_at":"2025-07-23T21:03:17.130Z","repository":{"id":39966816,"uuid":"254459577","full_name":"mabuonomo/nestjs-guard-grpc","owner":"mabuonomo","description":"GrpcAuthGuard is an agnostic guard for NestJS optimized for grpc scope","archived":false,"fork":false,"pushed_at":"2023-07-18T21:40:49.000Z","size":451,"stargazers_count":11,"open_issues_count":12,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-26T00:06:13.369Z","etag":null,"topics":["authentication","decorators","guard","jwt","nestjs"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mabuonomo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-09T19:23:12.000Z","updated_at":"2024-10-05T05:01:37.000Z","dependencies_parsed_at":"2024-11-15T00:10:29.649Z","dependency_job_id":"c8339a9b-dfbb-4143-b813-fcc661838d19","html_url":"https://github.com/mabuonomo/nestjs-guard-grpc","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mabuonomo%2Fnestjs-guard-grpc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mabuonomo%2Fnestjs-guard-grpc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mabuonomo%2Fnestjs-guard-grpc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mabuonomo%2Fnestjs-guard-grpc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mabuonomo","download_url":"https://codeload.github.com/mabuonomo/nestjs-guard-grpc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248517714,"owners_count":21117506,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["authentication","decorators","guard","jwt","nestjs"],"created_at":"2024-11-15T00:10:23.643Z","updated_at":"2025-04-12T04:34:30.434Z","avatar_url":"https://github.com/mabuonomo.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Nestjs - GrpcAuthGuard\n\n\u003cimg src=\"https://miro.medium.com/max/4500/1*NMkClP0D2ZkWEXAPHYvy1Q.png\"\u003e\n\nGrpcAuthGuard 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.\n\nThe library contains also a decorator, called GRPCUser, that inject the user loaded into your service.\n\n## Installation\n\n```sh\nnpm i --save nestjs-guard-grpc\n```\n\n## Usage\n\nOn you controller use the guard _GrpcAuthGuard_\n\n```ts\n@Controller()\nexport class UserController {\n  @UseGuards(GrpcAuthGuard)\n  @GrpcMethod('UserService', 'FindAll')\n  findAll(@Payload() data: any, metadata: any, @GRPCUser() user: User) {\n    console.log('User injected', user);\n    return [];\n  }\n}\n```\n\n_@GRPCUser()_ is a decorator that inject the user loaded from the authentication.\n\n_@Payload()_ is a official \u003ca href=\"https://github.com/nestjs/nest/issues/4851\" target=\"_blank\"\u003eNestJS\u003c/a\u003e mandatory decorator, necessary if you use a custom decorator like _@GRPCUser()_\n\nNow 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:\n\n```ts\nimport { Injectable } from '@nestjs/common';\nimport { UserInterface, IAuthService } from 'nestjs-guard-grpc';\nimport * as jwt from 'jsonwebtoken';\nimport { ConfigService } from '@nestjs/config';\nconst jwksClient = require('jwks-rsa');\n\n@Injectable()\nexport class GrpcJwtService implements IAuthService {\n  client: any;\n\n  constructor(private configService: ConfigService) {\n    this.client = jwksClient({\n      jwksUri: configService.get\u003cstring\u003e('auth.jwks_uri'),\n      issuer: configService.get\u003cstring\u003e('auth.iss'),\n      audience: configService.get\u003cstring\u003e('auth.aud'),\n    });\n  }\n\n  async verify(params: any): Promise\u003cUserInterface | undefined\u003e {\n    let token = params;\n    let self = this;\n\n    return new Promise(function (resolve, reject) {\n      jwt.verify(token, getKey, {}, (err, decoded) =\u003e {\n        if (err) reject(err);\n\n        resolve(decoded);\n      });\n    }).then((user) =\u003e user);\n\n    function getKey(header, callback) {\n      self.client.getSigningKey(header.kid, function (err, key) {\n        var signingKey = key.publicKey || key.rsaPublicKey;\n        callback(null, signingKey);\n      });\n    }\n  }\n}\n```\n\nFinally inject your own service into _GrpcAuthGuard_\n\n```ts\n@Module({\n  controllers: [UserController],\n  providers: [\n    GrpcJwtService,\n    {\n      provide: 'IAuthService',\n      useClass: GrpcJwtService,\n    },\n  ],\n})\nexport class UsersModule {}\n```\n\n## Testing\n\nThe following code is a base grpc client. How you can see the token jwt is part of the metadata field.\n\n```ts\nconst echoService = new UserServiceClient('http://localhost:8080', null, null);\n\nconst request = new Empty();\n\nlet jwt = '{your_jwt_token}';\n\nconst call = echoService.findAll(\n  request,\n  { Authorization: 'Bearer ' + jwt },\n  (err: grpcWeb.Error, response: UserList) =\u003e {\n    console.log(err);\n    console.log(response);\n  },\n);\ncall.on('status', (status: grpcWeb.Status) =\u003e {\n  console.log('Status', status);\n});\n```\n\n## References\n\n- \u003ca href=\"https://docs.nestjs.com/guards\"\u003ehttps://docs.nestjs.com/guards\u003c/a\u003e\n- \u003ca href=\"https://grpc.io/\"\u003ehttps://grpc.io/\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmabuonomo%2Fnestjs-guard-grpc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmabuonomo%2Fnestjs-guard-grpc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmabuonomo%2Fnestjs-guard-grpc/lists"}