-
Notifications
You must be signed in to change notification settings - Fork 0
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
a96da90
commit 0fde5dc
Showing
12 changed files
with
716 additions
and
441 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,35 +2,36 @@ | |
* @file hash-object.ts | ||
* @author Brandon Kalinowski | ||
* @description Take a JavaScript object and compute a sha256 hash of it for comparison. | ||
* @copyright 2020 Brandon Kalinowski | ||
* @copyright 2020-2024 Brandon Kalinowski | ||
* @license MIT | ||
*/ | ||
|
||
import { sha256 } from 'https://deno.land/x/[email protected]/mod.ts' | ||
import { sha256 } from "https://deno.land/x/[email protected]/mod.ts"; | ||
|
||
/** A JSON replacer that sorts keys recursively and removes type key when required. */ | ||
export const replacer = (key: any, value: any) => { | ||
export const replacer = (_key: any, value: any) => { | ||
if (value instanceof Object && !(value instanceof Array)) { | ||
return Object.keys(value) | ||
.sort() | ||
.reduce((sorted, key) => { | ||
;(sorted as any)[key] = value[key] | ||
return sorted | ||
}, {}) | ||
(sorted as any)[key] = value[key]; | ||
return sorted; | ||
}, {}); | ||
} | ||
return value | ||
} | ||
return value; | ||
}; | ||
|
||
/** print deterministic JSON string specifically for karabiner */ | ||
export function orderedJSONString(obj: any) { | ||
return JSON.stringify(obj, replacer).trimEnd() | ||
return JSON.stringify(obj, replacer).trimEnd(); | ||
} | ||
|
||
/** | ||
* Takes a JavaScript object and computes a sha256 hash of the deterministic JSON string. | ||
* @param obj | ||
*/ | ||
// deno-lint-ignore require-await | ||
export async function hashObject(obj: any): Promise<string> { | ||
const str = orderedJSONString(obj) | ||
return sha256(str, 'utf8', 'hex') as string | ||
const str = orderedJSONString(obj); | ||
return sha256(str, "utf8", "hex") as string; | ||
} |
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 |
---|---|---|
|
@@ -7,7 +7,7 @@ | |
* @license MIT | ||
*/ | ||
|
||
import { HmacSha256 } from "https://deno.land/std@0.92.0/hash/sha256.ts"; | ||
import { HmacSha256 } from "https://deno.land/std@0.160.0/hash/sha256.ts"; | ||
import { encodeBase64 } from "https://deno.land/[email protected]/encoding/base64.ts"; | ||
|
||
///// | ||
|
@@ -81,20 +81,20 @@ export function parseRequest( | |
req: ParseRequest, | ||
opts: parseOptions = {}, | ||
): ParsedRequest { | ||
let headers = req.headers; | ||
let requiredHeaders = opts.headers || | ||
const headers = req.headers; | ||
const requiredHeaders = opts.headers || | ||
[headers["x-date"] ? "x-date" : "date"]; | ||
const authz = headers[opts.authzHeader || ""] || | ||
headers.authorization || | ||
headers.signature; | ||
if (!authz) { | ||
let missing = opts.authzHeader | ||
const missing = opts.authzHeader | ||
? opts.authzHeader | ||
: "authorization or signature"; | ||
throw new Error(`No ${missing} header present in the request`); | ||
} | ||
opts.clockSkew = opts.clockSkew || 300; | ||
let parsed: ParsedRequest & { params: Record<string, any> } = { | ||
const parsed: ParsedRequest & { params: Record<string, any> } = { | ||
scheme: authz === headers["signature"] ? "Signature" : ("" as any), | ||
params: {} as parsedParams, | ||
signingString: "", | ||
|
@@ -104,7 +104,8 @@ export function parseRequest( | |
let tmpName = ""; | ||
let tmpValue = ""; | ||
for (let i = 0; i < authz.length; i++) { | ||
var c = authz.charAt(i); | ||
const c = authz.charAt(i); | ||
let code: number; | ||
|
||
switch (Number(state)) { | ||
case State.New: | ||
|
@@ -115,7 +116,7 @@ export function parseRequest( | |
case State.Params: | ||
switch (Number(substate)) { | ||
case ParamsState.Name: | ||
var code = c.charCodeAt(0); | ||
code = c.charCodeAt(0); | ||
// restricted name of A-Z / a-z | ||
if ( | ||
(code >= 0x41 && code <= 0x5a) || // A-Z | ||
|
@@ -207,7 +208,7 @@ export function parseRequest( | |
|
||
// Build the signingString | ||
for (let i = 0; i < parsed.params.headers.length; i++) { | ||
var h = parsed.params.headers[i].toLowerCase(); | ||
const h = parsed.params.headers[i].toLowerCase(); | ||
parsed.params.headers[i] = h; | ||
|
||
if (h === "request-line") { | ||
|
@@ -221,7 +222,7 @@ export function parseRequest( | |
} else if (h === "(algorithm)") { | ||
parsed.signingString += "(algorithm): " + parsed.params.algorithm; | ||
} else if (h === "(opaque)") { | ||
var opaque = parsed.params.opaque; | ||
const opaque = parsed.params.opaque; | ||
if (opaque === undefined) { | ||
throw new Error( | ||
"opaque param was not in the " + opts.authzHeader + | ||
|
@@ -230,7 +231,7 @@ export function parseRequest( | |
} | ||
parsed.signingString += "(opaque): " + opaque; | ||
} else { | ||
var value = headers[h]; | ||
const value = headers[h]; | ||
if (value === undefined) { | ||
throw new Error(h + " was not in the request"); | ||
} | ||
|
@@ -241,15 +242,15 @@ export function parseRequest( | |
} | ||
|
||
// Check against the constraints | ||
var date; | ||
let date: Date; | ||
if (req.headers.date || req.headers["x-date"]) { | ||
if (req.headers["x-date"]) { | ||
date = new Date(req.headers["x-date"]); | ||
} else { | ||
date = new Date(req.headers.date); | ||
} | ||
var now = new Date(); | ||
var skew = Math.abs(now.getTime() - date.getTime()); | ||
const now = new Date(); | ||
const skew = Math.abs(now.getTime() - date.getTime()); | ||
|
||
if (skew > opts.clockSkew * 1000) { | ||
throw new Error( | ||
|
Oops, something went wrong.