2017-05-10 2 views
0

Ich versuche, eine einfache Authentifizierungsmethode zu entwickeln. Wenn der Benutzer über das richtige Zugriffstoken verfügt, wird die App weiterhin ausgeführt, andernfalls wird der Statuscode 401 (Nicht autorisiert) beendet.OAuth Token Middleware Slim Version 3 halt oder beenden

Ich habe so etwas wie dies:

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

Ich habe Multiples Lösung versucht, die Middleware zu vermeiden Fortsetzung der Ausführung, aber nichts funktioniert. Die App führt immer aus, was sie in $ app-> post ('/ user' ...) hat. Ich habe mehrere Lösungen für Slim v2 gefunden, aber bisher nichts für Slim v3. Vielen Dank.

Antwort

0

Es scheint, als ob Slim v3 im Vergleich zu v2 eine etwas andere Middleware handhabt. Die Antwort war, meinen eigenen $ response wie folgt zu erstellen:

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 
    } 

} 

Dies half mir How to use Middleware to control auth flow