@animo-id/oauth2-utils
Advanced tools
Comparing version 0.0.2-alpha-20241106081309 to 0.0.2-alpha-20241107055836
@@ -19,2 +19,3 @@ import * as v from 'valibot'; | ||
type FetchHeaders = globalThis.Headers; | ||
type FetchRequestInit = RequestInit; | ||
@@ -121,2 +122,18 @@ declare class JsonParseError extends Error { | ||
export { type BaseSchema, type Fetch, type FetchHeaders, type FetchResponse, _Headers as Headers, type HttpMethod, type InferOutputUnion, JsonParseError, type Optional, type Simplify, _URL as URL, _URLSearchParams as URLSearchParams, type ValibotFetcher, ValidationError, addSecondsToDate, arrayEqualsIgnoreOrder, createValibotFetcher, dateToSeconds, decodeBase64, decodeUtf8String, defaultFetcher, encodeToBase64, encodeToBase64Url, encodeToUtf8String, getQueryParams, joinUriParts, mergeDeep, objectToQueryParams, parseWithErrorHandling, stringToJsonWithErrorHandling, vHttpMethod, vHttpsUrl, vInteger, valibotRecursiveFlattenIssues }; | ||
interface WwwAuthenticateHeaderChallenge { | ||
scheme: string; | ||
/** | ||
* Record where the keys are the names, and the value can be 0 (null), 1 (string) or multiple (string[]) | ||
* entries | ||
*/ | ||
payload: Record<string, string | string[] | null>; | ||
} | ||
declare function parseWwwAuthenticateHeader(str: string): WwwAuthenticateHeaderChallenge[]; | ||
declare function encodeWwwAuthenticateHeader(challenges: WwwAuthenticateHeaderChallenge[]): string; | ||
declare enum ContentType { | ||
XWwwFormUrlencoded = "application/x-www-form-urlencoded", | ||
Json = "application/json" | ||
} | ||
export { type BaseSchema, ContentType, type Fetch, type FetchHeaders, type FetchRequestInit, type FetchResponse, _Headers as Headers, type HttpMethod, type InferOutputUnion, JsonParseError, type Optional, type Simplify, _URL as URL, _URLSearchParams as URLSearchParams, type ValibotFetcher, ValidationError, type WwwAuthenticateHeaderChallenge, addSecondsToDate, arrayEqualsIgnoreOrder, createValibotFetcher, dateToSeconds, decodeBase64, decodeUtf8String, defaultFetcher, encodeToBase64, encodeToBase64Url, encodeToUtf8String, encodeWwwAuthenticateHeader, getQueryParams, joinUriParts, mergeDeep, objectToQueryParams, parseWithErrorHandling, parseWwwAuthenticateHeader, stringToJsonWithErrorHandling, vHttpMethod, vHttpsUrl, vInteger, valibotRecursiveFlattenIssues }; |
@@ -33,2 +33,3 @@ "use strict"; | ||
__export(src_exports, { | ||
ContentType: () => ContentType, | ||
Headers: () => _Headers, | ||
@@ -49,2 +50,3 @@ JsonParseError: () => JsonParseError, | ||
encodeToUtf8String: () => encodeToUtf8String, | ||
encodeWwwAuthenticateHeader: () => encodeWwwAuthenticateHeader, | ||
getQueryParams: () => getQueryParams, | ||
@@ -55,2 +57,3 @@ joinUriParts: () => joinUriParts, | ||
parseWithErrorHandling: () => parseWithErrorHandling, | ||
parseWwwAuthenticateHeader: () => parseWwwAuthenticateHeader, | ||
stringToJsonWithErrorHandling: () => stringToJsonWithErrorHandling, | ||
@@ -208,2 +211,11 @@ vHttpMethod: () => vHttpMethod, | ||
var v2 = __toESM(require("valibot")); | ||
// src/content-type.ts | ||
var ContentType = /* @__PURE__ */ ((ContentType2) => { | ||
ContentType2["XWwwFormUrlencoded"] = "application/x-www-form-urlencoded"; | ||
ContentType2["Json"] = "application/json"; | ||
return ContentType2; | ||
})(ContentType || {}); | ||
// src/valibot-fetcher.ts | ||
var defaultFetcher = fetch; | ||
@@ -215,3 +227,3 @@ function createValibotFetcher(fetcher = defaultFetcher) { | ||
response, | ||
result: response.ok ? v2.safeParse(schema, await response.json()) : void 0 | ||
result: response.ok && response.headers.get("Content-Type") === "application/json" /* Json */ ? v2.safeParse(schema, await response.json()) : void 0 | ||
}; | ||
@@ -233,4 +245,69 @@ }; | ||
} | ||
// src/www-authenticate.ts | ||
var unquote = (value) => value.substring(1, value.length - 1).replace(/\\"/g, '"'); | ||
var sanitize = (value) => value.charAt(0) === '"' ? unquote(value) : value.trim(); | ||
var body = ( | ||
// biome-ignore lint/suspicious/noControlCharactersInRegex: <explanation> | ||
/((?:[a-zA-Z0-9._~+\/-]+=*(?:\s+|$))|[^\u0000-\u001F\u007F()<>@,;:\\"/?={}\[\]\u0020\u0009]+)(?:=([^\\"=\s,]+|"(?:[^"\\]|\\.)*"))?/g | ||
); | ||
var parsePayload = (scheme, string2) => { | ||
const payload = {}; | ||
while (true) { | ||
const res = body.exec(string2); | ||
if (!res) break; | ||
const [, key, newValue] = res; | ||
const payloadValue = payload[key]; | ||
if (newValue) { | ||
const sanitizedValue = sanitize(newValue); | ||
payload[key] = payloadValue ? Array.isArray(payloadValue) ? [...payloadValue, sanitizedValue] : [payloadValue, sanitizedValue] : sanitizedValue; | ||
} else if (!payloadValue) { | ||
payload[key] = null; | ||
} | ||
} | ||
return { scheme, payload }; | ||
}; | ||
function parseWwwAuthenticateHeader(str) { | ||
const start = str.indexOf(" "); | ||
let scheme = str.substring(0, start); | ||
let value = str.substring(start); | ||
const challenges = []; | ||
const endsWithSchemeRegex = /, ?(Bearer|DPoP|Basic)$/; | ||
const endsWithSchemeTest = endsWithSchemeRegex.exec(value); | ||
let endsWithScheme = void 0; | ||
if (endsWithSchemeTest) { | ||
value = value.substring(0, value.length - endsWithSchemeTest[0].length); | ||
endsWithScheme = endsWithSchemeTest[1]; | ||
} | ||
const additionalSchemesRegex = /(.*?)(, ?)(Bearer|DPoP|Basic)[, ]/; | ||
let match = additionalSchemesRegex.exec(value); | ||
while (match) { | ||
challenges.push(parsePayload(scheme, match[1])); | ||
value = value.substring(match[0].length - 1); | ||
scheme = match[3]; | ||
match = additionalSchemesRegex.exec(value); | ||
} | ||
challenges.push(parsePayload(scheme, value)); | ||
if (endsWithScheme) { | ||
challenges.push({ scheme: endsWithScheme, payload: {} }); | ||
} | ||
return challenges; | ||
} | ||
function encodeWwwAuthenticateHeader(challenges) { | ||
const entries = []; | ||
for (const challenge of challenges) { | ||
const encodedParams = Object.entries(challenge.payload).flatMap(([key, value]) => { | ||
const encode = (s) => s.replace(/\\/g, "\\\\").replace(/"/g, '\\"'); | ||
if (Array.isArray(value)) { | ||
return value.map((v4) => `${key}="${encode(v4)}"`); | ||
} | ||
return value ? `${key}="${encode(value)}"` : key; | ||
}); | ||
entries.push(encodedParams.length === 0 ? challenge.scheme : `${challenge.scheme} ${encodedParams.join(", ")}`); | ||
} | ||
return entries.join(", "); | ||
} | ||
// Annotate the CommonJS export names for ESM import in node: | ||
0 && (module.exports = { | ||
ContentType, | ||
Headers, | ||
@@ -251,2 +328,3 @@ JsonParseError, | ||
encodeToUtf8String, | ||
encodeWwwAuthenticateHeader, | ||
getQueryParams, | ||
@@ -257,2 +335,3 @@ joinUriParts, | ||
parseWithErrorHandling, | ||
parseWwwAuthenticateHeader, | ||
stringToJsonWithErrorHandling, | ||
@@ -259,0 +338,0 @@ vHttpMethod, |
{ | ||
"name": "@animo-id/oauth2-utils", | ||
"version": "0.0.2-alpha-20241106081309", | ||
"version": "0.0.2-alpha-20241107055836", | ||
"exports": { | ||
@@ -5,0 +5,0 @@ ".": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
70957
695