|
1 | 1 | import type { |
2 | 2 | ClientRPCRequestParams, |
3 | 3 | ClientRPCResponseResult, |
4 | | - IdentityRequestData, |
5 | 4 | IdentityResponseData, |
6 | | - TokenIdentityRequest, |
7 | 5 | TokenIdentityResponse, |
8 | 6 | } from '../types.js'; |
9 | 7 | import type KeyRing from '../../keys/KeyRing.js'; |
10 | | -import type { PublicKey } from '../../keys/types.js'; |
11 | 8 | import { UnaryHandler } from '@matrixai/rpc'; |
12 | 9 | import Token from '../../tokens/Token.js'; |
13 | | -import * as clientErrors from '../errors.js'; |
14 | 10 | import * as nodesUtils from '../../nodes/utils.js'; |
15 | 11 |
|
16 | 12 | class AuthSignToken extends UnaryHandler< |
17 | 13 | { |
18 | 14 | keyRing: KeyRing; |
19 | 15 | }, |
20 | | - ClientRPCRequestParams<TokenIdentityRequest>, |
| 16 | + ClientRPCRequestParams, |
21 | 17 | ClientRPCResponseResult<TokenIdentityResponse> |
22 | 18 | > { |
23 | | - public handle = async ( |
24 | | - input: ClientRPCRequestParams<TokenIdentityRequest>, |
25 | | - ): Promise<TokenIdentityResponse> => { |
| 19 | + public handle = async (): Promise<TokenIdentityResponse> => { |
26 | 20 | const { keyRing }: { keyRing: KeyRing } = this.container; |
27 | | - |
28 | | - // Get and verify incoming node |
29 | | - const inputToken = { payload: input.payload, signatures: input.signatures }; |
30 | | - const incomingToken = Token.fromEncoded<IdentityRequestData>(inputToken); |
31 | | - if (!('publicKey' in incomingToken.payload)) { |
32 | | - throw new clientErrors.ErrorClientAuthenticationInvalidToken( |
33 | | - 'Input token does not contain public key', |
34 | | - ); |
35 | | - } |
36 | | - const incomingPublicKey = Buffer.from( |
37 | | - incomingToken.payload.publicKey, |
38 | | - 'base64url', |
39 | | - ) as PublicKey; |
40 | | - if (!incomingToken.verifyWithPublicKey(incomingPublicKey)) { |
41 | | - throw new clientErrors.ErrorClientAuthenticationInvalidToken( |
42 | | - 'Incoming token does not match its signature', |
43 | | - ); |
44 | | - } |
45 | | - |
46 | | - // Create the outgoing token with the incoming token integrated into the |
47 | | - // payload. |
48 | | - const outgoingTokenPayload: IdentityResponseData = { |
49 | | - requestToken: inputToken, |
| 21 | + const tokenPayload: IdentityResponseData = { |
50 | 22 | nodeId: nodesUtils.encodeNodeId(keyRing.getNodeId()), |
51 | 23 | }; |
52 | | - const outgoingToken = |
53 | | - Token.fromPayload<IdentityResponseData>(outgoingTokenPayload); |
| 24 | + const outgoingToken = Token.fromPayload<IdentityResponseData>(tokenPayload); |
54 | 25 | outgoingToken.signWithPrivateKey(keyRing.keyPair); |
55 | 26 | return outgoingToken.toEncoded(); |
56 | 27 | }; |
|
0 commit comments