-
-
Notifications
You must be signed in to change notification settings - Fork 774
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
Showing
15 changed files
with
331 additions
and
27 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
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
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,99 @@ | ||
/* eslint-disable no-plusplus, no-bitwise, no-param-reassign, no-restricted-syntax */ | ||
|
||
const { createHmac } = require('crypto'); | ||
|
||
const base64url = require('./base64url'); | ||
|
||
function sixfourbeify(value) { | ||
const buf = Buffer.alloc(8); | ||
for (let i = buf.length - 1; i >= 0; i--) { | ||
buf[i] = value & 0xff; | ||
value >>= 8; | ||
} | ||
|
||
return buf; | ||
} | ||
|
||
function compute(secret, step) { | ||
return base64url.encodeBuffer(createHmac('sha256', secret).update(sixfourbeify(step)).digest()); | ||
} | ||
|
||
function compare(server, client) { | ||
let result = 0; | ||
|
||
if (server.length !== client.length) { | ||
result = 1; | ||
client = server; | ||
} | ||
|
||
for (let i = 0; i < server.length; i++) { | ||
result |= server.charCodeAt(i) ^ client.charCodeAt(i); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
const STEP = 60; | ||
|
||
module.exports = class DPoPNonces { | ||
#counter; | ||
|
||
#secret; | ||
|
||
#prevprev; | ||
|
||
#prev; | ||
|
||
#now; | ||
|
||
#next; | ||
|
||
#nextnext; | ||
|
||
constructor(secret) { | ||
if (!Buffer.isBuffer(secret) || secret.byteLength !== 32) { | ||
throw new TypeError('features.dPoP.nonceSecret must be a 32-byte Buffer instance'); | ||
} | ||
|
||
this.#secret = Uint8Array.prototype.slice.call(secret); | ||
this.#counter = Math.floor(Date.now() / 1000 / STEP); | ||
|
||
[this.#prevprev, this.#prev, this.#now, this.#next, this.#nextnext] = [ | ||
this.#counter - 2, | ||
this.#counter - 1, | ||
this.#counter, | ||
this.#counter + 1, | ||
this.#counter++ + 2, | ||
].map(compute.bind(undefined, this.#secret)); | ||
|
||
setInterval(() => { | ||
[ | ||
this.#prevprev, | ||
this.#prev, | ||
this.#now, | ||
this.#next, | ||
this.#nextnext, | ||
] = [ | ||
this.#prev, | ||
this.#now, | ||
this.#next, | ||
this.#nextnext, | ||
compute(this.#secret, this.#counter++ + 2), | ||
]; | ||
}, STEP * 1000).unref(); | ||
} | ||
|
||
nextNonce() { | ||
return this.#next; | ||
} | ||
|
||
checkNonce(nonce) { | ||
let result = 0; | ||
|
||
for (const server of [this.#prevprev, this.#prev, this.#now, this.#next, this.#nextnext]) { | ||
result ^= compare(server, nonce); | ||
} | ||
|
||
return result === 0; | ||
} | ||
}; |
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
Oops, something went wrong.