2017-05-10 9 views
0

간단한 인증 방법을 개발하려고합니다. 사용자에게 올바른 액세스 토큰이 있으면 앱이 계속 실행되고 그렇지 않으면 401 (권한없는) 상태 코드로 종료됩니다. 내가 배수에게로 미들웨어를 피하기 위해 솔루션을 시도OAuth 토큰 미들웨어 슬림 버전 3 중지 또는 종료

api.php

... 
$headers = getallheaders(); 
$auth = new OAuth2Auth($headers, $authconfig); 
$app->add($auth, $dbconfig); 

$app->post('/user', function($req, $res, $args) { 
    //MY CODE ONLY FOR LOGGED IN USERS 
}); 

OAuth2Auth.php

public function __construct($headers, $dbconfig) { 
    $this->whiteList = array('\/auth'); 
    $this->config = $dbconfig; 
    $this->headers = $headers; 
} 

public function __invoke($req, $res, $next) { 
    $authHeader = $this->headers['Authorization']; //grabbing the token 
    $auth = new AuthService($this->dbconfig); 
    $validated = $auth->verifyOAuth($authHeader); //Verifying Token against DB 
    if ($validated){ 
     $response = $next($request, $response); 
    }else{ 
     //EXIT, STOP or HALT 
    } 
    return $response; 
} 

:

나는 이런 식으로 뭔가있어 그 실행을 계속하지만 아무것도 작동하지 않습니다. 앱은 항상 $ app-> post ('/ user'...) 안에있는 것을 실행합니다. 나는 Slim v2를위한 배수 해결책을 찾아 냈다 그러나 Slim v3를위한 지금까지는 아무것도. 감사.

답변

0

Slim v3은 v2와는 조금 다른 미들웨어를 처리합니다. 이것은 나에게 How to use Middleware to control auth flow

도움

public function __invoke($req, $res, $next) { 
    $authHeader = $this->headers['Authorization']; //grabbing the token 
    $auth = new AuthService($this->dbconfig); 
    $validated = $auth->verifyOAuth($authHeader); //Verifying Token against DB 
    if ($validated){ 
     return $response = $next($request, $response) 
         ->withStatus(200);//OK 
    }else{ 
     return $response->withStatus(403);//Forbidden 
    } 

} 

: 대답은 이런 내 자신의 $ 응답을 만드는 것이 었습니다