-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e842c6
commit a4db5f3
Showing
9 changed files
with
901 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const request = require("supertest"); | ||
const express = require("express"); | ||
|
||
let { middleware, errorHandler } = require("../../../framework/express"); | ||
|
||
const getWebauthnLib = require("./getWebAuthnLib"); | ||
|
||
const createUser = async (rpId, rpName, origin) => { | ||
const app = express(); | ||
app.use(middleware()); | ||
app.use(errorHandler()); | ||
|
||
const email = `${Math.random().toString().slice(2)}@supertokens.com`; | ||
let registerOptionsResponse = await new Promise((resolve, reject) => | ||
request(app) | ||
.post("/auth/webauthn/options/register") | ||
.send({ | ||
email, | ||
}) | ||
.expect(200) | ||
.end((err, res) => { | ||
if (err) { | ||
console.log(err); | ||
reject(err); | ||
} else { | ||
resolve(JSON.parse(res.text)); | ||
} | ||
}) | ||
); | ||
|
||
const { createCredential } = await getWebauthnLib(); | ||
const credential = createCredential(registerOptionsResponse, { | ||
rpId, | ||
rpName, | ||
origin, | ||
userNotPresent: false, | ||
userNotVerified: false, | ||
}); | ||
|
||
let signUpResponse = await new Promise((resolve, reject) => | ||
request(app) | ||
.post("/auth/webauthn/signup") | ||
.send({ | ||
credential, | ||
webauthnGeneratedOptionsId: registerOptionsResponse.webauthnGeneratedOptionsId, | ||
shouldTryLinkingWithSessionUser: false, | ||
}) | ||
.expect(200) | ||
.end((err, res) => { | ||
if (err) { | ||
console.log(err); | ||
reject(err); | ||
} else { | ||
resolve(JSON.parse(res.text)); | ||
} | ||
}) | ||
); | ||
|
||
return { email, signUpResponse, registerOptionsResponse, credential }; | ||
}; | ||
|
||
module.exports = createUser; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const { readFile } = require("fs/promises"); | ||
|
||
require("./wasm_exec"); | ||
|
||
const getWebauthnLib = async () => { | ||
const wasmBuffer = await readFile(__dirname + "/webauthn.wasm"); | ||
|
||
// Set up the WebAssembly module instance | ||
const go = new Go(); | ||
const { instance } = await WebAssembly.instantiate(wasmBuffer, go.importObject); | ||
go.run(instance); | ||
|
||
// Export extractURL from the global object | ||
const createCredential = ( | ||
registerOptions, | ||
{ userNotPresent = true, userNotVerified = true, rpId, rpName, origin } | ||
) => { | ||
const registerOptionsString = JSON.stringify(registerOptions); | ||
const result = global.createCredential( | ||
registerOptionsString, | ||
rpId, | ||
rpName, | ||
origin, | ||
userNotPresent, | ||
userNotVerified | ||
); | ||
|
||
if (!result) { | ||
throw new Error("Failed to create credential"); | ||
} | ||
|
||
try { | ||
const credential = JSON.parse(result); | ||
return credential; | ||
} catch (e) { | ||
throw new Error("Failed to parse credential"); | ||
} | ||
}; | ||
|
||
const createAndAssertCredential = ( | ||
registerOptions, | ||
signInOptions, | ||
{ userNotPresent = false, userNotVerified = false, rpId, rpName, origin } | ||
) => { | ||
const registerOptionsString = JSON.stringify(registerOptions); | ||
const signInOptionsString = JSON.stringify(signInOptions); | ||
|
||
const result = global.createAndAssertCredential( | ||
registerOptionsString, | ||
signInOptionsString, | ||
rpId, | ||
rpName, | ||
origin, | ||
userNotPresent, | ||
userNotVerified | ||
); | ||
|
||
if (!result) { | ||
throw new Error("Failed to create/assert credential"); | ||
} | ||
|
||
try { | ||
const parsedResult = JSON.parse(result); | ||
return { attestation: parsedResult.attestation, assertion: parsedResult.assertion }; | ||
} catch (e) { | ||
throw new Error("Failed to parse result"); | ||
} | ||
}; | ||
|
||
return { createCredential, createAndAssertCredential }; | ||
}; | ||
|
||
module.exports = getWebauthnLib; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
let SuperTokens = require("../../../lib/build/supertokens").default; | ||
|
||
const getWebAuthnRecipe = () => { | ||
return SuperTokens.getInstanceOrThrowError().recipeModules.find((rm) => rm.getRecipeId() === "webauthn"); | ||
}; | ||
|
||
module.exports = getWebAuthnRecipe; |
File renamed without changes.
File renamed without changes.
Oops, something went wrong.