Skip to content

Commit

Permalink
🔄 synced local 'src/' with remote 'src/'
Browse files Browse the repository at this point in the history
  • Loading branch information
circle-github-action-bot committed Aug 30, 2024
1 parent c5399b9 commit a5bc1b7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 52 deletions.
4 changes: 2 additions & 2 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('W3SSdk', () => {
expect(addEventListenerSpy).toHaveBeenCalledWith(
'message',
expect.any(Function),
false
false,
)
})

Expand Down Expand Up @@ -97,7 +97,7 @@ describe('W3SSdk', () => {
code: 1,
message: 'Some error',
} as Error,
undefined
undefined,
)
})
})
66 changes: 33 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import {
import { decode } from 'jsonwebtoken'
import { v4 as uuidv4 } from 'uuid'

import packageInfo from '../package.json'

import { SocialLoginProvider } from './types'

import type {
Expand All @@ -43,12 +45,10 @@ import type { FirebaseApp } from 'firebase/app'
import type { UserCredential } from 'firebase/auth'
import type { JwtPayload } from 'jsonwebtoken'

const packageInfo = require('../package.json') as { version: string }

export class W3SSdk {
private readonly serviceUrl = 'https://pw-auth.circle.com'
private static instance: W3SSdk | null = null
private readonly iframe: HTMLIFrameElement
private readonly iframe!: HTMLIFrameElement
private readonly window: Window = window
private configs?: Configs
private challenge?: Challenge
Expand Down Expand Up @@ -181,7 +181,7 @@ export class W3SSdk {
code: 155140,
message: 'Invalid social login provider',
},
undefined
undefined,
)
}
}
Expand All @@ -191,7 +191,7 @@ export class W3SSdk {
*/
verifyOtp() {
this.subscribeMessage()
this.appendIframe(true, 'sso/verify-email')
this.appendIframe(true, 'social/verify-email')

setTimeout(() => {
if (!this.receivedResponseFromService) {
Expand All @@ -200,7 +200,7 @@ export class W3SSdk {
code: 155706,
message: 'Network error',
},
undefined
undefined,
)
}
}, 1000 * 10)
Expand All @@ -226,7 +226,7 @@ export class W3SSdk {
setCustomSecurityQuestions(
questions?: SecurityQuestion[] | null,
requiredCount = 2,
securityConfirmItems?: string[]
securityConfirmItems?: string[],
): void {
this.securityQuestions = questions
this.securityConfirmItems = securityConfirmItems
Expand Down Expand Up @@ -277,7 +277,7 @@ export class W3SSdk {
*/
setOnForgotPin(
onForgotPin: () => void,
shouldCloseModalOnForgotPin = false
shouldCloseModalOnForgotPin = false,
): void {
this.shouldCloseModalOnForgotPin = shouldCloseModalOnForgotPin

Expand Down Expand Up @@ -305,7 +305,7 @@ export class W3SSdk {
*/
private setupInstance(
configs?: Configs,
onLoginComplete?: LoginCompleteCallback
onLoginComplete?: LoginCompleteCallback,
) {
if (configs?.loginConfigs?.apple && getApps().length === 0) {
this.firebaseApp = initializeApp(configs.loginConfigs.apple)
Expand Down Expand Up @@ -375,7 +375,7 @@ export class W3SSdk {
code: 155706,
message: 'Network error',
},
undefined
undefined,
)
}
}, 1000 * 10)
Expand All @@ -388,7 +388,7 @@ export class W3SSdk {
code: 155140,
message: 'Please provide the Apple social login configurations.',
},
undefined
undefined,
)

return
Expand All @@ -408,7 +408,7 @@ export class W3SSdk {
code: 155140,
message: 'Please provide the Facebook social login configurations.',
},
undefined
undefined,
)

return
Expand All @@ -420,7 +420,7 @@ export class W3SSdk {
this.generateOauthUrlWithParams(
SocialLoginProvider.FACEBOOK,
appId,
redirectUri
redirectUri,
) || {}

this.saveOAuthInfo(SocialLoginProvider.FACEBOOK, state)
Expand All @@ -434,7 +434,7 @@ export class W3SSdk {
code: 155140,
message: 'Please provide the Google social login configurations.',
},
undefined
undefined,
)

return
Expand All @@ -449,7 +449,7 @@ export class W3SSdk {
} = this.generateOauthUrlWithParams(
SocialLoginProvider.GOOGLE,
clientId,
redirectUri
redirectUri,
) || {}

this.saveOAuthInfo(SocialLoginProvider.GOOGLE, state, nonce)
Expand All @@ -466,7 +466,7 @@ export class W3SSdk {
private generateOauthUrlWithParams(
provider: SocialLoginProvider,
id: string,
redirectUri: string
redirectUri: string,
):
| {
url: string
Expand All @@ -478,14 +478,14 @@ export class W3SSdk {

if (provider === SocialLoginProvider.GOOGLE) {
const scope = encodeURIComponent(
'openid https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email'
'openid https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email',
)
const responseType = encodeURIComponent('id_token token')
const nonce = uuidv4()

return {
url: `https://accounts.google.com/o/oauth2/v2/auth?client_id=${id}&redirect_uri=${encodeURIComponent(
redirectUri
redirectUri,
)}&scope=${scope}&state=${state}&response_type=${responseType}&nonce=${nonce}`,
state,
nonce,
Expand All @@ -495,7 +495,7 @@ export class W3SSdk {

return {
url: `https://www.facebook.com/v13.0/dialog/oauth?client_id=${id}&redirect_uri=${encodeURIComponent(
redirectUri
redirectUri,
)}&scope=${scope}&state=${state}&response_type=token`,
state,
}
Expand All @@ -506,7 +506,7 @@ export class W3SSdk {
*/
private async execSocialLoginStatusCheck(): Promise<void> {
const socialLoginProvider = this.window.localStorage.getItem(
'socialLoginProvider'
'socialLoginProvider',
) as SocialLoginProvider

if (socialLoginProvider === SocialLoginProvider.APPLE) {
Expand Down Expand Up @@ -543,7 +543,7 @@ export class W3SSdk {
* @param socialLoginProvider - Social login provider.
*/
private handleHashLoginResponse(
socialLoginProvider: SocialLoginProvider
socialLoginProvider: SocialLoginProvider,
): void {
const hashParams = new URLSearchParams(window.location.hash.slice(1))

Expand Down Expand Up @@ -610,13 +610,13 @@ export class W3SSdk {
code: 155140,
message: 'Failed to validate the idToken / accessToken',
},
undefined
undefined,
)
}

private verifyTokenViaService(): void {
this.subscribeMessage()
this.appendIframe(false, 'sso/verify-token')
this.appendIframe(false, 'social/verify-token')

setTimeout(() => {
if (!this.receivedResponseFromService) {
Expand All @@ -625,7 +625,7 @@ export class W3SSdk {
code: 155706,
message: 'Network error',
},
undefined
undefined,
)
}
}, 1000 * 10)
Expand All @@ -640,7 +640,7 @@ export class W3SSdk {
private saveOAuthInfo(
provider: SocialLoginProvider,
state?: string,
nonce?: string
nonce?: string,
): void {
this.window.localStorage.setItem('socialLoginProvider', provider)
this.window.localStorage.setItem('state', state ?? '')
Expand All @@ -662,7 +662,7 @@ export class W3SSdk {
code: 155140,
message: 'Failed to validate the idToken / accessToken',
},
undefined
undefined,
)

return false
Expand Down Expand Up @@ -720,7 +720,7 @@ export class W3SSdk {
if (event.data?.onFrameReady) {
this.receivedResponseFromService = true
const iframe = this.window.document.getElementById(
'sdkIframe'
'sdkIframe',
) as HTMLIFrameElement
iframe?.contentWindow?.postMessage(
{
Expand All @@ -740,7 +740,7 @@ export class W3SSdk {
customLinks: this.customLinks,
},
deviceInfo: this.deviceInfo,
ssoVerification: {
socialVerification: {
token: this.socialLoginToken,
deviceToken: this.configs?.loginConfigs?.deviceToken,
deviceEncryptionKey:
Expand All @@ -755,13 +755,13 @@ export class W3SSdk {
},
},
},
this.serviceUrl
this.serviceUrl,
)
} else if (event.data?.onForgotPin) {
this.onForgotPin?.()
} else if (event.data?.onComplete) {
const iframe = this.window.document.getElementById(
'sdkIframe'
'sdkIframe',
) as HTMLIFrameElement
iframe?.parentNode?.removeChild(iframe)

Expand All @@ -783,15 +783,15 @@ export class W3SSdk {
} else if (event.data?.onSocialLoginVerified) {
void this.onLoginComplete?.(
event.data.onSocialLoginVerified.error,
event.data.onSocialLoginVerified.result
event.data.onSocialLoginVerified.result,
)

this.closeModal()
this.unSubscribeMessage()
} else if (event.data?.onEmailLoginVerified) {
void this.onLoginComplete?.(
event.data.onEmailLoginVerified.error,
event.data.onEmailLoginVerified.result
event.data.onEmailLoginVerified.result,
)

if (
Expand All @@ -816,7 +816,7 @@ export class W3SSdk {
*/
private closeModal(): void {
const iframe = this.window.document.getElementById(
'sdkIframe'
'sdkIframe',
) as HTMLIFrameElement
iframe?.parentNode?.removeChild(iframe)
}
Expand Down
26 changes: 9 additions & 17 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,9 @@ export enum ErrorCode {
insecurePinCode = 155704,
hintsMatchAnswers = 155705,
networkError = 155706,
biometricsSettingNotEnabled = 155708,
deviceNotSupportBiometrics = 155709,
biometricsKeyPermanentlyInvalidated = 155710,
biometricsUserSkip = 155711,
biometricsUserDisableForPin = 155712,
biometricsUserLockout = 155713,
biometricsUserLockoutPermanent = 155714,
biometricsUserNotAllowPermission = 155715,
biometricsInternalError = 155716,

userSecretMissing = 155717,
invalidUserSecret = 155718,
invalidUserTokenFormat = 155718,
userTokenMismatch = 155719,

walletIdNotFound = 156001,
Expand Down Expand Up @@ -249,7 +241,7 @@ export interface Challenge {
*/
challengeId: string
/**
* SSO user secret.
* Social login user secret.
*/
userSecret?: string
}
Expand Down Expand Up @@ -338,8 +330,8 @@ export interface SignTransactionResult extends ChallengeResult {
export interface OAuthInfo {
provider: SocialLoginProvider
scope?: string[]
ssoUserUUID?: string
ssoUserInfo?: {
socialUserUUID?: string
socialUserInfo?: {
email?: string
name?: string
phone?: string
Expand Down Expand Up @@ -391,12 +383,12 @@ export type ChallengeCompleteCallback = (
| ChallengeResult
| SignMessageResult
| SignTransactionResult
| undefined
| undefined,
) => Promise<void> | void

export type LoginCompleteCallback = (
error: Error | undefined,
result: SocialLoginResult | EmailLoginResult | undefined
result: SocialLoginResult | EmailLoginResult | undefined,
) => Promise<void> | void

export interface PostMessageEvent extends MessageEvent {
Expand Down Expand Up @@ -490,7 +482,7 @@ export interface SecuritySummary {
question?: string
}

export interface SsoConfirm {
export interface SocialEmailConfirm {
title?: string
headline?: string
}
Expand Down Expand Up @@ -599,7 +591,7 @@ export interface Localizations {
securityIntros?: SecurityIntros
securityQuestions?: SecurityQuestions
securitySummary?: SecuritySummary
ssoConfirm?: SsoConfirm
socialEmailConfirm?: SocialEmailConfirm
transactionRequest?: TransactionRequest
contractInteraction?: ContractInteraction
signatureRequest?: SignatureRequest
Expand Down

0 comments on commit a5bc1b7

Please sign in to comment.