Skip to content

Commit

Permalink
Merge pull request authts#72 from pamapa/reduce-any-types
Browse files Browse the repository at this point in the history
Reduce any types
  • Loading branch information
PascalLuginbuehl authored Sep 27, 2021
2 parents 9edebc5 + 4e56fb3 commit b83cd6f
Show file tree
Hide file tree
Showing 24 changed files with 207 additions and 155 deletions.
45 changes: 28 additions & 17 deletions docs/oidc-client-ts.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ export class InMemoryWebStorage implements Storage {
// (undocumented)
clear(): void;
// (undocumented)
getItem(key: string): any;
getItem(key: string): string;
// (undocumented)
key(index: number): string;
// (undocumented)
get length(): number;
// (undocumented)
removeItem(key: string): void;
// (undocumented)
setItem(key: string, value: any): void;
setItem(key: string, value: string): void;
}

// @public (undocumented)
Expand Down Expand Up @@ -135,7 +135,7 @@ export class OidcClient {
// (undocumented)
readonly metadataService: MetadataService;
// (undocumented)
processSigninResponse(url: string): Promise<SigninResponse>;
processSigninResponse(url?: string): Promise<SigninResponse>;
// (undocumented)
processSignoutResponse(url: string): Promise<SignoutResponse>;
// (undocumented)
Expand Down Expand Up @@ -241,33 +241,42 @@ export class TokenRevocationClient {

// @public (undocumented)
export class User {
constructor({ id_token, session_state, access_token, refresh_token, token_type, scope, profile, expires_at, state }: any);
constructor(args: {
id_token?: string;
session_state?: string;
access_token: string;
refresh_token?: string;
token_type: string;
scope?: string;
profile: UserProfile;
expires_at?: number;
});
// (undocumented)
access_token: string;
// (undocumented)
get expired(): boolean | undefined;
// (undocumented)
expires_at: number;
expires_at: number | undefined;
// (undocumented)
get expires_in(): number | undefined;
set expires_in(value: number | undefined);
// (undocumented)
static fromStorageString(storageString: string): User;
// (undocumented)
id_token: string;
id_token: string | undefined;
// Warning: (ae-forgotten-export) The symbol "UserProfile" needs to be exported by the entry point index.d.ts
//
// (undocumented)
profile: any;
profile: UserProfile;
// (undocumented)
refresh_token: string | undefined;
// (undocumented)
scope: string;
scope: string | undefined;
// (undocumented)
get scopes(): string[];
// (undocumented)
session_state: string | undefined;
// (undocumented)
state: any | undefined;
// (undocumented)
token_type: string;
// (undocumented)
toStorageString(): string;
Expand Down Expand Up @@ -323,16 +332,16 @@ export class UserManager {
// (undocumented)
readonly settings: UserManagerSettingsStore;
// Warning: (ae-forgotten-export) The symbol "INavigator" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "NavigatorParams" needs to be exported by the entry point index.d.ts
// Warning: (ae-forgotten-export) The symbol "NavigateParams" needs to be exported by the entry point index.d.ts
//
// (undocumented)
protected _signin(args: SigninArgs, navigator: INavigator, navigatorParams: NavigatorParams): Promise<User>;
protected _signin(args: SigninArgs, navigator: INavigator, navigatorParams: NavigateParams): Promise<User>;
// (undocumented)
signinCallback(url?: string): Promise<User | null>;
// (undocumented)
protected _signinCallback(url: string | undefined, navigator: IFrameNavigator | PopupNavigator): Promise<void>;
// (undocumented)
protected _signinEnd(url: string, args?: SigninArgs): Promise<User>;
protected _signinEnd(url?: string, args?: SigninArgs): Promise<User>;
// (undocumented)
signinPopup(): Promise<User>;
// (undocumented)
Expand All @@ -349,12 +358,14 @@ export class UserManager {
//
// (undocumented)
protected _signinSilentIframe(args: SigninArgs): Promise<User>;
// Warning: (ae-forgotten-export) The symbol "NavigateResponse" needs to be exported by the entry point index.d.ts
//
// (undocumented)
protected _signinStart(args: SigninArgs, navigator: INavigator, navigatorParams: NavigatorParams): Promise<any>;
protected _signinStart(args: SigninArgs, navigator: INavigator, navigatorParams: NavigateParams): Promise<NavigateResponse>;
// Warning: (ae-forgotten-export) The symbol "SignoutArgs" needs to be exported by the entry point index.d.ts
//
// (undocumented)
protected _signout(args: SignoutArgs, navigator: INavigator, navigatorParams: NavigatorParams): Promise<SignoutResponse>;
protected _signout(args: SignoutArgs, navigator: INavigator, navigatorParams: NavigateParams): Promise<SignoutResponse>;
// (undocumented)
signoutCallback(url?: string, keepOpen?: boolean): Promise<void>;
// (undocumented)
Expand All @@ -368,7 +379,7 @@ export class UserManager {
// (undocumented)
signoutRedirectCallback(url?: string): Promise<SignoutResponse>;
// (undocumented)
protected _signoutStart(args: CreateSignoutRequestArgs | undefined, navigator: INavigator, navigatorParams?: NavigatorParams): Promise<any>;
protected _signoutStart(args: CreateSignoutRequestArgs | undefined, navigator: INavigator, navigatorParams?: NavigateParams): Promise<any>;
// Warning: (ae-forgotten-export) The symbol "SilentRenewService" needs to be exported by the entry point index.d.ts
//
// (undocumented)
Expand All @@ -390,7 +401,7 @@ export class UserManager {
// (undocumented)
protected get _userStoreKey(): string;
// (undocumented)
protected _validateIdTokenFromTokenRefreshToken(profile: any, id_token: string): Promise<void>;
protected _validateIdTokenFromTokenRefreshToken(profile: UserProfile, id_token: string): Promise<void>;
}

// @public (undocumented)
Expand Down
2 changes: 1 addition & 1 deletion src/ErrorResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class ErrorResponse extends Error {
public readonly session_state: string | undefined;

public constructor(args: {
error?: string; error_description?: string; error_uri?: string; state?: string; session_state?: string;
error?: string; error_description?: string; error_uri?: string; state?: any; session_state?: string;
}) {
if (!args.error) {
Log.error("No error passed to ErrorResponse");
Expand Down
6 changes: 3 additions & 3 deletions src/InMemoryWebStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Log } from "./utils";
* @public
*/
export class InMemoryWebStorage implements Storage {
private _data: Record<string, any>;
private _data: Record<string, string>;

public constructor() {
this._data = {};
Expand All @@ -18,12 +18,12 @@ export class InMemoryWebStorage implements Storage {
this._data = {};
}

public getItem(key: string): any {
public getItem(key: string): string {
Log.debug("InMemoryWebStorage.getItem", key);
return this._data[key];
}

public setItem(key: string, value: any): void {
public setItem(key: string, value: string): void {
Log.debug("InMemoryWebStorage.setItem", key);
this._data[key] = value;
}
Expand Down
15 changes: 7 additions & 8 deletions src/JsonService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

import { Log } from "./utils";

export type JwtHandler = (text: string) => Promise<any>;

export class JsonService {
private _contentTypes: string[];
private _jwtHandler: any;
private _jwtHandler: JwtHandler | null;

public constructor(
additionalContentTypes: string[] = [],
jwtHandler: any = null
jwtHandler: JwtHandler | null = null
) {
this._contentTypes = additionalContentTypes.slice();
this._contentTypes.push("application/json");
Expand Down Expand Up @@ -42,17 +44,14 @@ export class JsonService {
throw new Error("Network Error");
}

const allowedContentTypes = this._contentTypes;
const jwtHandler = this._jwtHandler;

Log.debug("JsonService.getJson: HTTP response received, status", response.status);
if (response.status === 200) {
const contentType = response.headers.get("Content-Type");
if (contentType) {
const found = allowedContentTypes.find(item => contentType.startsWith(item));
if (found === "application/jwt") {
const found = this._contentTypes.find(item => contentType.startsWith(item));
if (found === "application/jwt" && this._jwtHandler) {
const text = await response.text();
return await jwtHandler(text);
return await this._jwtHandler(text);
}

if (found) {
Expand Down
4 changes: 2 additions & 2 deletions src/MetadataService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,10 @@ export class MetadataService {
return this._signingKeys;
}

const jwks_uri = await this.getKeysEndpoint(false);
const jwks_uri = await this.getKeysEndpoint(false) as string;
Log.debug("MetadataService.getSigningKeys: jwks_uri received", jwks_uri);

const keySet = await this._jsonService.getJson(jwks_uri as string);
const keySet = await this._jsonService.getJson(jwks_uri);
Log.debug("MetadataService.getSigningKeys: key set received", keySet);

if (!keySet.keys) {
Expand Down
2 changes: 1 addition & 1 deletion src/OidcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export class OidcClient {
return { state, response };
}

public async processSigninResponse(url: string): Promise<SigninResponse> {
public async processSigninResponse(url?: string): Promise<SigninResponse> {
Log.debug("OidcClient.processSigninResponse");

const { state, response } = await this.readSigninResponseState(url, true);
Expand Down
25 changes: 13 additions & 12 deletions src/ResponseValidator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { Log, JoseUtil, Timer } from "./utils";
import { Log, JoseUtil, Timer, ParsedJwt } from "./utils";
import type { MetadataService } from "./MetadataService";
import { UserInfoService } from "./UserInfoService";
import { TokenClient } from "./TokenClient";
Expand All @@ -11,6 +11,7 @@ import type { SigninState } from "./SigninState";
import type { SigninResponse } from "./SigninResponse";
import type { State } from "./State";
import type { SignoutResponse } from "./SignoutResponse";
import type { UserProfile } from "./User";

const ProtocolClaims = ["nonce", "at_hash", "iat", "nbf", "exp", "aud", "iss", "c_hash"];

Expand Down Expand Up @@ -160,8 +161,8 @@ export class ResponseValidator {
return response;
}

protected _mergeClaims(claims1: any, claims2: any): any {
const result = Object.assign({}, claims1);
protected _mergeClaims(claims1: UserProfile, claims2: any): UserProfile {
const result = Object.assign({}, claims1 as Record<string, any>);

for (const name in claims2) {
let values = claims2[name];
Expand Down Expand Up @@ -193,10 +194,10 @@ export class ResponseValidator {
return result;
}

protected _filterProtocolClaims(claims: any): any {
protected _filterProtocolClaims(claims: UserProfile): UserProfile {
Log.debug("ResponseValidator._filterProtocolClaims, incoming claims:", claims);

const result = Object.assign({}, claims);
const result = Object.assign({}, claims as Record<string, any>);

if (this._settings.filterProtocolClaims) {
ProtocolClaims.forEach(type => {
Expand Down Expand Up @@ -274,10 +275,10 @@ export class ResponseValidator {

const audience = state.client_id;
const clockSkewInSeconds = this._settings.clockSkewInSeconds;
Log.debug("ResponseValidator._validateIdTokenAttributes: Validaing JWT attributes; using clock skew (in seconds) of: ", clockSkewInSeconds);
Log.debug("ResponseValidator._validateIdTokenAttributes: Validating JWT attributes; using clock skew (in seconds) of: ", clockSkewInSeconds);

const now = Timer.getEpochTime();
const payload = await JoseUtil.validateJwtAttributes(id_token, issuer, audience, clockSkewInSeconds, now);
const payload = JoseUtil.validateJwtAttributes(id_token, issuer, audience, clockSkewInSeconds, now);
if (state.nonce && state.nonce !== payload.nonce) {
Log.error("ResponseValidator._validateIdTokenAttributes: Invalid nonce in id_token");
throw new Error("Invalid nonce in id_token");
Expand All @@ -292,15 +293,15 @@ export class ResponseValidator {
return response;
}

protected async _getSigningKeyForJwt(jwt: any): Promise<Record<string, string> | null> {
protected async _getSigningKeyForJwt(jwt: ParsedJwt): Promise<Record<string, string> | null> {
let keys = await this._metadataService.getSigningKeys();
if (!keys) {
Log.error("ResponseValidator._getSigningKeyForJwt: No signing keys from metadata");
throw new Error("No signing keys from metadata");
}

Log.debug("ResponseValidator._getSigningKeyForJwt: Received signing keys");
const kid = jwt.header.kid;
const kid = (jwt.header as any).kid;
if (kid) {
const key = keys.filter(key => key.kid === kid)[0] ?? null;
return key;
Expand All @@ -317,7 +318,7 @@ export class ResponseValidator {
return keys[0];
}

protected async _getSigningKeyForJwtWithSingleRetry(jwt: any): Promise<Record<string, string> | null> {
protected async _getSigningKeyForJwtWithSingleRetry(jwt: ParsedJwt): Promise<Record<string, string> | null> {
const key = await this._getSigningKeyForJwt(jwt);
if (key) {
return key;
Expand All @@ -341,7 +342,7 @@ export class ResponseValidator {
throw new Error("Failed to parse id_token");
}

const payload: any = jwt.payload;
const payload = jwt.payload;
if (state.nonce !== payload.nonce) {
Log.error("ResponseValidator._validateIdToken: Invalid nonce in id_token");
throw new Error("Invalid nonce in id_token");
Expand All @@ -357,7 +358,7 @@ export class ResponseValidator {

const audience = state.client_id;
const clockSkewInSeconds = this._settings.clockSkewInSeconds;
Log.debug("ResponseValidator._validateIdToken: Validaing JWT; using clock skew (in seconds) of: ", clockSkewInSeconds);
Log.debug("ResponseValidator._validateIdToken: Validating JWT; using clock skew (in seconds) of: ", clockSkewInSeconds);

JoseUtil.validateJwt(id_token, key, issuer, audience, clockSkewInSeconds);
Log.debug("ResponseValidator._validateIdToken: JWT validation successful");
Expand Down
26 changes: 14 additions & 12 deletions src/SessionMonitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import type { User } from "./User";
export class SessionMonitor {
private readonly _userManager: UserManager;
private readonly _timer: IntervalTimer;
private _sub: any;
private _sid: any;
private _sub: string | undefined;
private _sid: string | undefined;
private _checkSessionIFrame?: CheckSessionIFrame;

public constructor(userManager: UserManager) {
Expand Down Expand Up @@ -126,15 +126,17 @@ export class SessionMonitor {
this._timer.clearInterval(timerHandle);

try {
const session: any = await this._userManager.querySessionStatus();
const tmpUser = {
session_state: session.session_state,
profile: session.sub && session.sid ? {
sub: session.sub,
sid: session.sid
} : null
};
void this._start(tmpUser);
const session = await this._userManager.querySessionStatus();
if (session) {
const tmpUser = {
session_state: session.session_state,
profile: session.sub && session.sid ? {
sub: session.sub,
sid: session.sid
} : null
};
void this._start(tmpUser);
}
}
catch (err) {
// catch to suppress errors since we're in a callback
Expand All @@ -146,7 +148,7 @@ export class SessionMonitor {

protected async _callback(): Promise<void> {
try {
const session: any = await this._userManager.querySessionStatus();
const session = await this._userManager.querySessionStatus();
let raiseEvent = true;

if (session && this._checkSessionIFrame) {
Expand Down
Loading

0 comments on commit b83cd6f

Please sign in to comment.