Advertisement
Guest User

Untitled

a guest
May 28th, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.16 KB | None | 0 0
  1. <?php
  2.  
  3. namespace edesarrollos\auth;
  4.  
  5. use yii\web\IdentityInterface;
  6. use yii\web\Request;
  7. use yii\web\Response;
  8. use yii\web\UnauthorizedHttpException;
  9.  
  10. class Auth extends \yii\filters\auth\AuthMethod {
  11.  
  12.   public $realm = "api";
  13.  
  14.   /**
  15.    * Authenticates the current user.
  16.    * @param Usuario $user
  17.    * @param Request $request
  18.    * @param Response $response
  19.    * @return IdentityInterface the authenticated user identity. If authentication information is not provided, null will be returned.
  20.    * @throws UnauthorizedHttpException if authentication information is provided but is invalid.
  21.    */
  22.   public function authenticate($user, $request, $response) {
  23.     $master = $request->getHeaders()->get('X-Ede-User');
  24.     $secret = $request->getHeaders()->get('X-Ede-Pass');
  25.     if($master !== null && $secret !== null) {
  26.       $identity = $user->loginByMasterAndSecret($master, $secret);
  27.       if($identity === null) {
  28.         $this->handleFailure($response);
  29.       }
  30.       return $identity;
  31.     }
  32.  
  33.     return null;
  34.   }
  35.  
  36.   public function challenge($response) {
  37.     $response->getHeaders()->set('WWW-Authenticate', "Bearer realm=\"{$this->realm}\"");
  38.   }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement