|
| 1 | +import type { |
| 2 | + IdentityRequestData, |
| 3 | + IdentityResponseData, |
| 4 | +} from '#src/client/types.js'; |
| 5 | +import type { TLSConfig } from '#network/types.js'; |
| 6 | +import fs from 'node:fs'; |
| 7 | +import path from 'node:path'; |
| 8 | +import os from 'node:os'; |
| 9 | +import Logger, { formatting, LogLevel, StreamHandler } from '@matrixai/logger'; |
| 10 | +import { RPCClient } from '@matrixai/rpc'; |
| 11 | +import { WebSocketClient } from '@matrixai/ws'; |
| 12 | +import * as testsUtils from '../../utils/index.js'; |
| 13 | +import { AuthSignToken } from '#client/handlers/index.js'; |
| 14 | +import { authSignToken } from '#client/callers/index.js'; |
| 15 | +import KeyRing from '#keys/KeyRing.js'; |
| 16 | +import Token from '#tokens/Token.js'; |
| 17 | +import ClientService from '#client/ClientService.js'; |
| 18 | +import * as keysUtils from '#keys/utils/index.js'; |
| 19 | +import * as networkUtils from '#network/utils.js'; |
| 20 | +import * as clientErrors from '#client/errors.js'; |
| 21 | + |
| 22 | +describe('authSignToken', () => { |
| 23 | + const logger = new Logger('authSignToken test', LogLevel.WARN, [ |
| 24 | + new StreamHandler( |
| 25 | + formatting.format`${formatting.level}:${formatting.keys}:${formatting.msg}`, |
| 26 | + ), |
| 27 | + ]); |
| 28 | + const password = 'password'; |
| 29 | + const localhost = '127.0.0.1'; |
| 30 | + let dataDir: string; |
| 31 | + let keyRing: KeyRing; |
| 32 | + let tlsConfig: TLSConfig; |
| 33 | + let clientService: ClientService; |
| 34 | + let webSocketClient: WebSocketClient; |
| 35 | + let rpcClient: RPCClient<{ |
| 36 | + authSignToken: typeof authSignToken; |
| 37 | + }>; |
| 38 | + |
| 39 | + beforeEach(async () => { |
| 40 | + dataDir = await fs.promises.mkdtemp( |
| 41 | + path.join(os.tmpdir(), 'polykey-test-'), |
| 42 | + ); |
| 43 | + const keysPath = path.join(dataDir, 'keys'); |
| 44 | + keyRing = await KeyRing.createKeyRing({ |
| 45 | + password, |
| 46 | + keysPath, |
| 47 | + passwordOpsLimit: keysUtils.passwordOpsLimits.min, |
| 48 | + passwordMemLimit: keysUtils.passwordMemLimits.min, |
| 49 | + strictMemoryLock: false, |
| 50 | + logger, |
| 51 | + }); |
| 52 | + tlsConfig = await testsUtils.createTLSConfig(keyRing.keyPair); |
| 53 | + clientService = new ClientService({ |
| 54 | + tlsConfig, |
| 55 | + logger: logger.getChild(ClientService.name), |
| 56 | + }); |
| 57 | + await clientService.start({ |
| 58 | + manifest: { |
| 59 | + authSignToken: new AuthSignToken({ |
| 60 | + keyRing, |
| 61 | + }), |
| 62 | + }, |
| 63 | + host: localhost, |
| 64 | + }); |
| 65 | + webSocketClient = await WebSocketClient.createWebSocketClient({ |
| 66 | + config: { |
| 67 | + verifyPeer: false, |
| 68 | + }, |
| 69 | + host: localhost, |
| 70 | + logger: logger.getChild(WebSocketClient.name), |
| 71 | + port: clientService.port, |
| 72 | + }); |
| 73 | + rpcClient = new RPCClient({ |
| 74 | + manifest: { |
| 75 | + authSignToken, |
| 76 | + }, |
| 77 | + streamFactory: () => webSocketClient.connection.newStream(), |
| 78 | + toError: networkUtils.toError, |
| 79 | + logger: logger.getChild(RPCClient.name), |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + afterEach(async () => { |
| 84 | + await keyRing.stop(); |
| 85 | + await clientService.stop({ force: true }); |
| 86 | + await webSocketClient.destroy({ force: true }); |
| 87 | + await keyRing.stop(); |
| 88 | + await fs.promises.rm(dataDir, { |
| 89 | + force: true, |
| 90 | + recursive: true, |
| 91 | + }); |
| 92 | + }); |
| 93 | + |
| 94 | + test('should sign a valid token', async () => { |
| 95 | + // Create token with separate key pair |
| 96 | + const keyPair = keysUtils.generateKeyPair(); |
| 97 | + const token = Token.fromPayload<IdentityRequestData>({ |
| 98 | + publicKey: keyPair.publicKey.toString('base64url'), |
| 99 | + returnURL: 'test', |
| 100 | + }); |
| 101 | + token.signWithPrivateKey(keyPair); |
| 102 | + |
| 103 | + // Get the node to sign the token as well |
| 104 | + const encodedToken = token.toEncoded(); |
| 105 | + const identityToken = await rpcClient.methods.authSignToken(encodedToken); |
| 106 | + |
| 107 | + // Check the signature of both the incoming token and the original sent token |
| 108 | + const decodedToken = Token.fromEncoded<IdentityResponseData>(identityToken); |
| 109 | + const decodedPublicKey = keysUtils.publicKeyFromNodeId(keyRing.getNodeId()); |
| 110 | + expect(decodedToken.verifyWithPublicKey(decodedPublicKey)).toBeTrue(); |
| 111 | + const requestToken = Token.fromEncoded<IdentityRequestData>( |
| 112 | + decodedToken.payload.requestToken, |
| 113 | + ); |
| 114 | + expect(requestToken.verifyWithPublicKey(keyPair.publicKey)).toBeTrue(); |
| 115 | + }); |
| 116 | + |
| 117 | + test('should fail if public key does not match signature', async () => { |
| 118 | + // Create token with a key pair and sign it with another |
| 119 | + const keyPair1 = keysUtils.generateKeyPair(); |
| 120 | + const keyPair2 = keysUtils.generateKeyPair(); |
| 121 | + const token = Token.fromPayload<IdentityRequestData>({ |
| 122 | + publicKey: keyPair1.publicKey.toString('base64url'), |
| 123 | + returnURL: 'test', |
| 124 | + }); |
| 125 | + token.signWithPrivateKey(keyPair2); |
| 126 | + |
| 127 | + // The token should fail validation |
| 128 | + const encodedToken = token.toEncoded(); |
| 129 | + await testsUtils.expectRemoteError( |
| 130 | + rpcClient.methods.authSignToken(encodedToken), |
| 131 | + clientErrors.ErrorClientAuthenticationInvalidToken, |
| 132 | + ); |
| 133 | + }); |
| 134 | +}); |
0 commit comments