Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add AuthWeakPasswordError #817

Merged
merged 1 commit into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,25 @@ export class AuthRetryableFetchError extends CustomAuthError {
export function isAuthRetryableFetchError(error: unknown): error is AuthRetryableFetchError {
return isAuthError(error) && error.name === 'AuthRetryableFetchError'
}

/**
* This error is thrown on certain methods when the password used is deemed
* weak. Inspect the reasons to identify what password strength rules are
* inadequate.
*/
export class AuthWeakPasswordError extends CustomAuthError {
/**
* Reasons why the password is deemed weak.
*/
reasons: ('length' | 'characters' | 'pwned' | string)[]

constructor(message: string, status: number, reasons: string[]) {
super(message, 'AuthWeakPasswordError', status)

this.reasons = reasons
}
}

export function isAuthWeakPasswordError(error: unknown): error is AuthWeakPasswordError {
return isAuthError(error) && error.name === 'AuthWeakPasswordError'
}
23 changes: 22 additions & 1 deletion src/lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import {
User,
UserResponse,
} from './types'
import { AuthApiError, AuthRetryableFetchError, AuthUnknownError } from './errors'
import {
AuthApiError,
AuthRetryableFetchError,
AuthWeakPasswordError,
AuthUnknownError,
} from './errors'

export type Fetch = typeof fetch

Expand Down Expand Up @@ -46,6 +51,22 @@ async function handleError(error: unknown) {
throw new AuthUnknownError(_getErrorMessage(e), e)
}

if (
typeof data === 'object' &&
data &&
typeof data.weak_password === 'object' &&
data.weak_password &&
Array.isArray(data.weak_password.reasons) &&
data.weak_password.reasons.length &&
data.weak_password.reduce((a: boolean, i: any) => a && typeof i === 'string', true)
) {
throw new AuthWeakPasswordError(
_getErrorMessage(data),
error.status,
data.weak_password.reasons
)
}

throw new AuthApiError(_getErrorMessage(data), error.status || 500)
}

Expand Down
7 changes: 5 additions & 2 deletions src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,11 @@ function base64urlencode(str: string) {
}

export async function generatePKCEChallenge(verifier: string) {
const hasCryptoSupport = typeof crypto !== 'undefined' && typeof crypto.subtle !== 'undefined' && typeof TextEncoder !== 'undefined';

const hasCryptoSupport =
typeof crypto !== 'undefined' &&
typeof crypto.subtle !== 'undefined' &&
typeof TextEncoder !== 'undefined'

if (!hasCryptoSupport) {
console.warn(
'WebCrypto API is not supported. Code challenge method will default to use plain instead of sha256.'
Expand Down
Loading