解密Laravel密码重置令牌

时间:2017-01-25 21:55:37

标签: php laravel unit-testing laravel-5.3 laravel-5.4

我正在编写一个测试,确保我的应用程序的密码重置功能正常工作。密码重置系统是使用php artisan make:auth命令创建的。为了使测试通过,我需要自动向/password/reset/{$token}发送GET请求,其中$token是存储在password_resets表中的值。 Laravel像这样存储令牌:

$2y$10$9grKb3c6.Toiv0kjUWbCUeT8Q8D.Fg2gZ/xDLGQUAkmdyHigmRkNW

但是当Laravel将密码重置电子邮件发送给用户时,重置令牌在电子邮件中显示如下:

382aa64567ecd05a774c2e4ebb199d3340a1424300707053354c749c10487594

由于重置令牌中的正斜杠,我对/password/reset/$2y$10$9grKb3c6.Toiv0kjUWbCUeT8Q8D.Fg2gZ/xDLGQUAkmdyHigmRkNW的GET请求失败。 (在' g2gZ' 之后)

我尝试使用辅助函数decrypt(),但没有运气。

如何转换我从password_resets表中提取的密码重置令牌以匹配Laravel发送给用户的内容?

不确定这是否相关,但我确实将我的应用程序从5.3升级到5.4。

4 个答案:

答案 0 :(得分:11)

您可以从用于传递给Notification的assertSentTo方法的其他检查的闭包中获取令牌,因为$token是标准ResetPassword通知的公共属性。

在你的测试中:

Notification::fake();

$this->postJson('api/user/reset', ['email' => $user->email])
    ->assertStatus(200);

$token = '';

Notification::assertSentTo(
    $this->user,
    \Illuminate\Auth\Notifications\ResetPassword::class,
    function ($notification, $channels) use (&$token) {
        $token = $notification->token;

        return true;
    });

$this->postJson('api/user/resetting', [
    'email' => $user->email,
    'token' => $token,
    'password' => '87538753',
    'password_confirmation' => '87538753'
])
    ->assertStatus(200);

答案 1 :(得分:3)

存储在password_resets表中的令牌就像普通密码一样进行哈希处理,因此您无法将其反转以获取原始令牌。

我建议您在运行测试时使用log mail driver。然后密码重置电子邮件将以纯文本形式打印在laravel日志中,您可以从中获取令牌。

答案 2 :(得分:1)

我不认为你可以,保存的哈希值是随机40位数的sha256哈希值的加密值。这意味着只有一种方式可以检查它是不可逆的。

答案 3 :(得分:1)

为了测试密码重置功能,我用password_reset表替换了生成的令牌。

使用createTokenRepository()方法 - laravel/framework/src/Illuminate/Auth/Passwords/PasswordBrokerManager.php

创建重置令牌

对于散列创建的令牌,Laravel使用make()方法 - laravel/framework/src/Illuminate/Hashing/BcryptHasher.php

public function test_it_should_reset_the_password()
{

    Mail::fake();

    $user = factory(App\User::class)->create();

    $response = $this->json('POST', 'api/password/email',
                    [
                        'email' => $user->email
                    ]);
    $response->assertStatus(202);

    Mail::hasSent($user, ResetPassword::class);

    // Since we don't know the emailed token from 
    // the previous JSON call, we're
    // gonna replace the token with a new one
    $token = hash_hmac('sha256', Str::random(40), $user);
    DB::table('password_resets')
            ->where('email', $user->email)
            ->update([
                'token' => password_hash($token, PASSWORD_BCRYPT, ['cost' => '10'])
            ]);

    $response = $this->json('POST', 'api/password/reset', [
                    'email'                 => $user->email,
                    'password'              => 'new_user_password',
                    'password_confirmation' => 'new_user_password',
                    'token'                 => $token
                ]);
    $response->assertStatus(202);

    $response = $this->json('POST', 'api/login',
                    [
                        'email' => $user->email,
                        'password' => 'new_user_password'
                    ]);
    $response->assertStatus(202);
    // check for JWT token
    $response->assertJson(['token' => true]);

}