jeudi 26 juillet 2018

Passport auth not validate the request

People forgive me for my English,

These last week I had to make a change from this oauth lucadegasperi / oauth2-server-laravel to the passport with the migration to laravel 5.6. However in my authentication the oauth always returns me access denied, my midddleware is like this.

use Closure;
use Illuminate\Http\Request;
use Response;
use Input;
use Hash;
use App\Usuarios;
use Lang;
use Auth;

use Laravel\Passport\TokenRepository;
use League\OAuth2\Server\ResourceServer;
use League\OAuth2\Server\Exception\OAuthServerException;
use Symfony\Bridge\PsrHttpMessage\Factory\DiactorosFactory;


class OAuth2Middleware {


protected $server;
protected $tokens;

public function __construct(ResourceServer $server, TokenRepository $tokens) {
    $this->server = $server;
    $this->tokens = $tokens;
}



/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next)
{

    try {
        if( $this->validateToken( $request) === null ){

            return $next($request);
        }
    }
    catch (Exception\InvalidRequestException $e) { }
    catch (Exception\AccessDeniedException $e) { }

    $http_codigo = 499;
    $retorno['tipo'] = 'erro';
    $retorno['mensagem'] = Lang::get('webservice.acesso_negado');

    return response( json_encode( $retorno ) , $http_codigo);
}



public function validateToken(Request $request, $localCall = false) {

    try {
        $psr = (new DiactorosFactory)->createRequest($request);
        $psr = $this->server->validateAuthenticatedRequest($psr);

        $token = $this->tokens->find(
            $psr->getAttribute('oauth_access_token_id')
        );

        $currentDate = new DateTime();
        $tokenExpireDate = new DateTime($token->expires_at);

        $isAuthenticated = $tokenExpireDate > $currentDate ? true : false;

        if($localCall) {
            return $isAuthenticated;
        }
        else {
            return json_encode(array('authenticated' => $isAuthenticated));
        }
    } catch (OAuthServerException $e) {
        if($localCall) {
            return false;
        }
        else {
            return json_encode(array('error' => 'Algo ocorreu de errado com a autenticação'));
        }
    }
}

}

but when you run this method validateAuthenticatedRequest() return not authorizate ever ! The request is right, with bearer and others informations ,What could be going wrong based on the posted code? Any help is welcome, I do not know what else to do..

Tks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire