Skip to content

Commit 829efbd

Browse files
chore(lit-auth-client): LIT-4211 - eslint --fix and resolve tslint errors in preparation for moving providers to new auth package as authenticators
1 parent ab96720 commit 829efbd

File tree

8 files changed

+119
-58
lines changed

8 files changed

+119
-58
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"pako": "^2.1.0",
6868
"siwe": "^2.3.2",
6969
"siwe-recap": "0.0.2-alpha.0",
70+
"stytch": "^12.4.0",
7071
"tslib": "^2.7.0",
7172
"tweetnacl": "^1.0.3",
7273
"tweetnacl-util": "^0.15.1",

packages/lit-auth-client/src/lib/providers/AppleProvider.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
1-
import {
2-
AuthMethod,
3-
BaseProviderOptions,
4-
OAuthProviderOptions,
5-
} from '@lit-protocol/types';
1+
import { ethers } from 'ethers';
2+
import * as jose from 'jose';
3+
64
import {
75
AUTH_METHOD_TYPE,
86
UnauthorizedException,
97
UnknownError,
108
} from '@lit-protocol/constants';
9+
import {
10+
AuthMethod,
11+
BaseProviderOptions,
12+
OAuthProviderOptions,
13+
} from '@lit-protocol/types';
14+
1115
import {
1216
prepareLoginUrl,
1317
parseLoginParams,
1418
getStateParam,
1519
decode,
1620
} from '../utils';
1721
import { BaseProvider } from './BaseProvider';
18-
import { ethers } from 'ethers';
19-
import * as jose from 'jose';
2022

2123
export default class AppleProvider extends BaseProvider {
2224
/**

packages/lit-auth-client/src/lib/providers/BaseProvider.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import depd from 'depd';
21
import { ethers } from 'ethers';
32

43
import {
54
AUTH_METHOD_TYPE_VALUES,
65
InvalidArgumentException,
76
LitNodeClientNotReadyError,
8-
ParamsMissingError,
97
UnknownError,
108
} from '@lit-protocol/constants';
119
import { LitContracts } from '@lit-protocol/contracts-sdk';
@@ -24,8 +22,6 @@ import {
2422

2523
import { validateMintRequestBody } from '../validators';
2624

27-
const deprecated = depd('lit-js-sdk:auth-browser:base-provider');
28-
2925
export abstract class BaseProvider {
3026
/**
3127
* Relay server to subsidize minting of PKPs
@@ -65,7 +61,7 @@ export abstract class BaseProvider {
6561
*/
6662
abstract getAuthMethodId(
6763
authMethod: AuthMethod,
68-
options?: any
64+
options?: unknown
6965
): Promise<string>;
7066

7167
/**

packages/lit-auth-client/src/lib/providers/DiscordProvider.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import {
2-
AuthMethod,
3-
BaseProviderOptions,
4-
OAuthProviderOptions,
5-
} from '@lit-protocol/types';
1+
import { ethers } from 'ethers';
2+
63
import {
74
AUTH_METHOD_TYPE,
85
UnauthorizedException,
96
UnknownError,
107
} from '@lit-protocol/constants';
8+
import {
9+
AuthMethod,
10+
BaseProviderOptions,
11+
OAuthProviderOptions,
12+
} from '@lit-protocol/types';
13+
1114
import { BaseProvider } from './BaseProvider';
1215
import {
1316
prepareLoginUrl,
@@ -16,7 +19,6 @@ import {
1619
decode,
1720
LIT_LOGIN_GATEWAY,
1821
} from '../utils';
19-
import { ethers } from 'ethers';
2022

2123
export default class DiscordProvider extends BaseProvider {
2224
/**

packages/lit-auth-client/src/lib/providers/StytchAuthFactorOtp.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import {
2424
export default class StytchAuthFactorOtpProvider<
2525
T extends FactorParser
2626
> extends BaseProvider {
27-
private _params: StytchOtpProviderOptions;
2827
private _factor: T;
2928
private static _provider: string = 'https://stytch.com/session';
3029

@@ -34,7 +33,6 @@ export default class StytchAuthFactorOtpProvider<
3433
factor: T
3534
) {
3635
super(params);
37-
this._params = config;
3836
this._factor = factor;
3937
}
4038

@@ -144,7 +142,7 @@ export default class StytchAuthFactorOtpProvider<
144142
}
145143

146144
private static _resolveAuthFactor(factor: FactorParser): {
147-
parser: Function;
145+
parser: (parsedToken: StytchToken, provider: string) => string;
148146
authMethodType: AUTH_METHOD_TYPE_VALUES;
149147
} {
150148
switch (factor) {

packages/lit-auth-client/src/lib/providers/StytchAuthFactors.ts

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
import { ethers } from 'ethers';
2+
13
import { WrongParamFormat } from '@lit-protocol/constants';
24
import { StytchToken } from '@lit-protocol/types';
3-
import { ethers } from 'ethers';
5+
6+
import type {
7+
AuthenticationFactor,
8+
AuthenticatorAppFactor,
9+
EmailFactor,
10+
PhoneNumberFactor,
11+
} from 'stytch';
412

513
export type FactorParser = 'email' | 'sms' | 'whatsApp' | 'totp';
614

@@ -9,10 +17,9 @@ export const emailOtpAuthFactorParser = (
917
provider: string
1018
): string => {
1119
const session = parsedToken[provider];
12-
const authFactors: any[] = session['authentication_factors'];
13-
let authFactor = authFactors.find((value, _index, _obj) => {
14-
if (value.email_factor) return value;
15-
});
20+
const authFactors: AuthenticationFactor[] = session['authentication_factors'];
21+
22+
const authFactor = authFactors.find((value) => !!value.email_factor);
1623

1724
if (!authFactor) {
1825
throw new WrongParamFormat(
@@ -25,6 +32,9 @@ export const emailOtpAuthFactorParser = (
2532
'Could not find email authentication info in session'
2633
);
2734
}
35+
36+
const emailFactor = authFactor.email_factor as EmailFactor;
37+
2838
const audience = (parsedToken['aud'] as string[])[0];
2939
if (!audience) {
3040
throw new WrongParamFormat(
@@ -38,7 +48,7 @@ export const emailOtpAuthFactorParser = (
3848
);
3949
}
4050

41-
const userId = authFactor.email_factor.email_address;
51+
const userId = emailFactor.email_address;
4252
const authMethodId = ethers.utils.keccak256(
4353
ethers.utils.toUtf8Bytes(
4454
`${userId.toLowerCase()}:${audience.toLowerCase()}`
@@ -53,10 +63,8 @@ export const smsOtpAuthFactorParser = (
5363
provider: string
5464
): string => {
5565
const session = parsedToken[provider];
56-
const authFactors: any[] = session['authentication_factors'];
57-
let authFactor = authFactors.find((value, _index, _obj) => {
58-
if (value.phone_number_factor) return value;
59-
});
66+
const authFactors: AuthenticationFactor[] = session['authentication_factors'];
67+
const authFactor = authFactors.find((value) => !!value.phone_number_factor);
6068

6169
if (!authFactor) {
6270
throw new WrongParamFormat(
@@ -66,9 +74,12 @@ export const smsOtpAuthFactorParser = (
6674
provider,
6775
},
6876
},
69-
'Could not find email authentication info in session'
77+
'Could not find phone authentication info in session'
7078
);
7179
}
80+
81+
const phoneNumberFactor = authFactor.phone_number_factor as PhoneNumberFactor;
82+
7283
const audience = (parsedToken['aud'] as string[])[0];
7384
if (!audience) {
7485
throw new WrongParamFormat(
@@ -82,7 +93,7 @@ export const smsOtpAuthFactorParser = (
8293
);
8394
}
8495

85-
const userId = authFactor.phone_number_factor.phone_number;
96+
const userId = phoneNumberFactor.phone_number;
8697
const authMethodId = ethers.utils.keccak256(
8798
ethers.utils.toUtf8Bytes(
8899
`${userId.toLowerCase()}:${audience.toLowerCase()}`
@@ -97,10 +108,8 @@ export const whatsAppOtpAuthFactorParser = (
97108
provider: string
98109
): string => {
99110
const session = parsedToken[provider];
100-
const authFactors: any[] = session['authentication_factors'];
101-
let authFactor = authFactors.find((value, _index, _obj) => {
102-
if (value.phone_number_factor) return value;
103-
});
111+
const authFactors: AuthenticationFactor[] = session['authentication_factors'];
112+
const authFactor = authFactors.find((value) => !!value.phone_number_factor);
104113

105114
if (!authFactor) {
106115
throw new WrongParamFormat(
@@ -110,9 +119,12 @@ export const whatsAppOtpAuthFactorParser = (
110119
provider,
111120
},
112121
},
113-
'Could not find email authentication info in session'
122+
'Could not find phone authentication info in session'
114123
);
115124
}
125+
126+
const phoneNumberFactor = authFactor.phone_number_factor as PhoneNumberFactor;
127+
116128
const audience = (parsedToken['aud'] as string[])[0];
117129
if (!audience) {
118130
throw new WrongParamFormat(
@@ -126,7 +138,7 @@ export const whatsAppOtpAuthFactorParser = (
126138
);
127139
}
128140

129-
const userId = authFactor.phone_number_factor.phone_number;
141+
const userId = phoneNumberFactor.phone_number;
130142
const authMethodId = ethers.utils.keccak256(
131143
ethers.utils.toUtf8Bytes(
132144
`${userId.toLowerCase()}:${audience.toLowerCase()}`
@@ -141,10 +153,10 @@ export const totpAuthFactorParser = (
141153
provider: string
142154
): string => {
143155
const session = parsedToken[provider];
144-
const authFactors: any[] = session['authentication_factors'];
145-
let authFactor = authFactors.find((value, _index, _obj) => {
146-
if (value.phone_number_factor) return value;
147-
});
156+
const authFactors: AuthenticationFactor[] = session['authentication_factors'];
157+
const authFactor = authFactors.find(
158+
(value) => !!value.authenticator_app_factor
159+
);
148160

149161
if (!authFactor) {
150162
throw new WrongParamFormat(
@@ -154,9 +166,13 @@ export const totpAuthFactorParser = (
154166
provider,
155167
},
156168
},
157-
'Could not find email authentication info in session'
169+
'Could not find authenticator app authentication info in session'
158170
);
159171
}
172+
173+
const authenticatorAppFactor =
174+
authFactor.authenticator_app_factor as AuthenticatorAppFactor;
175+
160176
const audience = (parsedToken['aud'] as string[])[0];
161177
if (!audience) {
162178
throw new WrongParamFormat(
@@ -170,7 +186,7 @@ export const totpAuthFactorParser = (
170186
);
171187
}
172188

173-
const userId = authFactor.authenticator_app_factor.totp_id;
189+
const userId = authenticatorAppFactor.totp_id;
174190
const authMethodId = ethers.utils.keccak256(
175191
ethers.utils.toUtf8Bytes(
176192
`${userId.toLowerCase()}:${audience.toLowerCase()}`

packages/lit-auth-client/src/lib/providers/WebAuthnProvider.ts

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,26 @@
11
import {
2-
AuthMethod,
3-
BaseProviderOptions,
4-
MintRequestBody,
5-
WebAuthnProviderOptions,
6-
} from '@lit-protocol/types';
2+
PublicKeyCredentialCreationOptionsJSON,
3+
UserVerificationRequirement,
4+
RegistrationResponseJSON,
5+
} from '@simplewebauthn/typescript-types';
6+
import base64url from 'base64url';
7+
import { ethers } from 'ethers';
8+
79
import {
810
AUTH_METHOD_TYPE,
911
RemovedFunctionError,
1012
UnknownError,
1113
WrongParamFormat,
1214
} from '@lit-protocol/constants';
13-
import { ethers } from 'ethers';
1415
import {
15-
PublicKeyCredentialCreationOptionsJSON,
16-
UserVerificationRequirement,
17-
} from '@simplewebauthn/typescript-types';
18-
import base64url from 'base64url';
16+
AuthMethod,
17+
BaseProviderOptions,
18+
MintRequestBody,
19+
WebAuthnProviderOptions,
20+
} from '@lit-protocol/types';
21+
1922
import { getRPIdFromOrigin, parseAuthenticatorData } from '../utils';
2023
import { BaseProvider } from './BaseProvider';
21-
import { RegistrationResponseJSON } from '@simplewebauthn/typescript-types';
2224

2325
export default class WebAuthnProvider extends BaseProvider {
2426
/**
@@ -228,8 +230,8 @@ export default class WebAuthnProvider extends BaseProvider {
228230

229231
// Parse the buffer to reconstruct the object
230232
// Buffer is COSE formatted, utilities decode the buffer into json, and extract the public key information
231-
const authenticationResponse: any =
232-
parseAuthenticatorData(attestationBuffer);
233+
const authenticationResponse = parseAuthenticatorData(attestationBuffer);
234+
assertAuthenticationResponse(authenticationResponse);
233235

234236
// Public key in cose format to register the auth method
235237
const publicKeyCoseBuffer: Buffer = authenticationResponse
@@ -251,3 +253,34 @@ export default class WebAuthnProvider extends BaseProvider {
251253
return publicKey;
252254
}
253255
}
256+
257+
function assertAuthenticationResponse(
258+
authenticationResponse: unknown
259+
): asserts authenticationResponse is {
260+
attestedCredentialData: {
261+
credentialPublicKey: Buffer;
262+
};
263+
} {
264+
/* eslint-disable @typescript-eslint/no-explicit-any */
265+
if (
266+
typeof authenticationResponse !== 'object' ||
267+
authenticationResponse === null ||
268+
!('attestedCredentialData' in authenticationResponse) ||
269+
typeof (authenticationResponse as any).attestedCredentialData !==
270+
'object' ||
271+
(authenticationResponse as any).attestedCredentialData === null ||
272+
!(
273+
'credentialPublicKey' in
274+
(authenticationResponse as any).attestedCredentialData
275+
) ||
276+
!(
277+
(authenticationResponse as any).attestedCredentialData
278+
.credentialPublicKey instanceof Buffer
279+
)
280+
) {
281+
throw new Error(
282+
'authenticationResponse does not match the expected structure: { attestedCredentialData: { credentialPublicKey: Buffer } }'
283+
);
284+
}
285+
/* eslint-enable @typescript-eslint/no-explicit-any */
286+
}

yarn.lock

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15538,6 +15538,11 @@ jose@^4.14.4:
1553815538
resolved "https://registry.yarnpkg.com/jose/-/jose-4.15.9.tgz#9b68eda29e9a0614c042fa29387196c7dd800100"
1553915539
integrity sha512-1vUQX+IdDMVPj4k8kOxgUqlcK518yluMuGZwqlr44FS1ppZB/5GWh4rZG89erpOBOJjU/OBsnCVFfapsRz6nEA==
1554015540

15541+
jose@^5.6.3:
15542+
version "5.9.6"
15543+
resolved "https://registry.npmjs.org/jose/-/jose-5.9.6.tgz#77f1f901d88ebdc405e57cce08d2a91f47521883"
15544+
integrity sha512-AMlnetc9+CV9asI19zHmrgS/WYsWUwCn2R7RzlbJWD7F9eWYUTGyBmU9o6PxngtLGOiDGPRu+Uc4fhKzbpteZQ==
15545+
1554115546
js-levenshtein@^1.1.6:
1554215547
version "1.1.6"
1554315548
resolved "https://registry.yarnpkg.com/js-levenshtein/-/js-levenshtein-1.1.6.tgz#c6cee58eb3550372df8deb85fad5ce66ce01d59d"
@@ -21615,6 +21620,14 @@ strong-log-transformer@^2.1.0:
2161521620
minimist "^1.2.0"
2161621621
through "^2.3.4"
2161721622

21623+
stytch@^12.4.0:
21624+
version "12.4.0"
21625+
resolved "https://registry.npmjs.org/stytch/-/stytch-12.4.0.tgz#2a9dbac10e2a45057409ed1bd4f7743e1fc6cda9"
21626+
integrity sha512-jyYIfirVnhy3gAtGLEIK5c5tSp5bhi9tUE0JRzItJlwISBW/StMMOvP0hhPUb831EGjV2l1S4YRPg/NqJ+eYNg==
21627+
dependencies:
21628+
jose "^5.6.3"
21629+
undici "^6.19.5"
21630+
2161821631
superstatic@^7.1.0:
2161921632
version "7.1.0"
2162021633
resolved "https://registry.yarnpkg.com/superstatic/-/superstatic-7.1.0.tgz#42cc773a0f500fb691841e0533d0b8c31f25997f"

0 commit comments

Comments
 (0)