Skip to content

Commit cec241f

Browse files
committed
chore: cleaned up error reporting
1 parent c18059a commit cec241f

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

src/client/callers/authSignToken.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { HandlerTypes } from '@matrixai/rpc';
2-
import type AuthSignToken from '../handlers/AgentLockAll.js';
2+
import type AuthSignToken from '../handlers/AuthSignToken.js';
33
import { UnaryCaller } from '@matrixai/rpc';
44

55
type CallerTypes = HandlerTypes<AuthSignToken>;

src/client/errors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ErrorClientVerificationFailed<T> extends ErrorClientService<T> {
5353
class ErrorAuthentication<T> extends ErrorPolykey<T> {}
5454

5555
class ErrorAuthenticationInvalidToken<T> extends ErrorAuthentication<T> {
56-
static description = 'Incoming token does not match its signature';
56+
static description = 'Token is invalid';
5757
exitCode = sysexits.PROTOCOL;
5858
}
5959

src/client/handlers/AuthSignToken.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { UnaryHandler } from '@matrixai/rpc';
1212
import Token from '../../tokens/Token.js';
1313
import * as clientErrors from '../errors.js';
1414
import * as nodesUtils from '../../nodes/utils.js';
15+
import * as tokensUtils from '../../tokens/utils.js';
1516

1617
class AuthSignToken extends UnaryHandler<
1718
{
@@ -27,12 +28,23 @@ class AuthSignToken extends UnaryHandler<
2728

2829
// Get and verify incoming node
2930
const inputToken = { payload: input.payload, signatures: input.signatures };
30-
const incomingToken = Token.fromEncoded<IdentityRequestData>(inputToken);
31+
const incomingToken = Token.fromEncoded<TokenIdentityRequest>(inputToken);
32+
const inputPayload = tokensUtils.parseTokenPayload<IdentityRequestData>(
33+
inputToken.payload,
34+
);
35+
if (!('publicKey' in inputPayload)) {
36+
throw new clientErrors.ErrorAuthenticationInvalidToken(
37+
'Input token does not contain public key',
38+
);
39+
}
3140
const incomingPublicKey = Buffer.from(
32-
incomingToken.payload.publicKey,
41+
inputPayload.publicKey,
42+
'base64url',
3343
) as PublicKey;
3444
if (!incomingToken.verifyWithPublicKey(incomingPublicKey)) {
35-
throw new clientErrors.ErrorAuthenticationInvalidToken();
45+
throw new clientErrors.ErrorAuthenticationInvalidToken(
46+
'Incoming token does not match its signature',
47+
);
3648
}
3749

3850
// Create the outgoing token with the incoming token integrated into the

src/network/utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,10 @@ function fromError(error: any) {
477477
// serialising only the error type, message and its stack.
478478
const wrappedError = new errors.ErrorPolykeyUnexpected(
479479
`Unexpected error occurred: ${error.name}`,
480-
{ cause: error },
480+
{
481+
cause: error,
482+
data: { message: 'message' in error ? error.message : undefined },
483+
},
481484
);
482485
return wrappedError.toJSON();
483486
}

0 commit comments

Comments
 (0)