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

Switch sha256 to be synchronous in @turnkey/webauthn-stamper #179

Merged
merged 2 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/breezy-kangaroos-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@turnkey/webauthn-stamper": patch
---

Make sha256 computation synchronous to resolve ios passkey prompt issues
24 changes: 15 additions & 9 deletions examples/with-federated-passkeys/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -184,15 +184,21 @@ export default function Home() {
);

const login = async () => {
// We use the parent org ID, which we know at all times,
const res = await turnkeyClient.getWhoami({
organizationId: process.env.NEXT_PUBLIC_ORGANIZATION_ID!,
});
// to get the sub-org ID, which we don't know at this point because we don't
// have a DB. Note that we are able to perform this lookup by using the
// credential ID from the users WebAuthn stamp.
setSubOrgId(res.organizationId);
await getWallet(res.organizationId);
// We use the parent org ID, which we know at all times...
try {
const res = await turnkeyClient.getWhoami({
organizationId: process.env.NEXT_PUBLIC_ORGANIZATION_ID!,
});
// ...to get the sub-org ID, which we don't know at this point because we don't
// have a DB. Note that we are able to perform this lookup by using the
// credential ID from the users WebAuthn stamp.
setSubOrgId(res.organizationId);
await getWallet(res.organizationId);
} catch (e: any) {
const message = `Error caught during login: ${e.toString()}`;
console.error(message);
alert(message);
}
};

return (
Expand Down
3 changes: 2 additions & 1 deletion packages/webauthn-stamper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"node": ">=16.0.0"
},
"dependencies": {
"buffer": "^6.0.3"
"buffer": "^6.0.3",
"@noble/hashes": "^1.3.2"
}
}
7 changes: 4 additions & 3 deletions packages/webauthn-stamper/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/// <reference lib="dom" />
import { get as webauthnCredentialGet } from "./webauthn-json";
import { buffer as Buffer } from "./universal";
import { sha256 } from "@noble/hashes/sha256";

// Header name for a webauthn stamp
const stampHeaderName = "X-Stamp-Webauthn";
Expand Down Expand Up @@ -38,7 +39,7 @@ export class WebauthnStamper {
}

async stamp(payload: string) {
const challenge = await getChallengeFromPayload(payload);
const challenge = getChallengeFromPayload(payload);

const signingOptions: CredentialRequestOptions = {
publicKey: {
Expand Down Expand Up @@ -67,9 +68,9 @@ export class WebauthnStamper {
}
}

async function getChallengeFromPayload(payload: string): Promise<Uint8Array> {
function getChallengeFromPayload(payload: string): Uint8Array {
const messageBuffer = new TextEncoder().encode(payload);
const hashBuffer = await crypto.subtle.digest("SHA-256", messageBuffer);
const hashBuffer = sha256(messageBuffer);
const hexString = Buffer.from(hashBuffer).toString("hex");
const hexBuffer = Buffer.from(hexString, "utf8");
return new Uint8Array(hexBuffer);
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading