Skip to content

Commit

Permalink
Replace @hexagon/base64-arraybuffer with @hexagon/base64
Browse files Browse the repository at this point in the history
  • Loading branch information
Hexagon committed Dec 28, 2021
1 parent e0b7dde commit 075b882
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 67 deletions.
77 changes: 37 additions & 40 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webauthn-skeleton",
"version": "0.9.0",
"version": "0.9.1",
"description": "Skeleton for a Node.js powered Web Authentication API enabled website",
"main": "app.js",
"scripts": {
Expand Down Expand Up @@ -32,7 +32,7 @@
"homepage": "https://github.com/hexagon/webauthn-skeleton#readme",
"license": "MIT",
"dependencies": {
"@hexagon/base64-arraybuffer": "^2.0.1",
"@hexagon/base64": "^1.0.11",
"@koa/router": "^10.1.1",
"fido2-lib": "^2.6.8",
"koa": "^2.13.4",
Expand Down
2 changes: 1 addition & 1 deletion public/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ <h4>Your credentials</h4>

<!-- External dependencies -->
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@hexagon/base64[email protected]/dist/base64-arraybuffer.umd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@hexagon/base64@1/dist/base64.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/qr-creator/dist/qr-creator.min.js"></script>

<!-- Internals-->
Expand Down
12 changes: 6 additions & 6 deletions public/static/js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const publicKeyCredentialToJSON = (pubKeyCred) => {
}

if(pubKeyCred instanceof ArrayBuffer) {
return base64.encode(pubKeyCred,true);
return base64.fromArrayBuffer(pubKeyCred,true);
}

if(pubKeyCred instanceof Object) {
Expand All @@ -37,12 +37,12 @@ const publicKeyCredentialToJSON = (pubKeyCred) => {
* Decodes arrayBuffer required fields.
*/
let preformatMakeCredReq = (makeCredReq) => {
makeCredReq.challenge = base64.decode(makeCredReq.challenge,true);
makeCredReq.user.id = base64.decode(makeCredReq.user.id,true);
makeCredReq.challenge = base64.toArrayBuffer(makeCredReq.challenge,true);
makeCredReq.user.id = base64.toArrayBuffer(makeCredReq.user.id,true);

// Decode id of each excludeCredentials
if (makeCredReq.excludeCredentials) {
makeCredReq.excludeCredentials = makeCredReq.excludeCredentials.map((e) => { return { id: base64.decode(e.id, true), type: e.type };});
makeCredReq.excludeCredentials = makeCredReq.excludeCredentials.map((e) => { return { id: base64.toArrayBuffer(e.id, true), type: e.type };});
}

return makeCredReq;
Expand All @@ -52,11 +52,11 @@ let preformatMakeCredReq = (makeCredReq) => {
* Decodes arrayBuffer required fields.
*/
let preformatGetAssertReq = (getAssert) => {
getAssert.challenge = base64.decode(getAssert.challenge,true);
getAssert.challenge = base64.toArrayBuffer(getAssert.challenge,true);

// Allow any credential, this will be handled later
for(let allowCred of getAssert.allowCredentials) {
allowCred.id = base64.decode(allowCred.id,true);
allowCred.id = base64.toArrayBuffer(allowCred.id,true);
}

return getAssert;
Expand Down
6 changes: 3 additions & 3 deletions public/static/js/webauthn.auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ function register (username, additional) {
.then((response) => {
let makeCredResponse = {
id: response.id,
rawId: base64.encode(response.rawId,true),
rawId: base64.fromArrayBuffer(response.rawId,true),
response: {
attestationObject: base64.encode(response.response.attestationObject,true),
clientDataJSON: base64.encode(response.response.clientDataJSON,true)
attestationObject: base64.fromArrayBuffer(response.response.attestationObject,true),
clientDataJSON: base64.fromArrayBuffer(response.response.clientDataJSON,true)
},
type: response.type
};
Expand Down
16 changes: 8 additions & 8 deletions routes/webauthn.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const
crypto = require("crypto"),
database = require("../db/db"),
username = require("../utils/username"),
base64url = require("@hexagon/base64-arraybuffer"),
base64 = require("@hexagon/base64"),

router = require("@koa/router")({ prefix: "/webauthn" }),

Expand All @@ -18,7 +18,7 @@ const
let randomBase64URLBuffer = (len) => {
len = len || 32;
let buff = crypto.randomBytes(len);
return base64url.encode(buff, true);
return base64.fromArrayBuffer(buff, true);
};

router.post("/register", async (ctx) => {
Expand Down Expand Up @@ -93,7 +93,7 @@ router.post("/add", async (ctx) => {
ctx.session.challenge = challengeMakeCred.challenge;

// Exclude existing credentials
challengeMakeCred.excludeCredentials = database.users[ctx.session.username].authenticators.map((e) => { return { id: base64url.encode(e.credId, true), type: e.type }; });
challengeMakeCred.excludeCredentials = database.users[ctx.session.username].authenticators.map((e) => { return { id: base64.fromArrayBuffer(e.credId, true), type: e.type }; });

// Respond with credentials
return ctx.body = challengeMakeCred;
Expand Down Expand Up @@ -128,7 +128,7 @@ router.post("/login", async (ctx) => {
for(let authr of database.users[ctx.session.username].authenticators) {
allowCredentials.push({
type: authr.type,
id: base64url.encode(authr.credId, true),
id: base64.fromArrayBuffer(authr.credId, true),
transports: ["usb", "nfc", "ble","internal"]
});
}
Expand All @@ -152,8 +152,8 @@ router.post("/response", async (ctx) => {
let webauthnResp = ctx.request.body;
if(webauthnResp.response.attestationObject !== undefined) {
/* This is create cred */
webauthnResp.rawId = base64url.decode(webauthnResp.rawId, true);
webauthnResp.response.attestationObject = base64url.decode(webauthnResp.response.attestationObject, true);
webauthnResp.rawId = base64.toArrayBuffer(webauthnResp.rawId, true);
webauthnResp.response.attestationObject = base64.toArrayBuffer(webauthnResp.response.attestationObject, true);
const result = await f2l.attestation(webauthnResp, config.origin, ctx.session.challenge);

const token = {
Expand All @@ -179,8 +179,8 @@ router.post("/response", async (ctx) => {
// save the challenge in the session information...
// send authnOptions to client and pass them in to `navigator.credentials.get()`...
// get response back from client (clientAssertionResponse)
webauthnResp.rawId = base64url.decode(webauthnResp.rawId, true);
webauthnResp.response.userHandle = base64url.decode(webauthnResp.rawId, true);
webauthnResp.rawId = base64.toArrayBuffer(webauthnResp.rawId, true);
webauthnResp.response.userHandle = base64.toArrayBuffer(webauthnResp.rawId, true);
let validAuthenticators = database.users[ctx.session.username].authenticators,
winningAuthenticator;
for(let authrIdx in validAuthenticators) {
Expand Down
6 changes: 3 additions & 3 deletions utils/fido2.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const
{ Fido2Lib } = require("fido2-lib"),
base64url = require("@hexagon/base64-arraybuffer");
base64 = require("@hexagon/base64");

class Fido2 {
constructor(rpId, rpName, rpIcon, timeout) {
Expand Down Expand Up @@ -29,7 +29,7 @@ class Fido2 {

registrationOptions.status = "ok";

registrationOptions.challenge = base64url.encode(registrationOptions.challenge, true);
registrationOptions.challenge = base64.fromArrayBuffer(registrationOptions.challenge, true);

return registrationOptions;
}
Expand All @@ -46,7 +46,7 @@ class Fido2 {

async login() {
let assertionOptions = await this.f2l.assertionOptions();
assertionOptions.challenge = base64url.encode(assertionOptions.challenge, true);
assertionOptions.challenge = base64.fromArrayBuffer(assertionOptions.challenge, true);
assertionOptions.status = "ok";
return assertionOptions;
}
Expand Down
8 changes: 4 additions & 4 deletions utils/token.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const
base64 = require("@hexagon/base64-arraybuffer"),
base64 = require("@hexagon/base64"),
username = require("./username"),
crypto = require("crypto");

Expand All @@ -9,7 +9,7 @@ const validate = (usernameInput, token, tokenValidator) => {
// Try decoding token from base64url
let tokenDecoded;
try {
tokenDecoded = base64.decode(token, true);
tokenDecoded = base64.toArrayBuffer(token, true);
} catch (e) {
return false;
}
Expand All @@ -22,7 +22,7 @@ const validate = (usernameInput, token, tokenValidator) => {
return false;
} else if (tokenValidator.expires < timeNow) {
return false;
} else if (base64.encode(tokenValidator.token,true) !== base64.encode(tokenDecoded, true)) {
} else if (base64.fromArrayBuffer(tokenValidator.token,true) !== base64.fromArrayBuffer(tokenDecoded, true)) {
return false;
} else {
// Success!
Expand Down Expand Up @@ -59,7 +59,7 @@ const generate = (usernameInput, expireMs) => {

// Encode token to base64url format
const encode = (token) => {
return base64.encode(token, true);
return base64.fromArrayBuffer(token, true);
};

module.exports = {
Expand Down

0 comments on commit 075b882

Please sign in to comment.