-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAuthIdentityToken.ts
More file actions
38 lines (36 loc) · 1.22 KB
/
AuthIdentityToken.ts
File metadata and controls
38 lines (36 loc) · 1.22 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
import type {
ClientRPCRequestParams,
ClientRPCResponseResult,
IdentityResponseData,
TokenIdentityResponse,
} from '../types.js';
import type KeyRing from '../../keys/KeyRing.js';
import { IdSortable } from '@matrixai/id';
import { UnaryHandler } from '@matrixai/rpc';
import Token from '../../tokens/Token.js';
import * as nodesUtils from '../../nodes/utils.js';
import * as clientErrors from '../errors.js';
class AuthIdentityToken extends UnaryHandler<
{
keyRing: KeyRing;
},
ClientRPCRequestParams,
ClientRPCResponseResult<TokenIdentityResponse>
> {
public handle = async (): Promise<TokenIdentityResponse> => {
const { keyRing }: { keyRing: KeyRing } = this.container;
const idGen = new IdSortable();
const jti = idGen.next().value;
if (jti == null) {
throw new clientErrors.ErrorClientAuthenticationInvalidJTI();
}
const outgoingToken = Token.fromPayload<IdentityResponseData>({
jti: jti.toMultibase('base64'),
exp: Math.floor(Date.now() / 1000) + 60, // 60 seconds after issuing
iss: nodesUtils.encodeNodeId(keyRing.getNodeId()),
});
outgoingToken.signWithPrivateKey(keyRing.keyPair);
return outgoingToken.toEncoded();
};
}
export default AuthIdentityToken;