1- import type {
2- IdentityRequestData ,
3- IdentityResponseData ,
4- } from '#src/client/types.js' ;
1+ import type { IdentityResponseData } from '#client/types.js' ;
52import type { TLSConfig } from '#network/types.js' ;
63import fs from 'node:fs' ;
74import path from 'node:path' ;
@@ -10,17 +7,17 @@ import Logger, { formatting, LogLevel, StreamHandler } from '@matrixai/logger';
107import { RPCClient } from '@matrixai/rpc' ;
118import { WebSocketClient } from '@matrixai/ws' ;
129import * as testsUtils from '../../utils/index.js' ;
13- import { AuthSignToken } from '#client/handlers/index.js' ;
14- import { authSignToken } from '#client/callers/index.js' ;
10+ import { AuthIdentityToken } from '#client/handlers/index.js' ;
11+ import { authIdentityToken } from '#client/callers/index.js' ;
1512import KeyRing from '#keys/KeyRing.js' ;
1613import Token from '#tokens/Token.js' ;
1714import ClientService from '#client/ClientService.js' ;
1815import * as keysUtils from '#keys/utils/index.js' ;
1916import * as networkUtils from '#network/utils.js' ;
20- import * as clientErrors from '#client/errors .js' ;
17+ import * as nodesUtils from '#nodes/utils .js' ;
2118
22- describe ( 'authSignToken ' , ( ) => {
23- const logger = new Logger ( 'authSignToken test' , LogLevel . WARN , [
19+ describe ( 'authIdentityToken ' , ( ) => {
20+ const logger = new Logger ( 'authIdentityToken test' , LogLevel . WARN , [
2421 new StreamHandler (
2522 formatting . format `${ formatting . level } :${ formatting . keys } :${ formatting . msg } ` ,
2623 ) ,
@@ -33,7 +30,7 @@ describe('authSignToken', () => {
3330 let clientService : ClientService ;
3431 let webSocketClient : WebSocketClient ;
3532 let rpcClient : RPCClient < {
36- authSignToken : typeof authSignToken ;
33+ authIdentityToken : typeof authIdentityToken ;
3734 } > ;
3835
3936 beforeEach ( async ( ) => {
@@ -56,7 +53,7 @@ describe('authSignToken', () => {
5653 } ) ;
5754 await clientService . start ( {
5855 manifest : {
59- authSignToken : new AuthSignToken ( {
56+ authIdentityToken : new AuthIdentityToken ( {
6057 keyRing,
6158 } ) ,
6259 } ,
@@ -72,7 +69,7 @@ describe('authSignToken', () => {
7269 } ) ;
7370 rpcClient = new RPCClient ( {
7471 manifest : {
75- authSignToken ,
72+ authIdentityToken ,
7673 } ,
7774 streamFactory : ( ) => webSocketClient . connection . newStream ( ) ,
7875 toError : networkUtils . toError ,
@@ -91,44 +88,14 @@ describe('authSignToken', () => {
9188 } ) ;
9289 } ) ;
9390
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
91+ test ( 'should return a signed token' , async ( ) => {
92+ const identityToken = await rpcClient . methods . authIdentityToken ( { } ) ;
10893 const decodedToken = Token . fromEncoded < IdentityResponseData > ( identityToken ) ;
10994 const decodedPublicKey = keysUtils . publicKeyFromNodeId ( keyRing . getNodeId ( ) ) ;
11095 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- ) ;
96+ const encodedNodeId = nodesUtils . encodeNodeId ( keyRing . getNodeId ( ) ) ;
97+ expect ( decodedToken . payload . iss ) . toBe ( encodedNodeId ) ;
98+ expect ( decodedToken . payload . exp ) . toBeDefined ( ) ;
99+ expect ( decodedToken . payload . jti ) . toBeDefined ( ) ;
133100 } ) ;
134101} ) ;
0 commit comments