fix(mfa): expose challenge code directly in response model instead of aliasing to secret

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Matej BačoandClaude Fable 5 committed 2026-08-08 20:32:11 +02:00
1 parent 6a55ae49f4
commit 69c01a4b9d
4 files changed
+21 -20

No files matched your search

@@ -37,7 +37,7 @@ class Get extends Action
namespace: 'users',
group: 'mfa',
name: 'getMFAChallenge',
description: 'Get a custom MFA challenge for a user, including the secret to be delivered through your own channel.',
description: 'Get a custom MFA challenge for a user, including the code to be delivered through your own channel.',
auth: [AuthType::ADMIN, AuthType::KEY],
responses: [
new SDKResponse(
@@ -72,8 +72,6 @@ class Get extends Action
throw new Exception(Exception::USER_INVALID_TOKEN);
}
$challenge->setAttribute('secret', $challenge->getAttribute('code'));
$response->dynamic($challenge, Response::MODEL_MFA_CHALLENGE_SECRET);
}
}
@@ -11,9 +11,9 @@ class MFAChallengeSecret extends MFAChallenge
parent::__construct();
$this
->addRule('secret', [
->addRule('code', [
'type' => self::TYPE_STRING,
'description' => 'Challenge secret to be delivered to the end user through a custom channel.',
'description' => 'Challenge code to be delivered to the end user through a custom channel.',
'default' => '',
'example' => '446372',
]);
@@ -4286,27 +4286,29 @@ final class AccountCustomClientTest extends Scope
$this->assertEquals(201, $challenge['headers']['status-code']);
$this->assertNotEmpty($challenge['body']['$id']);
// The challenge code must never be exposed on the client-facing create response
$this->assertArrayNotHasKey('code', $challenge['body']);
$challengeId = $challenge['body']['$id'];
$userId = $challenge['body']['userId'];
// Test FAILURE: a client session (no API key) must not be able to read the secret
$clientSecretResponse = $this->client->call(Client::METHOD_GET, '/users/' . $userId . '/mfa/challenges/' . $challengeId, array_merge([
// Test FAILURE: a client session (no API key) must not be able to read the code
$clientCodeResponse = $this->client->call(Client::METHOD_GET, '/users/' . $userId . '/mfa/challenges/' . $challengeId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()));
$this->assertEquals(401, $clientSecretResponse['headers']['status-code']);
$this->assertEquals(401, $clientCodeResponse['headers']['status-code']);
// Server SDK reads the raw secret via the server-only endpoint
$secretResponse = $this->client->call(Client::METHOD_GET, '/users/' . $userId . '/mfa/challenges/' . $challengeId, [
// Server SDK reads the raw code via the server-only endpoint
$codeResponse = $this->client->call(Client::METHOD_GET, '/users/' . $userId . '/mfa/challenges/' . $challengeId, [
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
'x-appwrite-key' => $apiKey,
]);
$this->assertEquals(200, $secretResponse['headers']['status-code']);
$this->assertNotEmpty($secretResponse['body']['secret']);
$secret = $secretResponse['body']['secret'];
$this->assertEquals(200, $codeResponse['headers']['status-code']);
$this->assertNotEmpty($codeResponse['body']['code']);
$code = $codeResponse['body']['code'];
// Test FAILURE: verifying with a random/incorrect otp before the real one is used
$wrongFirst = $this->client->call(Client::METHOD_PUT, '/account/mfa/challenge', array_merge([
@@ -4319,13 +4321,13 @@ final class AccountCustomClientTest extends Scope
$this->assertEquals(401, $wrongFirst['headers']['status-code']);
// Test SUCCESS: Verify with the correct secret
// Test SUCCESS: Verify with the correct code
$verification = $this->client->call(Client::METHOD_PUT, '/account/mfa/challenge', array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $projectId,
], $this->getHeaders()), [
'challengeId' => $challengeId,
'otp' => $secret
'otp' => $code
]);
$this->assertEquals(200, $verification['headers']['status-code']);
@@ -4341,7 +4343,7 @@ final class AccountCustomClientTest extends Scope
'x-appwrite-project' => $projectId,
], $this->getHeaders()), [
'challengeId' => $challengeId,
'otp' => $secret
'otp' => $code
]);
$this->assertEquals(401, $reuse['headers']['status-code']);
+5 -4
View File
@@ -663,10 +663,11 @@ trait UsersBase
]);
$this->assertEquals(201, $challenge['headers']['status-code']);
$this->assertArrayNotHasKey('code', $challenge['body']);
$challengeId = $challenge['body']['$id'];
/**
* Test for SUCCESS: server (API key) can read the secret
* Test for SUCCESS: server (API key) can read the code
*/
$response = $this->client->call(Client::METHOD_GET, '/users/' . $userId . '/mfa/challenges/' . $challengeId, array_merge([
'content-type' => 'application/json',
@@ -676,7 +677,7 @@ trait UsersBase
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals($challengeId, $response['body']['$id']);
$this->assertEquals($userId, $response['body']['userId']);
$this->assertNotEmpty($response['body']['secret']);
$this->assertNotEmpty($response['body']['code']);
/**
* Test for FAILURE: a client session (not a server key) must be rejected
@@ -735,8 +736,8 @@ trait UsersBase
/**
* Test for FAILURE: a non-custom challenge (e.g. totp) must not be readable,
* even with valid ownership and a valid API key. Native factors deliver their
* own secret out of band (email/SMS); this endpoint only exists to hand the
* 'custom' factor's secret to the developer's own delivery mechanism.
* own code out of band (email/SMS); this endpoint only exists to hand the
* 'custom' factor's code to the developer's own delivery mechanism.
*/
$totpChallenge = $this->client->call(Client::METHOD_POST, '/account/mfa/challenge', [
'content-type' => 'application/json',