Skip to content

Commit

Permalink
Merge pull-request #179
Browse files Browse the repository at this point in the history
  • Loading branch information
r-n-o committed Nov 28, 2023
2 parents fb3a175 + a03e385 commit 1146884
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
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.

0 comments on commit 1146884

Please sign in to comment.