Skip to content
This repository has been archived by the owner on Jan 10, 2024. It is now read-only.

Commit

Permalink
feat: #4 introduce SigningKey type
Browse files Browse the repository at this point in the history
  • Loading branch information
pamapa committed Oct 6, 2021
1 parent 0861bfe commit 63fccb1
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 15 deletions.
7 changes: 5 additions & 2 deletions docs/oidc-client-ts.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export class MetadataService {
// (undocumented)
getRevocationEndpoint(): Promise<string | undefined>;
// (undocumented)
getSigningKeys(): Promise<Record<string, string>[] | null>;
getSigningKeys(): Promise<SigningKey[] | null>;
// (undocumented)
getTokenEndpoint(optional?: boolean): Promise<string | undefined>;
// (undocumented)
Expand Down Expand Up @@ -241,7 +241,7 @@ export interface OidcClientSettings {
response_mode?: "query" | "fragment";
response_type?: string;
scope?: string;
signingKeys?: Record<string, string>[];
signingKeys?: SigningKey[];
staleStateAgeInSeconds?: number;
// Warning: (ae-forgotten-export) The symbol "StateStore" needs to be exported by the entry point index.d.ts
//
Expand Down Expand Up @@ -285,6 +285,9 @@ export interface SessionStatus {
sub?: string;
}

// @public (undocumented)
export type SigningKey = Record<string, string | string[]>;

// Warning: (ae-forgotten-export) The symbol "PopupWindowParams" needs to be exported by the entry point index.d.ts
//
// @public (undocumented)
Expand Down
6 changes: 3 additions & 3 deletions src/MetadataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import { Log } from "./utils";
import { JsonService } from "./JsonService";
import type { OidcClientSettingsStore } from "./OidcClientSettings";
import type { OidcClientSettingsStore, SigningKey } from "./OidcClientSettings";
import type { OidcMetadata } from "./OidcMetadata";

const OidcMetadataUrlPath = ".well-known/openid-configuration";
Expand All @@ -17,7 +17,7 @@ export class MetadataService {

// cache
private _metadataUrl: string | null;
private _signingKeys: Record<string, string>[] | null;
private _signingKeys: SigningKey[] | null;
private _metadata: Partial<OidcMetadata> | null;

public constructor(settings: OidcClientSettingsStore) {
Expand Down Expand Up @@ -123,7 +123,7 @@ export class MetadataService {
return metadata[name];
}

public async getSigningKeys(): Promise<Record<string, string>[] | null> {
public async getSigningKeys(): Promise<SigningKey[] | null> {
if (this._signingKeys) {
Log.debug("MetadataService.getSigningKeys: Returning signingKeys from cache");
return this._signingKeys;
Expand Down
9 changes: 7 additions & 2 deletions src/OidcClientSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ const DefaultClientAuthentication = "client_secret_post"; // The default value m
const DefaultStaleStateAgeInSeconds = 60 * 15; // seconds
const DefaultClockSkewInSeconds = 60 * 5;

/**
* @public
*/
export type SigningKey = Record<string, string | string[]>;

/**
* @public
*/
Expand All @@ -23,7 +28,7 @@ export interface OidcClientSettings {
/** Can be used to seed or add additional values to the results of the discovery request */
metadataSeed?: Partial<OidcMetadata>;
/** Provide signingKeys when authority server does not allow CORS on the jwks uri */
signingKeys?: Record<string, string>[];
signingKeys?: SigningKey[];

/** Your client application's identifier as registered with the OIDC/OAuth2 */
client_id: string;
Expand Down Expand Up @@ -70,7 +75,7 @@ export class OidcClientSettingsStore {
public readonly metadataUrl: string | undefined;
public readonly metadata: Partial<OidcMetadata> | undefined;
public readonly metadataSeed: Partial<OidcMetadata> | undefined;
public readonly signingKeys: Record<string, string>[] | undefined;
public readonly signingKeys: SigningKey[] | undefined;

// client config
public readonly client_id: string;
Expand Down
8 changes: 4 additions & 4 deletions src/ResponseValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { MetadataService } from "./MetadataService";
import { UserInfoService } from "./UserInfoService";
import { TokenClient } from "./TokenClient";
import { ErrorResponse } from "./ErrorResponse";
import type { OidcClientSettingsStore } from "./OidcClientSettings";
import type { OidcClientSettingsStore, SigningKey } from "./OidcClientSettings";
import type { SigninState } from "./SigninState";
import type { SigninResponse } from "./SigninResponse";
import type { State } from "./State";
Expand Down Expand Up @@ -293,7 +293,7 @@ export class ResponseValidator {
return response;
}

protected async _getSigningKeyForJwt(jwt: ParsedJwt): Promise<Record<string, string> | null> {
protected async _getSigningKeyForJwt(jwt: ParsedJwt): Promise<SigningKey | null> {
let keys = await this._metadataService.getSigningKeys();
if (!keys) {
Log.error("ResponseValidator._getSigningKeyForJwt: No signing keys from metadata");
Expand All @@ -317,7 +317,7 @@ export class ResponseValidator {
return keys[0];
}

protected async _getSigningKeyForJwtWithSingleRetry(jwt: ParsedJwt): Promise<Record<string, string> | null> {
protected async _getSigningKeyForJwtWithSingleRetry(jwt: ParsedJwt): Promise<SigningKey | null> {
const key = await this._getSigningKeyForJwt(jwt);
if (key) {
return key;
Expand Down Expand Up @@ -371,7 +371,7 @@ export class ResponseValidator {
return response;
}

protected _filterByAlg(keys: Record<string, string>[], alg: string): Record<string, string>[] {
protected _filterByAlg(keys: SigningKey[], alg: string): SigningKey[] {
let kty: string | null = null;
if (alg.startsWith("RS")) {
kty = "RSA";
Expand Down
6 changes: 3 additions & 3 deletions src/UserInfoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { Log, JoseUtil } from "./utils";
import { JsonService } from "./JsonService";
import type { MetadataService } from "./MetadataService";
import type { OidcClientSettingsStore } from "./OidcClientSettings";
import type { OidcClientSettingsStore, SigningKey } from "./OidcClientSettings";

export class UserInfoService {
private _settings: OidcClientSettingsStore;
Expand Down Expand Up @@ -62,7 +62,7 @@ export class UserInfoService {
}

Log.debug("UserInfoService._getClaimsFromJwt: Received signing keys");
let key: Record<string, string> | null;
let key: SigningKey | null;
if (jwt.header.kid) {
key = keys.filter(key => key.kid === jwt.header.kid)[0] ?? null;
}
Expand Down Expand Up @@ -97,7 +97,7 @@ export class UserInfoService {
}
}

protected _filterByAlg(keys: Record<string, string>[], alg: string): Record<string, string>[] {
protected _filterByAlg(keys: SigningKey[], alg: string): SigningKey[] {
let kty: string | null = null;
if (alg.startsWith("RS")) {
kty = "RSA";
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
export { Log } from "./utils";

export * from "./OidcClient";
export type { OidcClientSettings } from "./OidcClientSettings";
export type { OidcClientSettings, SigningKey } from "./OidcClientSettings";
export { WebStorageStateStore } from "./WebStorageStateStore";
export { InMemoryWebStorage } from "./InMemoryWebStorage";
export * from "./UserManager";
Expand Down

0 comments on commit 63fccb1

Please sign in to comment.