2016-08-11 2 views
3

Cakephp 3.1의 인증 구성 요소를 사용하여 다이제스트 인증을 만들려고하는데 문제가 있습니다. 아래의 코드를 사용하고 있으며 이전 팝업에서 올바른 사용자 이름과 암호를 입력 한 직후에 팝업되는 HTTP 인증 팝업을 가지고 있습니다. 그런 다음 취소를 누르면 Cake \ Auth \ BasicAuthenticate-> unauthenticated가됩니다.CakePHP 3의 다이제스트 인증

누군가 내가 잘못하고있는 것을 말해 줄 수 있습니까? 내가 디버그 - 키트 환경에서 검사 할 때 클라이언트 부분

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', 
     ] 
    ]); 

에서 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; 
} 

,이 있습니다

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

이것은 너무 늦었을 수도 있지만 여전히 도움이 될 것입니다.

잘 사용 $this->Auth->unauthorizedRedirect = false,입니다. 유효한 사용자 이름과 암호를 제출하지 않으면 AuthComponent가 ForbiddenException을 예외로 처리하고 다른 페이지로 리디렉션하지 않습니다. 올바르게

가져 오기 등록 :

분명히 등록하는 것이 중요하다는/가능한 인증을 소화하기 위해 정확하게 사용자의 소화 비밀번호를 추가 할 수 있습니다.

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; 
    } 

그러나 우리는 위에서 언급 한 변수/용어에주의해야한다 :

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.) 

결론적으로 전자 메일 (사용자 이름)과 password_hash (암호)가있는 경우 위의 함수는 다음과 같습니다.

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; 
} 

위의 사항에 초점을 맞추는 이유는 실수가있을 가능성이 있기 때문입니다.