-
Notifications
You must be signed in to change notification settings - Fork 9
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
80952e9
commit e30ccf2
Showing
4 changed files
with
147 additions
and
19 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 |
---|---|---|
|
@@ -12,6 +12,13 @@ import { TokenResponseError } from "../errors.ts"; | |
import { OIDCClientConfig } from "./oidc_client.ts"; | ||
import { IDToken, JWTPayload, OIDCTokens } from "./types.ts"; | ||
import { encode as base64Encode } from "https://deno.land/[email protected]/encoding/base64.ts"; | ||
import { | ||
isNumber, | ||
isString, | ||
isStringArray, | ||
isStringOrStringArray, | ||
optionallyIncludesClaim, | ||
} from "./validation.ts"; | ||
|
||
type ValueOrArray<T> = T | T[]; | ||
function valueOrArrayToArray<T>( | ||
|
@@ -297,19 +304,6 @@ export class AuthorizationCodeFlow extends AuthorizationCodeGrant { | |
} | ||
} | ||
|
||
function isString(v: unknown): v is string { | ||
return typeof v === "string"; | ||
} | ||
function isStringArray(v: unknown): v is string[] { | ||
return Array.isArray(v) && v.every(isString); | ||
} | ||
function isStringOrStringArray(v: unknown): v is string | string[] { | ||
return Array.isArray(v) ? v.every(isString) : isString(v); | ||
} | ||
function isNumber(v: unknown): v is number { | ||
return typeof v === "number"; | ||
} | ||
|
||
function requireIDTokenClaim< | ||
P extends Record<string, unknown>, | ||
K extends string, | ||
|
@@ -343,10 +337,7 @@ function requireOptionalIDTokenClaim< | |
isValid: (value: unknown) => value is T, | ||
tokenResponse: Response, | ||
): asserts payload is P & { [Key in K]?: T } { | ||
if (!(key in payload)) { | ||
return; | ||
} | ||
if (!isValid(payload[key])) { | ||
if (!optionallyIncludesClaim(payload, key, isValid)) { | ||
throw new TokenResponseError( | ||
`id_token contains an invalid ${key} claim`, | ||
tokenResponse, | ||
|
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,38 @@ | ||
export function isString(v: unknown): v is string { | ||
return typeof v === "string"; | ||
} | ||
export function isStringArray(v: unknown): v is string[] { | ||
return Array.isArray(v) && v.every(isString); | ||
} | ||
export function isStringOrStringArray(v: unknown): v is string | string[] { | ||
return Array.isArray(v) ? v.every(isString) : isString(v); | ||
} | ||
export function isNumber(v: unknown): v is number { | ||
return typeof v === "number"; | ||
} | ||
export function isObject(v: unknown): v is Record<string, unknown> { | ||
return typeof v === "object" && v !== null && !Array.isArray(v); | ||
} | ||
|
||
export function includesClaim< | ||
P extends Record<string, unknown>, | ||
K extends string, | ||
T, | ||
>( | ||
payload: P, | ||
key: K, | ||
isValid: (value: unknown) => value is T, | ||
): payload is P & { [Key in K]: T } { | ||
return (key in payload) && isValid(payload[key]); | ||
} | ||
export function optionallyIncludesClaim< | ||
P extends Record<string, unknown>, | ||
K extends string, | ||
T, | ||
>( | ||
payload: P, | ||
key: K, | ||
isValid: (value: unknown) => value is T, | ||
): payload is P & { [Key in K]?: T } { | ||
return !(key in payload) || isValid(payload[key]); | ||
} |