-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathauthenticationMiddleware.ts
More file actions
146 lines (143 loc) · 4.53 KB
/
Copy pathauthenticationMiddleware.ts
File metadata and controls
146 lines (143 loc) · 4.53 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import type {
JSONRPCRequest,
JSONRPCResponse,
MiddlewareFactory,
} from '@matrixai/rpc';
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
} from './types.js';
import type { Session } from '../sessions/index.js';
import type SessionManager from '../sessions/SessionManager.js';
import type KeyRing from '../keys/KeyRing.js';
import type {
JSONRPCResponseError,
JSONRPCResponseFailed,
} from '@matrixai/rpc';
import { TransformStream } from 'stream/web';
import { authenticate, decodeAuth } from './utils.js';
import { sysexits } from '../errors.js';
import * as utils from '../utils/index.js';
import * as networkUtils from '../network/utils.js';
function authenticationMiddlewareServer(
sessionManager: SessionManager,
keyRing: KeyRing,
): MiddlewareFactory<
JSONRPCRequest<ClientRPCRequestParams>,
JSONRPCRequest<ClientRPCRequestParams>,
JSONRPCResponse<ClientRPCResponseResult>,
JSONRPCResponse<ClientRPCResponseResult>
> {
return () => {
// Flag for tracking if the first message has been processed
let forwardFirst = true;
let reverseController;
let outgoingToken: string | null = null;
return {
forward: new TransformStream<
JSONRPCRequest<ClientRPCRequestParams>,
JSONRPCRequest<ClientRPCRequestParams>
>({
transform: async (chunk, controller) => {
if (forwardFirst) {
try {
outgoingToken = await authenticate(
sessionManager,
keyRing,
chunk,
);
} catch (e) {
controller.error(e);
const rpcError: JSONRPCResponseError = {
code: e.exitCode ?? sysexits.UNKNOWN,
message: e.description ?? '',
data: networkUtils.fromError(e),
};
const rpcErrorMessage: JSONRPCResponseFailed = {
jsonrpc: '2.0',
error: rpcError,
id: null,
};
reverseController.enqueue(rpcErrorMessage);
reverseController.error(e);
return;
}
}
forwardFirst = false;
controller.enqueue(chunk);
},
}),
reverse: new TransformStream({
start: (controller) => {
reverseController = controller;
},
transform: (chunk, controller) => {
// Add the outgoing metadata to the next message.
if (outgoingToken != null && 'result' in chunk) {
if (chunk.result.metadata == null) {
chunk.result.metadata = {
authorization: '',
};
}
chunk.result.metadata.authorization = outgoingToken;
outgoingToken = null;
}
controller.enqueue(chunk);
},
}),
};
};
}
function authenticationMiddlewareClient(
session: Session,
): MiddlewareFactory<
JSONRPCRequest<ClientRPCRequestParams>,
JSONRPCRequest<ClientRPCRequestParams>,
JSONRPCResponse<ClientRPCResponseResult>,
JSONRPCResponse<ClientRPCResponseResult>
> {
return () => {
// Flag for tracking if the first message has been processed
let forwardFirst = true;
return {
forward: new TransformStream<
JSONRPCRequest<ClientRPCRequestParams>,
JSONRPCRequest<ClientRPCRequestParams>
>({
transform: async (chunk, controller) => {
if (forwardFirst) {
if (chunk.params == null) {
utils.never('chunk.params must be defined');
}
if (chunk.params.metadata?.authorization == null) {
const token = await session.readToken();
if (token != null) {
if (chunk.params.metadata == null) {
chunk.params.metadata = {
authorization: '',
};
}
chunk.params.metadata.authorization = `Bearer ${token}`;
}
}
}
forwardFirst = false;
controller.enqueue(chunk);
},
}),
reverse: new TransformStream<
JSONRPCResponse<ClientRPCResponseResult>,
JSONRPCResponse<ClientRPCResponseResult>
>({
transform: async (chunk, controller) => {
controller.enqueue(chunk);
if (!('result' in chunk)) return;
const token = decodeAuth(chunk.result);
if (token == null) return;
await session.writeToken(token);
},
}),
};
};
}
export { authenticationMiddlewareServer, authenticationMiddlewareClient };