CakePHP 3下的摘要认证

时间:2016-08-11 08:53:31

标签: php cakephp cakephp-3.x digest-authentication

我正在尝试使用Cakephp 3.1下的Authentification组件创建摘要认证,我遇到了问题。我正在使用下面的代码,我在上一个弹出窗口输入正确的用户名和密码后立即弹出HTTP-Authentication弹出窗口。然后如果我按下取消我有这个:Cake \ Auth \ BasicAuthenticate-> unauthenticated。

有人可以告诉我我做错了什么吗?

AppController.php

$this->loadComponent('Auth', [
        'authorize' => 'Controller',
        'loginRedirect' => [
            'controller' => 'Users',
            'action' => 'index'
        ],
        'authenticate' => [
            'Digest' => [
                'fields' => ['username' => 'username', 'password' => 'digest_hash'],
                'userModel' => 'Users',
            ],
        ],
        'loginAction' => [
            'controller' => 'Users',
            'action' => 'login',
        ],
        'storage' => 'Memory',
        'unauthorizedRedirect' => false
    ]);

UserTable.php

public function beforeSave(Event $event)
{
    $entity = $event->data['entity'];

    // Make a password for digest auth.
    $entity->digest_hash = DigestAuthenticate::password(
        $entity->username,
        $entity->plain_password,
        env('SCRIPT_NAME')
    );
    return true;
}

在客户端部分

    public function digest(){
    $http = new Client();
    $response = $http->get('http://localhost/project/api/v1/users/view/22', [], [
        'auth' => [
            'type' => 'digest',
            'username' => 'Digest',
            'password' => 'my_password',
        ]
    ]);

当我签入Debug-kit环境时,我有这个:

PHP_AUTH_DIGEST     username="Digest", realm="localhost", nonce="57ac3609a5b79", uri="/project/api/v1/users/view/22", response="af0e1fe455aa7f1475df715ef5231b56", opaque="421aa90e079fa326b6494f812ad13e79", qop=auth, nc=00000001, cnonce="0bb461453700ebc1"

1 个答案:

答案 0 :(得分:1)

这可能为时已晚,但仍对某人有帮助!

  

好好使用 public function beforeSave(Event $event) { $entity = $event->data['entity']; // Make a password for digest auth. $entity->digest_hash = DigestAuthenticate::password( $entity->username, $entity->plain_password, env('SERVER_NAME') ); return true; } 。导致AuthComponent抛出ForbiddenException   异常,而不是重定向到另一个页面,除非您提交有效的用户名和密码。

正确注册

显然,正确注册/添加用户的摘要密码以进行摘要式身份验证非常重要。

documentation中所述,我们可以通过在UsersTable.php中添加以下代码来添加摘要哈希密码:

1. $entity->digest_hash (this should be equivalent to the field you have made to
   save password, eg. password_hash)

2. $entity->username (this should be equivalent to the field you have made to
   save username, eg. email)

3. $entity->plain_password (again this should be equivalent to the field you have made to
   save password, eg. password_hash)

4. env('SERVER_NAME') (this is third parameter for making digest password,
   "SERVER_NAME" is default value and we can left it this way.)

但是我们应该注意上面提到的变量/术语:

 public function beforeSave(Event $event)
 {
  $entity = $event->data['entity'];

  // Make a password for digest auth.
  $entity->password_hash= DigestAuthenticate::password(
    $entity->email,
    $entity->password_hash,
    env('SERVER_NAME')
  );
  return true;
 }

作为结论,如果我们有一个电子邮件(用于用户名)和password_hash(用于密码),那么上面的功能将是:

client.Search<KeywordEntity>(s => s.Index("<INDEX NAME>")
                                    .Type("<TYPE NAME>")
                                    .Query(q =>q
                                        .Bool(b => b.
                                            Must(prefix => prefix.Prefix(pre => pre.OnField("KeywordName").Value("PREFIX QUERY")))
                                            .Must(range => range.Range(ran => ran.OnField("TotalSearch").GreaterOrEquals(minimumTotalSearch)))
                          )).SortAscending("KeywordName")
                          .From(StartIndex)
                          .Size(totalItems));

我之所以专注于上述事情,是因为他们有可能犯错误。