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

fix: handle recovery brute force protection #296

Merged
merged 2 commits into from
Nov 27, 2024
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
6 changes: 6 additions & 0 deletions packages/elements-react/jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ export default {
".+\\.(svg|css|styl|less|sass|scss|png|jpg|ttf|woff|woff2)$":
"jest-transform-stub",
},
collectCoverageFrom: [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.js",
"src/**/*.jsx",
],
moduleFileExtensions: ["ts", "tsx", "js", "jsx"],
coverageDirectory: "../../coverage/packages/elements-react",
coveragePathIgnorePatterns: ["/node_modules/", "/dist/", ".svg"],
Expand Down
49 changes: 42 additions & 7 deletions packages/elements-react/src/util/onSubmitRecovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@
// SPDX-License-Identifier: Apache-2.0

import {
ContinueWith,
FlowType,
GenericError,
handleContinueWith,
handleFlowError,
instanceOfContinueWithRecoveryUi,
OnRedirectHandler,
RecoveryFlow,
recoveryUrl,
UpdateRecoveryFlowBody,
} from "@ory/client-fetch"
import { frontendClient } from "./client"

Check warning on line 16 in packages/elements-react/src/util/onSubmitRecovery.ts

View check run for this annotation

Codecov / codecov/patch

packages/elements-react/src/util/onSubmitRecovery.ts#L16

Added line #L16 was not covered by tests
import { OryClientConfiguration } from "./clientConfiguration"
import { OryFlowContainer } from "./flowContainer"
import { OnSubmitHandlerProps } from "./submitHandler"
import { frontendClient } from "./client"

/**
* Use this method to submit a recovery flow. This method is used in the `onSubmit` handler of the recovery form.
Expand Down Expand Up @@ -63,14 +68,44 @@
onRestartFlow: () => {
onRedirect(recoveryUrl(config), true)
},
onValidationError: (body: RecoveryFlow) => {
setFlowContainer({
flow: body,
flowType: FlowType.Recovery,
config,
})
onValidationError: (body: RecoveryFlow | { error: GenericError }) => {

Check warning on line 71 in packages/elements-react/src/util/onSubmitRecovery.ts

View check run for this annotation

Codecov / codecov/patch

packages/elements-react/src/util/onSubmitRecovery.ts#L71

Added line #L71 was not covered by tests
if ("error" in body) {
handleContinueWithRecoveryUIError(body.error, config, onRedirect)
return

Check warning on line 74 in packages/elements-react/src/util/onSubmitRecovery.ts

View check run for this annotation

Codecov / codecov/patch

packages/elements-react/src/util/onSubmitRecovery.ts#L73-L74

Added lines #L73 - L74 were not covered by tests
} else {
setFlowContainer({

Check warning on line 76 in packages/elements-react/src/util/onSubmitRecovery.ts

View check run for this annotation

Codecov / codecov/patch

packages/elements-react/src/util/onSubmitRecovery.ts#L76

Added line #L76 was not covered by tests
flow: body,
flowType: FlowType.Recovery,
config,
})
}
},
onRedirect,
}),
)
}

function handleContinueWithRecoveryUIError(

Check warning on line 88 in packages/elements-react/src/util/onSubmitRecovery.ts

View check run for this annotation

Codecov / codecov/patch

packages/elements-react/src/util/onSubmitRecovery.ts#L88

Added line #L88 was not covered by tests
error: GenericError,
config: OryClientConfiguration,
onRedirect: OnRedirectHandler,
) {
if (
"continue_with" in error.details &&
Array.isArray(error.details.continue_with)
) {
const continueWithRecovery = (
error.details.continue_with as ContinueWith[]

Check warning on line 98 in packages/elements-react/src/util/onSubmitRecovery.ts

View check run for this annotation

Codecov / codecov/patch

packages/elements-react/src/util/onSubmitRecovery.ts#L98

Added line #L98 was not covered by tests
).find(instanceOfContinueWithRecoveryUi)
if (continueWithRecovery?.action === "show_recovery_ui") {
onRedirect(

Check warning on line 101 in packages/elements-react/src/util/onSubmitRecovery.ts

View check run for this annotation

Codecov / codecov/patch

packages/elements-react/src/util/onSubmitRecovery.ts#L101

Added line #L101 was not covered by tests
config.project.recovery_ui_url +
"?flow=" +
continueWithRecovery?.flow.id,
false,
)
return

Check warning on line 107 in packages/elements-react/src/util/onSubmitRecovery.ts

View check run for this annotation

Codecov / codecov/patch

packages/elements-react/src/util/onSubmitRecovery.ts#L107

Added line #L107 was not covered by tests
}
}
onRedirect(recoveryUrl(config), true)

Check warning on line 110 in packages/elements-react/src/util/onSubmitRecovery.ts

View check run for this annotation

Codecov / codecov/patch

packages/elements-react/src/util/onSubmitRecovery.ts#L110

Added line #L110 was not covered by tests
}
Loading