This repository has been archived by the owner on Feb 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathauth_context.ts
96 lines (87 loc) · 2.83 KB
/
auth_context.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* @license
* Copyright (c) Aiden.ai
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { ModuleRpcClient } from '../../client';
import { ModuleRpcCommon } from '../../common';
import { ModuleRpcServer } from '../../server';
/**
* This is the type of the request context that results from context decoding by the client
* context connector (used client-side).
*
* In this mock authentication connector, what is provided is the ID of the user
* performing the request.
*/
export type AuthRequestContext = {
userId: string;
};
/**
* This is the type of the response context that results from context decoding by the server
* context connector (used server-side).
*
* Here, the server sends an empty context to the client.
*/
export type AuthResponseContext = {};
/**
* These are the context keys that the context connectors decode from.
*/
export const authContextKeys = {
/** The key for the encoded context entry that stores the ID of the user making the request. */
userId: 'x-user-id',
};
/**
* This context connector is used client-side.
*/
export class AuthClientContextConnector
implements ModuleRpcClient.ClientContextConnector<AuthResponseContext> {
constructor(private readonly userId: string) {}
/**
* Encode the request context that is sent to the server
* (the server decodes it into an `AuthRequestContext` using the `AuthServerContextConnector`)
*/
async provideRequestContext(): Promise<ModuleRpcCommon.EncodedContext> {
return {
[authContextKeys.userId]: this.userId,
};
}
/**
* Decode the response context received from the server into an `AuthResponseContext`.
*/
async decodeResponseContext(
_encodedResponseContext: ModuleRpcCommon.EncodedContext,
): Promise<AuthResponseContext> {
return {};
}
}
/**
* This context connector is used server-side.
*/
export class AuthServerContextConnector
implements ModuleRpcServer.ServerContextConnector<AuthRequestContext> {
constructor(private readonly expectedUserId: string) {}
/**
* Decode the encode request context sent by the client into an `AuthRequestContext`.
*/
async decodeRequestContext(
encodedRequestContext: ModuleRpcCommon.EncodedContext,
): Promise<AuthRequestContext> {
const userId = encodedRequestContext[authContextKeys.userId];
if (userId !== this.expectedUserId) {
throw new ModuleRpcServer.ServerRpcError(
ModuleRpcCommon.RpcErrorType.unauthenticated,
'unexpected userId',
);
}
return { userId };
}
/**
* Encode the response context that is sent to the client (the client decodes it
* into an `AuthResponseContext` using the `AuthClientContextConnector`).
*/
async provideResponseContext(): Promise<ModuleRpcCommon.EncodedContext> {
return {};
}
}