-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
[PM-7382] Add support for non-UUID credential #11993
Changes from all commits
f453b75
d444d4c
3d1e744
8906495
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { compareCredentialIds, parseCredentialId } from "./credential-id-utils"; | ||
|
||
describe("credential-id-utils", () => { | ||
describe("parseCredentialId", () => { | ||
it("returns credentialId in binary format when given a valid UUID string", () => { | ||
const result = parseCredentialId("08d70b74-e9f5-4522-a425-e5dcd40107e7"); | ||
|
||
expect(result).toEqual( | ||
new Uint8Array([ | ||
0x08, 0xd7, 0x0b, 0x74, 0xe9, 0xf5, 0x45, 0x22, 0xa4, 0x25, 0xe5, 0xdc, 0xd4, 0x01, 0x07, | ||
0xe7, | ||
]), | ||
); | ||
}); | ||
|
||
it("returns credentialId in binary format when given a valid Base64Url string", () => { | ||
const result = parseCredentialId("b64.CNcLdOn1RSKkJeXc1AEH5w"); | ||
|
||
expect(result).toEqual( | ||
new Uint8Array([ | ||
0x08, 0xd7, 0x0b, 0x74, 0xe9, 0xf5, 0x45, 0x22, 0xa4, 0x25, 0xe5, 0xdc, 0xd4, 0x01, 0x07, | ||
0xe7, | ||
]), | ||
); | ||
}); | ||
|
||
it("returns undefined when given an invalid Base64 string", () => { | ||
const result = parseCredentialId("b64.#$%&"); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
|
||
it("returns undefined when given an invalid UUID string", () => { | ||
const result = parseCredentialId("invalid"); | ||
|
||
expect(result).toBeUndefined(); | ||
}); | ||
}); | ||
|
||
describe("compareCredentialIds", () => { | ||
it("returns true when the two credential IDs are equal", () => { | ||
const a = new Uint8Array([0x01, 0x02, 0x03]); | ||
const b = new Uint8Array([0x01, 0x02, 0x03]); | ||
|
||
const result = compareCredentialIds(a, b); | ||
|
||
expect(result).toBe(true); | ||
}); | ||
|
||
it("returns false when the two credential IDs are not equal", () => { | ||
const a = new Uint8Array([0x01, 0x02, 0x03]); | ||
const b = new Uint8Array([0x01, 0x02, 0x04]); | ||
|
||
const result = compareCredentialIds(a, b); | ||
|
||
expect(result).toBe(false); | ||
}); | ||
|
||
it("returns false when the two credential IDs have different lengths", () => { | ||
const a = new Uint8Array([0x01, 0x02, 0x03]); | ||
const b = new Uint8Array([0x01, 0x02, 0x03, 0x04]); | ||
|
||
const result = compareCredentialIds(a, b); | ||
|
||
expect(result).toBe(false); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Fido2Utils } from "./fido2-utils"; | ||
import { guidToRawFormat } from "./guid-utils"; | ||
|
||
export function parseCredentialId(encodedCredentialId: string): Uint8Array { | ||
try { | ||
if (encodedCredentialId.startsWith("b64.")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I hate it, but let's run with it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 100% agree |
||
return Fido2Utils.stringToBuffer(encodedCredentialId.slice(4)); | ||
} | ||
|
||
return guidToRawFormat(encodedCredentialId); | ||
} catch { | ||
return undefined; | ||
} | ||
} | ||
|
||
/** | ||
* Compares two credential IDs for equality. | ||
*/ | ||
export function compareCredentialIds(a: Uint8Array, b: Uint8Array): boolean { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We may benefit from having a general util function for comparing Uint8Arrays. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah but you've always stopped me when adding functions to the general utils in the past so now I'm going by "if we use it twice put it in general" XD |
||
if (a.length !== b.length) { | ||
return false; | ||
} | ||
|
||
for (let i = 0; i < a.length; i++) { | ||
if (a[i] !== b[i]) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { guidToRawFormat } from "./guid-utils"; | ||
|
||
describe("guid-utils", () => { | ||
describe("guidToRawFormat", () => { | ||
it.each([ | ||
[ | ||
"00000000-0000-0000-0000-000000000000", | ||
[ | ||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | ||
0x00, | ||
], | ||
"08d70b74-e9f5-4522-a425-e5dcd40107e7", | ||
[ | ||
0x08, 0xd7, 0x0b, 0x74, 0xe9, 0xf5, 0x45, 0x22, 0xa4, 0x25, 0xe5, 0xdc, 0xd4, 0x01, 0x07, | ||
0xe7, | ||
], | ||
], | ||
])("returns UUID in binary format when given a valid UUID string", (input, expected) => { | ||
const result = guidToRawFormat(input); | ||
|
||
expect(result).toEqual(new Uint8Array(expected)); | ||
}); | ||
|
||
it("throws an error when given an invalid UUID string", () => { | ||
expect(() => guidToRawFormat("invalid")).toThrow(TypeError); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
General question, should be interface be prescriptive enough about the ID to define it as
Uint8Array
, or useArrayBuffer
? I don't see an issue sticking withUint8Array
though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually commented this in the commit. I don't think it matters