Skip to content

Commit 45e1dff

Browse files
committed
Add withMultiFactors and tests
1 parent 2bed385 commit 45e1dff

File tree

5 files changed

+89
-27
lines changed

5 files changed

+89
-27
lines changed

docs/user-management.rst

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,8 @@ Property Type Description
269269
``deletePhotoUrl`` boolean Whether or not to delete the user's photo.
270270
``deleteDisplayName`` boolean Whether or not to delete the user's display name.
271271
``deletePhoneNumber`` boolean Whether or not to delete the user's phone number.
272-
``resetMultiFactor`` boolean Whether or not to reset all of the user's enrolled factors.
272+
``resetMultiFactor`` boolean Whether or not to reset all of the user's enrolled factors. Including phone and TOTP factors.
273+
``multiFactors`` array An array of multi-factor factors.
273274
``deleteProvider`` string|array One or more identity providers to delete.
274275
``customAttributes`` array A list of custom attributes which will be available in a User's ID token.
275276
====================== ============ ===========
@@ -323,18 +324,6 @@ Enable a user
323324
324325
$updatedUser = $auth->enableUser($uid);
325326
326-
*********************************
327-
Reset multi factor authentication
328-
*********************************
329-
330-
The Firebase Admin SDK allows removing all enrolled factors for multi factor authentication for a user:
331-
332-
.. code-block:: php
333-
334-
$uid = 'some-uid';
335-
336-
$updatedUser = $auth->resetMultiFactor($uid);
337-
338327
******************
339328
Custom user claims
340329
******************
@@ -409,6 +398,25 @@ This method always returns an instance of ``Kreait\Firebase\Auth\DeleteUsersResu
409398
Cloud Functions for Firebase. This is because batch deletes do not trigger a user deletion event on each user.
410399
Delete users one at a time if you want user deletion events to fire for each deleted user.
411400

401+
*********************************
402+
Set multi factor authentication
403+
*********************************
404+
405+
The Firebase Admin SDK allows setting multi-factor authentication for a user, consisting of phone factors. Setting the
406+
multi-factor authentication overwrites all existing factors. Setting the `mfaEnrollmentId` and `enrolledAt` properties is
407+
optional. For example:
408+
409+
.. code-block:: php
410+
411+
$uid = 'some-uid';
412+
413+
$updatedUser = $auth->updateUser($uid, ['multifactors' => [[
414+
'mfaEnrollmentId' => '85dc3f7b-7bef-45b9-b9e6-0a1c2c656fed',
415+
'phoneInfo' => '+31123456789',
416+
'displayName' => 'foo',
417+
'enrolledAt' => '2025-02-28T15:30:00Z',
418+
]]);
419+
412420
**************************************
413421
Duplicate/Unregistered email addresses
414422
**************************************

src/Firebase/Auth.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -235,11 +235,6 @@ public function disableUser(Stringable|string $uid): UserRecord
235235
return $this->updateUser($uid, UpdateUser::new()->markAsDisabled());
236236
}
237237

238-
public function resetMultiFactor(Stringable|string $uid): UserRecord
239-
{
240-
return $this->updateUser($uid, UpdateUser::new()->resetMultiFactor());
241-
}
242-
243238
public function deleteUser(Stringable|string $uid): void
244239
{
245240
$uid = Uid::fromString($uid)->value;

src/Firebase/Contract/Auth.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,6 @@ public function enableUser(Stringable|string $uid): UserRecord;
165165
*/
166166
public function disableUser(Stringable|string $uid): UserRecord;
167167

168-
/**
169-
* @param Stringable|non-empty-string $uid
170-
*
171-
* @throws Exception\AuthException
172-
* @throws Exception\FirebaseException
173-
*/
174-
public function resetMultiFactor(Stringable|string $uid): UserRecord;
175-
176168
/**
177169
* @param Stringable|non-empty-string $uid
178170
*

src/Firebase/Request/UpdateUser.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ public static function withProperties(array $properties): self
159159
$request = $request->resetMultiFactor();
160160
}
161161

162+
break;
163+
164+
case 'multifactors':
165+
$request = $request->withMultiFactors($value);
166+
162167
break;
163168
}
164169
}
@@ -212,6 +217,27 @@ public function withRemovedEmail(): self
212217
return $request;
213218
}
214219

220+
/**
221+
* @param array<array-key, array{
222+
* 'mfaEnrollmentId'?: string,
223+
* 'displayName': string,
224+
* 'phoneInfo': string,
225+
* 'enrolledAt'?: string,
226+
* }> $enrollments
227+
*/
228+
public function withMultiFactors(array $enrollments): self
229+
{
230+
$request = clone $this;
231+
232+
if (is_null($request->multiFactor)) {
233+
$request->multiFactor = [];
234+
}
235+
236+
$request->multiFactor['enrollments'] = $enrollments;
237+
238+
return $request;
239+
}
240+
215241
public function resetMultiFactor(): self
216242
{
217243
$request = clone $this;
@@ -220,7 +246,7 @@ public function resetMultiFactor(): self
220246
$request->multiFactor = [];
221247
}
222248

223-
$request->multiFactor['enrolledFactors'] = [];
249+
$request->multiFactor['enrollments'] = [];
224250

225251
return $request;
226252
}

tests/Integration/Request/UpdateUserTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,4 +203,45 @@ public function timeOfLastPasswordUpdateIsIncluded(): void
203203
$this->auth->deleteUser($user->uid);
204204
}
205205
}
206+
207+
#[Test]
208+
public function setMultiFactor(): void
209+
{
210+
$user = $this->auth->createUser(CreateUser::new());
211+
212+
$factor = [
213+
'mfaEnrollmentId' => '85dc3f7b-7bef-45b9-b9e6-0a1c2c656fed',
214+
'phoneInfo' => '+31123456789',
215+
'displayName' => '',
216+
'enrolledAt' => '2025-02-28T15:30:00Z',
217+
];
218+
219+
$check = $this->auth->updateUser($user->uid, ['enrolledfactors' => [$factor]]);
220+
221+
$this->assertEquals($factor['mfaEnrollmentId'], $check->mfaInfo?->mfaEnrollmentId);
222+
$this->assertEquals($factor['phoneInfo'], $check->mfaInfo?->phoneInfo);
223+
$this->assertEquals($factor['displayName'], $check->mfaInfo?->displayName);
224+
$this->assertEquals($factor['enrolledAt'], $check->mfaInfo?->enrolledAt);
225+
}
226+
227+
#[Test]
228+
public function resetMultiFactor(): void
229+
{
230+
$user = $this->auth->createUser(CreateUser::new());
231+
232+
$factor = [
233+
'mfaEnrollmentId' => '85dc3f7b-7bef-45b9-b9e6-0a1c2c656fed',
234+
'phoneInfo' => '+31123456789',
235+
'displayName' => '',
236+
'enrolledAt' => '2025-02-28T15:30:00Z',
237+
];
238+
239+
$updatedUser = $this->auth->updateUser($user->uid, ['enrolledfactors' => [$factor]]);
240+
241+
$this->assertNotNull($updatedUser->mfaInfo);
242+
243+
$check = $this->auth->updateUser($user->uid, ['resetmultifactor' => true]);
244+
245+
$this->assertNull($check->mfaInfo);
246+
}
206247
}

0 commit comments

Comments
 (0)