Skip to content

fix: mfa_requirements in MFARequiredErrorPayload made optional - #1272

Open
NandanPrabhu wants to merge 1 commit into
masterfrom
fix/mfa-required-payload-optional-requirements
Open

fix: mfa_requirements in MFARequiredErrorPayload made optional#1272
NandanPrabhu wants to merge 1 commit into
masterfrom
fix/mfa-required-payload-optional-requirements

Conversation

@NandanPrabhu

@NandanPrabhu NandanPrabhu commented Jul 28, 2026

Copy link
Copy Markdown
Contributor

Changes

  • Added a custom init(from:) on MFARequiredErrorPayload (via a Decodable extension) that uses decodeIfPresent for mfa_requirements and falls back to MFARequirements() when the key is absent from the server response.
  • Added a no-arg init to MFARequirements (with defaulted nil parameters) to make the fallback expression concise.
  • mfaRequirements remains non-optional on MFARequiredErrorPayload — callers don't need to unwrap it.

Test plan

  • should decode mfa required payload with mfa_requirements — verifies enroll and challenge are decoded correctly when present.
  • should decode mfa required payload without mfa_requirements — verifies mfaRequirements.enroll and mfaRequirements.challenge are nil when the key is absent.
  • Full test suite passes via swift test.

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

MFA-required error payload decoding now explicitly decodes its fields, defaults missing MFA requirements, documents optional requirement lists, and adds tests for present and absent mfa_requirements.

Changes

MFA payload decoding

Layer / File(s) Summary
Custom MFA payload decoding
Auth0/MFARequiredErrorPayload.swift
MFARequiredErrorPayload now decodes through a public Decodable initializer, defaults missing requirements to nil lists, and documents optional enrollment requirements.
MFA decoding test coverage
Auth0Tests/AuthenticationErrorSpec.swift
Tests validate MFA token and requirement decoding when mfa_requirements is present or absent.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Suggested reviewers: pmathew92, amitsingh05667

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title is concise and accurately reflects the main change to MFARequiredErrorPayload decoding around mfa_requirements.
Description check ✅ Passed The description matches the implemented decoding fallback, initializer addition, and tests for payloads with and without mfa_requirements.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/mfa-required-payload-optional-requirements

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@NandanPrabhu
NandanPrabhu marked this pull request as ready for review July 28, 2026 09:27
@NandanPrabhu
NandanPrabhu requested a review from a team as a code owner July 28, 2026 09:27
  When the server omits mfa_requirements from the mfa_required error response,
  the custom init(from:) now falls back to MFARequirements() (enroll and
  challenge both nil) instead of failing to decode the entire payload.
@NandanPrabhu
NandanPrabhu force-pushed the fix/mfa-required-payload-optional-requirements branch from da0569b to b28bb2f Compare July 28, 2026 09:28
@NandanPrabhu NandanPrabhu changed the title fix: handle missing mfa_requirements in MFARequiredErrorPayload decoding fix: mfa_requirements in MFARequiredErrorPayload made optional Jul 28, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@Auth0/MFARequiredErrorPayload.swift`:
- Line 52: Add a DocC comment immediately before
MFARequiredErrorPayload.init(from:) describing that it decodes the payload from
a Decoder and can throw decoding errors. Keep the initializer’s behavior
unchanged.

In `@Auth0Tests/AuthenticationErrorSpec.swift`:
- Around line 419-449: The newly added MFA decoding examples do not follow the
required test naming conventions. Update both descriptions in the
`AuthenticationErrorSpec` examples to present-tense declarative `should … when
…` wording, and rename local test constants such as `values`, `error`, and
`payload` to UpperCamelCase names (`Values`, `Error`, and `Payload`) while
preserving the existing assertions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: c18e766d-9419-4b51-a9d2-e47dc22a89f0

📥 Commits

Reviewing files that changed from the base of the PR and between 2e337ed and b28bb2f.

📒 Files selected for processing (2)
  • Auth0/MFARequiredErrorPayload.swift
  • Auth0Tests/AuthenticationErrorSpec.swift

Comment thread Auth0/MFARequiredErrorPayload.swift
Comment on lines +419 to +449
it("should decode mfa required payload with mfa_requirements") {
let values: [String: Any] = [
"error": "mfa_required",
"error_description": "MFA Required",
"mfa_token": "test-mfa-token",
"mfa_requirements": [
"enroll": [["type": "otp"]],
"challenge": [["type": "otp"]]
]
]
let error = AuthenticationError(info: values, statusCode: 403)
let payload = error.mfaRequiredErrorPayload
expect(payload).toNot(beNil())
expect(payload?.mfaToken) == "test-mfa-token"
expect(payload?.mfaRequirements.enroll?.first?.type) == "otp"
expect(payload?.mfaRequirements.challenge?.first?.type) == "otp"
}

it("should decode mfa required payload without mfa_requirements") {
let values: [String: Any] = [
"error": "mfa_required",
"error_description": "MFA Required",
"mfa_token": "test-mfa-token"
]
let error = AuthenticationError(info: values, statusCode: 403)
let payload = error.mfaRequiredErrorPayload
expect(payload).toNot(beNil())
expect(payload?.mfaToken) == "test-mfa-token"
expect(payload?.mfaRequirements.enroll).to(beNil())
expect(payload?.mfaRequirements.challenge).to(beNil())
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win

Match the required test naming conventions.

Use should … when … descriptions and UpperCamelCase test constants for the newly added cases, e.g. Values, Error, and Payload. As per coding guidelines, “it descriptions must use present tense, declarative style: "should ... when ..."” and “Test constants must use UpperCamelCase.”

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@Auth0Tests/AuthenticationErrorSpec.swift` around lines 419 - 449, The newly
added MFA decoding examples do not follow the required test naming conventions.
Update both descriptions in the `AuthenticationErrorSpec` examples to
present-tense declarative `should … when …` wording, and rename local test
constants such as `values`, `error`, and `payload` to UpperCamelCase names
(`Values`, `Error`, and `Payload`) while preserving the existing assertions.

Sources: Coding guidelines, Path instructions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant