Skip to content

Commit 4e56fb3

Browse files
committed
feat: authts#4 reduce usage of any type
1 parent 703b941 commit 4e56fb3

24 files changed

Lines changed: 207 additions & 153 deletions

docs/oidc-client-ts.api.md

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ export class InMemoryWebStorage implements Storage {
4242
// (undocumented)
4343
clear(): void;
4444
// (undocumented)
45-
getItem(key: string): any;
45+
getItem(key: string): string;
4646
// (undocumented)
4747
key(index: number): string;
4848
// (undocumented)
4949
get length(): number;
5050
// (undocumented)
5151
removeItem(key: string): void;
5252
// (undocumented)
53-
setItem(key: string, value: any): void;
53+
setItem(key: string, value: string): void;
5454
}
5555

5656
// @public (undocumented)
@@ -135,7 +135,7 @@ export class OidcClient {
135135
// (undocumented)
136136
readonly metadataService: MetadataService;
137137
// (undocumented)
138-
processSigninResponse(url: string): Promise<SigninResponse>;
138+
processSigninResponse(url?: string): Promise<SigninResponse>;
139139
// (undocumented)
140140
processSignoutResponse(url: string): Promise<SignoutResponse>;
141141
// (undocumented)
@@ -241,33 +241,42 @@ export class TokenRevocationClient {
241241

242242
// @public (undocumented)
243243
export class User {
244-
constructor({ id_token, session_state, access_token, refresh_token, token_type, scope, profile, expires_at, state }: any);
244+
constructor(args: {
245+
id_token?: string;
246+
session_state?: string;
247+
access_token: string;
248+
refresh_token?: string;
249+
token_type: string;
250+
scope?: string;
251+
profile: UserProfile;
252+
expires_at?: number;
253+
});
245254
// (undocumented)
246255
access_token: string;
247256
// (undocumented)
248257
get expired(): boolean | undefined;
249258
// (undocumented)
250-
expires_at: number;
259+
expires_at: number | undefined;
251260
// (undocumented)
252261
get expires_in(): number | undefined;
253262
set expires_in(value: number | undefined);
254263
// (undocumented)
255264
static fromStorageString(storageString: string): User;
256265
// (undocumented)
257-
id_token: string;
266+
id_token: string | undefined;
267+
// Warning: (ae-forgotten-export) The symbol "UserProfile" needs to be exported by the entry point index.d.ts
268+
//
258269
// (undocumented)
259-
profile: any;
270+
profile: UserProfile;
260271
// (undocumented)
261272
refresh_token: string | undefined;
262273
// (undocumented)
263-
scope: string;
274+
scope: string | undefined;
264275
// (undocumented)
265276
get scopes(): string[];
266277
// (undocumented)
267278
session_state: string | undefined;
268279
// (undocumented)
269-
state: any | undefined;
270-
// (undocumented)
271280
token_type: string;
272281
// (undocumented)
273282
toStorageString(): string;
@@ -323,16 +332,16 @@ export class UserManager {
323332
// (undocumented)
324333
readonly settings: UserManagerSettingsStore;
325334
// Warning: (ae-forgotten-export) The symbol "INavigator" needs to be exported by the entry point index.d.ts
326-
// Warning: (ae-forgotten-export) The symbol "NavigatorParams" needs to be exported by the entry point index.d.ts
335+
// Warning: (ae-forgotten-export) The symbol "NavigateParams" needs to be exported by the entry point index.d.ts
327336
//
328337
// (undocumented)
329-
protected _signin(args: SigninArgs, navigator: INavigator, navigatorParams: NavigatorParams): Promise<User>;
338+
protected _signin(args: SigninArgs, navigator: INavigator, navigatorParams: NavigateParams): Promise<User>;
330339
// (undocumented)
331340
signinCallback(url?: string): Promise<User | null>;
332341
// (undocumented)
333342
protected _signinCallback(url: string | undefined, navigator: IFrameNavigator | PopupNavigator): Promise<void>;
334343
// (undocumented)
335-
protected _signinEnd(url: string, args?: SigninArgs): Promise<User>;
344+
protected _signinEnd(url?: string, args?: SigninArgs): Promise<User>;
336345
// (undocumented)
337346
signinPopup(): Promise<User>;
338347
// (undocumented)
@@ -349,12 +358,14 @@ export class UserManager {
349358
//
350359
// (undocumented)
351360
protected _signinSilentIframe(args: SigninArgs): Promise<User>;
361+
// Warning: (ae-forgotten-export) The symbol "NavigateResponse" needs to be exported by the entry point index.d.ts
362+
//
352363
// (undocumented)
353-
protected _signinStart(args: SigninArgs, navigator: INavigator, navigatorParams: NavigatorParams): Promise<any>;
364+
protected _signinStart(args: SigninArgs, navigator: INavigator, navigatorParams: NavigateParams): Promise<NavigateResponse>;
354365
// Warning: (ae-forgotten-export) The symbol "SignoutArgs" needs to be exported by the entry point index.d.ts
355366
//
356367
// (undocumented)
357-
protected _signout(args: SignoutArgs, navigator: INavigator, navigatorParams: NavigatorParams): Promise<SignoutResponse>;
368+
protected _signout(args: SignoutArgs, navigator: INavigator, navigatorParams: NavigateParams): Promise<SignoutResponse>;
358369
// (undocumented)
359370
signoutCallback(url?: string, keepOpen?: boolean): Promise<void>;
360371
// (undocumented)
@@ -368,7 +379,7 @@ export class UserManager {
368379
// (undocumented)
369380
signoutRedirectCallback(url?: string): Promise<SignoutResponse>;
370381
// (undocumented)
371-
protected _signoutStart(args: CreateSignoutRequestArgs | undefined, navigator: INavigator, navigatorParams?: NavigatorParams): Promise<any>;
382+
protected _signoutStart(args: CreateSignoutRequestArgs | undefined, navigator: INavigator, navigatorParams?: NavigateParams): Promise<any>;
372383
// Warning: (ae-forgotten-export) The symbol "SilentRenewService" needs to be exported by the entry point index.d.ts
373384
//
374385
// (undocumented)
@@ -390,7 +401,7 @@ export class UserManager {
390401
// (undocumented)
391402
protected get _userStoreKey(): string;
392403
// (undocumented)
393-
protected _validateIdTokenFromTokenRefreshToken(profile: any, id_token: string): Promise<void>;
404+
protected _validateIdTokenFromTokenRefreshToken(profile: UserProfile, id_token: string): Promise<void>;
394405
}
395406

396407
// @public (undocumented)

src/ErrorResponse.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class ErrorResponse extends Error {
1414
public readonly session_state: string | undefined;
1515

1616
public constructor(args: {
17-
error?: string; error_description?: string; error_uri?: string; state?: string; session_state?: string;
17+
error?: string; error_description?: string; error_uri?: string; state?: any; session_state?: string;
1818
}) {
1919
if (!args.error) {
2020
Log.error("No error passed to ErrorResponse");

src/InMemoryWebStorage.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Log } from "./utils";
77
* @public
88
*/
99
export class InMemoryWebStorage implements Storage {
10-
private _data: Record<string, any>;
10+
private _data: Record<string, string>;
1111

1212
public constructor() {
1313
this._data = {};
@@ -18,12 +18,12 @@ export class InMemoryWebStorage implements Storage {
1818
this._data = {};
1919
}
2020

21-
public getItem(key: string): any {
21+
public getItem(key: string): string {
2222
Log.debug("InMemoryWebStorage.getItem", key);
2323
return this._data[key];
2424
}
2525

26-
public setItem(key: string, value: any): void {
26+
public setItem(key: string, value: string): void {
2727
Log.debug("InMemoryWebStorage.setItem", key);
2828
this._data[key] = value;
2929
}

src/JsonService.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33

44
import { Log } from "./utils";
55

6+
export type JwtHandler = (text: string) => Promise<any>;
7+
68
export class JsonService {
79
private _contentTypes: string[];
8-
private _jwtHandler: any;
10+
private _jwtHandler: JwtHandler | null;
911

1012
public constructor(
1113
additionalContentTypes: string[] = [],
12-
jwtHandler: any = null
14+
jwtHandler: JwtHandler | null = null
1315
) {
1416
this._contentTypes = additionalContentTypes.slice();
1517
this._contentTypes.push("application/json");
@@ -42,17 +44,14 @@ export class JsonService {
4244
throw new Error("Network Error");
4345
}
4446

45-
const allowedContentTypes = this._contentTypes;
46-
const jwtHandler = this._jwtHandler;
47-
4847
Log.debug("JsonService.getJson: HTTP response received, status", response.status);
4948
if (response.status === 200) {
5049
const contentType = response.headers.get("Content-Type");
5150
if (contentType) {
52-
const found = allowedContentTypes.find(item => contentType.startsWith(item));
53-
if (found === "application/jwt") {
51+
const found = this._contentTypes.find(item => contentType.startsWith(item));
52+
if (found === "application/jwt" && this._jwtHandler) {
5453
const text = await response.text();
55-
return await jwtHandler(text);
54+
return await this._jwtHandler(text);
5655
}
5756

5857
if (found) {

src/MetadataService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ export class MetadataService {
129129
return this._signingKeys;
130130
}
131131

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

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

138138
if (!keySet.keys) {

src/OidcClient.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ export class OidcClient {
135135
return { state, response };
136136
}
137137

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

141141
const { state, response } = await this.readSigninResponseState(url, true);

src/ResponseValidator.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

4-
import { Log, JoseUtil, Timer } from "./utils";
4+
import { Log, JoseUtil, Timer, ParsedJwt } from "./utils";
55
import type { MetadataService } from "./MetadataService";
66
import { UserInfoService } from "./UserInfoService";
77
import { TokenClient } from "./TokenClient";
@@ -11,6 +11,7 @@ import type { SigninState } from "./SigninState";
1111
import type { SigninResponse } from "./SigninResponse";
1212
import type { State } from "./State";
1313
import type { SignoutResponse } from "./SignoutResponse";
14+
import type { UserProfile } from "./User";
1415

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

@@ -160,8 +161,8 @@ export class ResponseValidator {
160161
return response;
161162
}
162163

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

166167
for (const name in claims2) {
167168
let values = claims2[name];
@@ -193,10 +194,10 @@ export class ResponseValidator {
193194
return result;
194195
}
195196

196-
protected _filterProtocolClaims(claims: any): any {
197+
protected _filterProtocolClaims(claims: UserProfile): UserProfile {
197198
Log.debug("ResponseValidator._filterProtocolClaims, incoming claims:", claims);
198199

199-
const result = Object.assign({}, claims);
200+
const result = Object.assign({}, claims as Record<string, any>);
200201

201202
if (this._settings.filterProtocolClaims) {
202203
ProtocolClaims.forEach(type => {
@@ -274,10 +275,10 @@ export class ResponseValidator {
274275

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

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

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

302303
Log.debug("ResponseValidator._getSigningKeyForJwt: Received signing keys");
303-
const kid = jwt.header.kid;
304+
const kid = (jwt.header as any).kid;
304305
if (kid) {
305306
const key = keys.filter(key => key.kid === kid)[0] ?? null;
306307
return key;
@@ -317,7 +318,7 @@ export class ResponseValidator {
317318
return keys[0];
318319
}
319320

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

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

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

362363
JoseUtil.validateJwt(id_token, key, issuer, audience, clockSkewInSeconds);
363364
Log.debug("ResponseValidator._validateIdToken: JWT validation successful");

src/SessionMonitor.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import type { User } from "./User";
1212
export class SessionMonitor {
1313
private readonly _userManager: UserManager;
1414
private readonly _timer: IntervalTimer;
15-
private _sub: any;
16-
private _sid: any;
15+
private _sub: string | undefined;
16+
private _sid: string | undefined;
1717
private _checkSessionIFrame?: CheckSessionIFrame;
1818

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

128128
try {
129-
const session: any = await this._userManager.querySessionStatus();
130-
const tmpUser = {
131-
session_state: session.session_state,
132-
profile: session.sub && session.sid ? {
133-
sub: session.sub,
134-
sid: session.sid
135-
} : null
136-
};
137-
void this._start(tmpUser);
129+
const session = await this._userManager.querySessionStatus();
130+
if (session) {
131+
const tmpUser = {
132+
session_state: session.session_state,
133+
profile: session.sub && session.sid ? {
134+
sub: session.sub,
135+
sid: session.sid
136+
} : null
137+
};
138+
void this._start(tmpUser);
139+
}
138140
}
139141
catch (err) {
140142
// catch to suppress errors since we're in a callback
@@ -146,7 +148,7 @@ export class SessionMonitor {
146148

147149
protected async _callback(): Promise<void> {
148150
try {
149-
const session: any = await this._userManager.querySessionStatus();
151+
const session = await this._userManager.querySessionStatus();
150152
let raiseEvent = true;
151153

152154
if (session && this._checkSessionIFrame) {

0 commit comments

Comments
 (0)