@criipto/oidc
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -13,2 +13,21 @@ export { generate as generatePKCE } from './pkce'; | ||
}): URL; | ||
export declare type AuthorizeURLOptions = { | ||
redirect_uri: string; | ||
response_type: string; | ||
response_mode: string; | ||
acr_values?: string | string[]; | ||
code_challenge_method?: string; | ||
code_challenge?: string; | ||
state?: string; | ||
login_hint?: string; | ||
ui_locales?: string; | ||
scope: string; | ||
prompt?: string; | ||
nonce?: string; | ||
}; | ||
export declare function buildAuthorizeURL(configuration: OpenIDConfiguration, options: AuthorizeURLOptions): URL; | ||
export declare function parseAuthorizeOptionsFromUrl(input: string | URL): Partial<AuthorizeURLOptions> & { | ||
domain: string; | ||
client_id: string; | ||
}; | ||
export declare function codeExchange(configuration: OpenIDConfiguration, options: { | ||
@@ -15,0 +34,0 @@ code: string; |
@@ -12,3 +12,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.codeExchange = exports.buildLogoutURL = exports.OpenIDConfigurationManager = exports.parseURLResponse = exports.parseQueryResponse = exports.generatePKCE = void 0; | ||
exports.codeExchange = exports.parseAuthorizeOptionsFromUrl = exports.buildAuthorizeURL = exports.buildLogoutURL = exports.OpenIDConfigurationManager = exports.parseURLResponse = exports.parseQueryResponse = exports.generatePKCE = void 0; | ||
var pkce_1 = require("./pkce"); | ||
@@ -29,2 +29,36 @@ Object.defineProperty(exports, "generatePKCE", { enumerable: true, get: function () { return pkce_1.generate; } }); | ||
exports.buildLogoutURL = buildLogoutURL; | ||
function buildAuthorizeURL(configuration, options) { | ||
const url = new URL(configuration.authorization_endpoint); | ||
for (const [k, v] of Object.entries(options)) { | ||
if (k === 'acr_values') | ||
continue; | ||
url.searchParams.set(k, v); | ||
} | ||
if (options.acr_values) { | ||
url.searchParams.set('acr_values', Array.isArray(options.acr_values) ? options.acr_values.join(' ') : options.acr_values); | ||
} | ||
return url; | ||
} | ||
exports.buildAuthorizeURL = buildAuthorizeURL; | ||
function parseAuthorizeOptionsFromUrl(input) { | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o; | ||
const url = typeof input === "string" ? new URL(input) : input; | ||
return { | ||
domain: url.host, | ||
client_id: url.searchParams.get('client_id'), | ||
acr_values: (_b = (_a = url.searchParams.get('acr_values')) === null || _a === void 0 ? void 0 : _a.split(' ')) !== null && _b !== void 0 ? _b : undefined, | ||
redirect_uri: (_c = url.searchParams.get('redirect_uri')) !== null && _c !== void 0 ? _c : undefined, | ||
response_type: (_d = url.searchParams.get('response_type')) !== null && _d !== void 0 ? _d : undefined, | ||
response_mode: (_e = url.searchParams.get('response_mode')) !== null && _e !== void 0 ? _e : undefined, | ||
code_challenge: (_f = url.searchParams.get('code_challenge')) !== null && _f !== void 0 ? _f : undefined, | ||
code_challenge_method: (_g = url.searchParams.get('code_challenge_method')) !== null && _g !== void 0 ? _g : undefined, | ||
state: (_h = url.searchParams.get('state')) !== null && _h !== void 0 ? _h : undefined, | ||
login_hint: (_j = url.searchParams.get('login_hint')) !== null && _j !== void 0 ? _j : undefined, | ||
ui_locales: (_k = url.searchParams.get('ui_locales')) !== null && _k !== void 0 ? _k : undefined, | ||
scope: (_l = url.searchParams.get('scope')) !== null && _l !== void 0 ? _l : undefined, | ||
nonce: (_m = url.searchParams.get('nonce')) !== null && _m !== void 0 ? _m : undefined, | ||
prompt: (_o = url.searchParams.get('prompt')) !== null && _o !== void 0 ? _o : undefined | ||
}; | ||
} | ||
exports.parseAuthorizeOptionsFromUrl = parseAuthorizeOptionsFromUrl; | ||
function codeExchange(configuration, options) { | ||
@@ -31,0 +65,0 @@ return __awaiter(this, void 0, void 0, function* () { |
@@ -15,7 +15,15 @@ export declare type OpenIDConfiguration = { | ||
}; | ||
export declare class Cache<T> { | ||
storage: Storage; | ||
lifetimeMs: number; | ||
constructor(storage: Storage, lifetimeMs: number); | ||
add(key: string, entry: T): void; | ||
get(key: string): T | null; | ||
} | ||
export declare class OpenIDConfigurationManager { | ||
authority: string; | ||
clientID: string; | ||
constructor(authority: string, clientID: string); | ||
cache: Cache<OpenIDConfiguration> | undefined; | ||
constructor(authority: string, clientID: string, cacheStorage?: Storage); | ||
fetch(): Promise<OpenIDConfiguration>; | ||
} |
@@ -12,5 +12,30 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.OpenIDConfigurationManager = void 0; | ||
exports.OpenIDConfigurationManager = exports.Cache = void 0; | ||
class Cache { | ||
constructor(storage, lifetimeMs) { | ||
this.lifetimeMs = lifetimeMs; | ||
this.storage = storage; | ||
} | ||
add(key, entry) { | ||
const cacheEntry = { | ||
cachedAt: (new Date()).toJSON(), | ||
entry | ||
}; | ||
this.storage.setItem(key, JSON.stringify(cacheEntry)); | ||
} | ||
get(key) { | ||
const cacheEntryCandidate = this.storage.getItem(key); | ||
if (!cacheEntryCandidate) | ||
return null; | ||
const cacheEntry = JSON.parse(cacheEntryCandidate); | ||
const cachedAt = new Date(cacheEntry.cachedAt); | ||
const isExpired = (cachedAt.valueOf() + this.lifetimeMs) >= Date.now(); | ||
if (isExpired) | ||
return null; | ||
return cacheEntry.entry; | ||
} | ||
} | ||
exports.Cache = Cache; | ||
class OpenIDConfigurationManager { | ||
constructor(authority, clientID) { | ||
constructor(authority, clientID, cacheStorage) { | ||
if (!authority.startsWith('http')) | ||
@@ -20,8 +45,15 @@ throw new Error(`OpenIDConfigurationManager authority should start with https://`); | ||
this.clientID = clientID; | ||
this.cache = cacheStorage ? new Cache(cacheStorage, 60000) : undefined; | ||
} | ||
fetch() { | ||
var _a, _b; | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const response = yield fetch(`${this.authority}/.well-known/openid-configuration?client_id=${this.clientID}`); | ||
const url = `${this.authority}/.well-known/openid-configuration?client_id=${this.clientID}`; | ||
const cacheEntry = (_a = this.cache) === null || _a === void 0 ? void 0 : _a.get(url); | ||
if (cacheEntry) | ||
return cacheEntry; | ||
const response = yield fetch(url); | ||
const metadata = yield response.json(); | ||
metadata.client_id = this.clientID; | ||
(_b = this.cache) === null || _b === void 0 ? void 0 : _b.add(url, metadata); | ||
return metadata; | ||
@@ -28,0 +60,0 @@ }); |
{ | ||
"name": "@criipto/oidc", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
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
23134
321