@fphgov/authentication-sdk 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fphgov/authentication-sdk",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "Authentication gRPC service SDK only for FPH",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -8,8 +8,14 @@ service AuthService {
8
8
 
9
9
  message AuthRequest {
10
10
  string token = 1;
11
+ optional int32 serviceId = 2;
12
+ }
13
+
14
+ message AuthUser {
15
+ int32 id = 1;
16
+ string username = 2;
11
17
  }
12
18
 
13
19
  message AuthResponse {
14
- bool success = 1;
20
+ AuthUser user = 1;
15
21
  }
package/src/client.ts CHANGED
@@ -1,9 +1,7 @@
1
1
  import * as grpc from '@grpc/grpc-js'
2
2
  import * as protoLoader from '@grpc/proto-loader'
3
3
  import * as path from 'path'
4
- import { type AuthServiceClient, type AuthServiceConstructor } from './types'
5
-
6
- const { status } = grpc
4
+ import { type AuthUser, type AuthServiceClient, type AuthServiceConstructor } from './types'
7
5
 
8
6
  const PROTO_PATH = path.join(__dirname, '../protos/authentication.proto')
9
7
 
@@ -25,17 +23,13 @@ export class AuthenticationClient {
25
23
  this.grpcClient = new proto.authentication.AuthService(url, grpc.credentials.createInsecure())
26
24
  }
27
25
 
28
- authenticate(token: string): Promise<boolean> {
26
+ authenticate(token: string, serviceId: number): Promise<AuthUser> {
29
27
  return new Promise((resolve, reject) => {
30
- this.grpcClient.authenticate({ token }, (err, response) => {
28
+ this.grpcClient.authenticate({ token, serviceId }, (err, response) => {
31
29
  if (err) {
32
- if (err.code === status.UNAUTHENTICATED) {
33
- resolve(false)
34
- } else {
35
- reject(err)
36
- }
30
+ reject(err)
37
31
  } else {
38
- resolve(response.success)
32
+ resolve(response.user)
39
33
  }
40
34
  })
41
35
  })
package/src/index.ts CHANGED
@@ -1,3 +1,3 @@
1
1
  export { AuthenticationClient } from './client'
2
2
  export { AuthenticationInterceptor } from './interceptor'
3
- export type { AuthRequest, AuthResponse, AuthServiceClient, AuthServiceConstructor } from './types'
3
+ export type { AuthUser, AuthRequest, AuthResponse, AuthServiceClient, AuthServiceConstructor } from './types'
@@ -1,11 +1,16 @@
1
- import { type ExecutionContext, type CallHandler } from '@nestjs/common'
1
+ import { type ExecutionContext, type CallHandler, Logger } from '@nestjs/common'
2
2
  import { RpcException } from '@nestjs/microservices'
3
3
  import { type Observable } from 'rxjs'
4
4
  import { Metadata, status } from '@grpc/grpc-js'
5
5
  import { AuthenticationClient } from './client'
6
6
 
7
7
  export class AuthenticationInterceptor {
8
- constructor(private readonly client: AuthenticationClient) {}
8
+ private readonly logger = new Logger(AuthenticationInterceptor.name)
9
+
10
+ constructor(
11
+ private readonly client: AuthenticationClient,
12
+ private readonly serviceId: number,
13
+ ) {}
9
14
 
10
15
  async intercept(context: ExecutionContext, next: CallHandler): Promise<Observable<unknown>> {
11
16
  const metadata = context.getArgByIndex<Metadata>(1)
@@ -16,9 +21,10 @@ export class AuthenticationInterceptor {
16
21
  throw new RpcException({ code: status.UNAUTHENTICATED, message: 'Missing authentication token' })
17
22
  }
18
23
 
19
- const success = await this.client.authenticate(token)
20
-
21
- if (!success) {
24
+ try {
25
+ const user = await this.client.authenticate(token, this.serviceId)
26
+ this.logger.log(`Authenticated user id=${user.id} username=${user.username}`)
27
+ } catch {
22
28
  throw new RpcException({ code: status.UNAUTHENTICATED, message: 'Invalid authentication token' })
23
29
  }
24
30
 
package/src/types.ts CHANGED
@@ -1,11 +1,17 @@
1
1
  import * as grpc from '@grpc/grpc-js'
2
2
 
3
+ export interface AuthUser {
4
+ id: number
5
+ username: string
6
+ }
7
+
3
8
  export interface AuthRequest {
4
9
  token: string
10
+ serviceId?: number
5
11
  }
6
12
 
7
13
  export interface AuthResponse {
8
- success: boolean
14
+ user: AuthUser
9
15
  }
10
16
 
11
17
  export interface AuthServiceClient extends grpc.Client {