Skip to content

Commit

Permalink
refactor for linted items
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkal committed Jun 14, 2024
1 parent a96da90 commit 0fde5dc
Show file tree
Hide file tree
Showing 12 changed files with 716 additions and 441 deletions.
3 changes: 2 additions & 1 deletion deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"exclude": ["kubernetes/gen/"]
},
"lint": {
"rules": {"exclude": ["no-explicit-any"]}
"rules": {"exclude": ["no-explicit-any", "no-namespace", "require-await"]},
"exclude": ["wip", "npm"]
}
}
254 changes: 253 additions & 1 deletion deno.lock

Large diffs are not rendered by default.

23 changes: 12 additions & 11 deletions hash-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
27 changes: 14 additions & 13 deletions http-signature/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/////
Expand Down Expand Up @@ -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: "",
Expand All @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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") {
Expand All @@ -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 +
Expand All @@ -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");
}
Expand All @@ -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(
Expand Down
Loading

0 comments on commit 0fde5dc

Please sign in to comment.