Skip to content

Commit b9cb52e

Browse files
committed
Merge branch 'ps-sean/1.x' into 1.x
2 parents b1f6125 + df6abce commit b9cb52e

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

routes/routes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Laravel\Fortify\Http\Controllers\TwoFactorAuthenticatedSessionController;
1818
use Laravel\Fortify\Http\Controllers\TwoFactorAuthenticationController;
1919
use Laravel\Fortify\Http\Controllers\TwoFactorQrCodeController;
20+
use Laravel\Fortify\Http\Controllers\TwoFactorSecretKeyController;
2021
use Laravel\Fortify\Http\Controllers\VerifyEmailController;
2122

2223
Route::group(['middleware' => config('fortify.middleware', ['web'])], function () {
@@ -154,6 +155,10 @@
154155
->middleware($twoFactorMiddleware)
155156
->name('two-factor.qr-code');
156157

158+
Route::get('/user/two-factor-secret-key', [TwoFactorSecretKeyController::class, 'show'])
159+
->middleware($twoFactorMiddleware)
160+
->name('two-factor.secret-key');
161+
157162
Route::get('/user/two-factor-recovery-codes', [RecoveryCodeController::class, 'index'])
158163
->middleware($twoFactorMiddleware)
159164
->name('two-factor.recovery-codes');
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Laravel\Fortify\Http\Controllers;
4+
5+
use Illuminate\Http\Request;
6+
use Illuminate\Routing\Controller;
7+
8+
class TwoFactorSecretKeyController extends Controller
9+
{
10+
/**
11+
* Get the current user's two factor authentication setup / secret key.
12+
*
13+
* @param \Illuminate\Http\Request $request
14+
* @return \Symfony\Component\HttpFoundation\Response
15+
*/
16+
public function show(Request $request)
17+
{
18+
if (is_null($request->user()->two_factor_secret)) {
19+
abort(404, 'Two factor authentication has not been enabled.');
20+
}
21+
22+
return response()->json([
23+
'secretKey' => decrypt($request->user()->two_factor_secret),
24+
]);
25+
}
26+
}

tests/TwoFactorAuthenticationControllerTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,29 @@ public function test_two_factor_authentication_can_be_enabled()
4444
$this->assertNotNull($user->twoFactorQrCodeSvg());
4545
}
4646

47+
public function test_two_factor_authentication_secret_key_can_be_retrieved()
48+
{
49+
Event::fake();
50+
51+
$this->loadLaravelMigrations(['--database' => 'testbench']);
52+
$this->artisan('migrate', ['--database' => 'testbench'])->run();
53+
54+
$user = TestTwoFactorAuthenticationUser::forceCreate([
55+
'name' => 'Taylor Otwell',
56+
'email' => '[email protected]',
57+
'password' => bcrypt('secret'),
58+
'two_factor_secret' => encrypt('foo'),
59+
]);
60+
61+
$response = $this->withoutExceptionHandling()->actingAs($user)->getJson(
62+
'/user/two-factor-secret-key'
63+
);
64+
65+
$response->assertStatus(200);
66+
67+
$this->assertEquals('foo', $response->original['secretKey']);
68+
}
69+
4770
public function test_two_factor_authentication_can_be_confirmed()
4871
{
4972
Event::fake();

0 commit comments

Comments
 (0)