Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept number maxTimeToLive #1051

Merged
merged 1 commit into from
Nov 24, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 29 additions & 14 deletions src/frontend/src/flows/authenticate/postMessageInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,38 +38,52 @@ export interface AuthRequest {
}

/** Try to read unknown data as authentication request */
const asAuthRequest = (msg: unknown): AuthRequest | undefined => {
const asAuthRequest = (msg: unknown): AuthRequest | string => {
const obj = unknownToRecord(msg);

if (obj === undefined) {
return undefined;
return "request is undefined";
}

if (!hasOwnProperty(obj, "kind") || obj.kind !== "authorize-client") {
return undefined;
if (!hasOwnProperty(obj, "kind")) {
return "request does not have 'kind'";
}

if (
!hasOwnProperty(obj, "sessionPublicKey") ||
!(obj.sessionPublicKey instanceof Uint8Array)
) {
return undefined;
if (obj.kind !== "authorize-client") {
return "'kind' is not 'authorize-client'";
}

if (!hasOwnProperty(obj, "sessionPublicKey")) {
return "request does not have 'sessionPublicKey'";
}

if (!(obj.sessionPublicKey instanceof Uint8Array)) {
return "'sessionPublicKey' is not 'Uint8Array'";
}

// Temporary work around for clients that use 'number' instead of 'bigint'
// https://github.com/dfinity/internet-identity/issues/1050
let maxTimeToLive = obj.maxTimeToLive;
if (typeof maxTimeToLive === "number") {
console.warn(
"maxTimeToLive is 'number' but should be 'bigint', this will be an error in the future"
);
maxTimeToLive = BigInt(maxTimeToLive);
}

const maxTimeToLive = obj.maxTimeToLive;
if (
typeof maxTimeToLive !== "undefined" &&
typeof maxTimeToLive !== "bigint"
) {
return undefined;
return "'maxTimeToLive' is not 'bigint'";
}

const derivationOrigin = obj.derivationOrigin;
if (
typeof derivationOrigin !== "undefined" &&
typeof derivationOrigin !== "string"
) {
return undefined;
return "'derivationOrigin' is not 'string'";
}

return {
Expand Down Expand Up @@ -165,7 +179,7 @@ const waitForAuthRequest = (): Promise<AuthContext> =>
const eventHandler = async (event: MessageEvent) => {
const message: unknown = event.data; // Drop assumptions about event.data (an 'any')
const authRequest = asAuthRequest(message);
if (authRequest !== undefined) {
if (typeof authRequest !== "string") {
window.removeEventListener("message", eventHandler);
console.log(
`Handling authorize-client request ${JSON.stringify(
Expand All @@ -179,7 +193,8 @@ const waitForAuthRequest = (): Promise<AuthContext> =>
});
} else {
console.warn(
`Bad authentication request received: ${JSON.stringify(message)}`
`Bad authentication request received: ${authRequest}`,
message
);
}
};
Expand Down