Laravel Auth0未经授权的用户

问题描述 投票:1回答:2

我的应用程序是一个单页面应用程序,在客户端使用Angular 1.x,在我的服务器/ api上使用Laravel 5.3。我很容易设法使Auth0身份验证在我的客户端(角度)工作,并成功生成一个令牌。移动到我的api(laravel),遗憾的是我无法访问受auth0.jwt中间件保护的任何路由,即使存在Authorization标头。它的回答是一个简单的文字,上面写着Unauthorized user

我正在使用Chrome邮递员测试我的api上的路线。 Auth0 Middleware route protected calls using Chrome postman

我试图通过检查vendor\auth0\login\src\Auth0\Login\Middleware\Auth0JWTMiddleware.php来追踪负责响应的函数,并发现CoreException被抛出。

这是Auth0JWTMIddleware的句柄方法:

/**
     * @param $request
     * @param \Closure $next
     *
     * @return mixed
     */
    public function handle($request, \Closure $next)
    {
        $auth0 = \App::make('auth0');

        $token = $this->getToken($request);

        if (!$this->validateToken($token)) {
            return \Response::make('Unauthorized user', 401);
        }

        if ($token) {
            try {
                $jwtUser = $auth0->decodeJWT($token);
            } catch (CoreException $e) {
                return \Response::make('Unauthorized user', 401);
            } catch (InvalidTokenException $e) {
                return \Response::make('Unauthorized user', 401);
            }

            // if it does not represent a valid user, return a HTTP 401
            $user = $this->userRepository->getUserByDecodedJWT($jwtUser);

            if (!$user) {
                return \Response::make('Unauthorized user', 401);
            }

            // lets log the user in so it is accessible
            \Auth::login($user);
        }

        // continue the execution
        return $next($request);
    }

我怀疑是从Auth0生成的令牌有更新的算法或什么,而Laravel Auth0包还没有支持它。

我完全遵循Auth0提供的文档,我还克隆了他们的示例项目,以确保配置正确但不幸的是它也不起作用。关于如何解决我的问题的任何想法或想法?提前致谢!

php angularjs laravel jwt auth0
2个回答
2
投票

我有同样的问题。我必须改变两件事才能解决。感谢@ Kangoo13的第一个建议:

1;在config / laravel-auth0.php文件中检查secret_base64_encoded = false。您会注意到您的密钥旁边的Auth0仪表板中显示“客户端密钥不是base64编码的”

 'secret_base64_encoded'  => false,

2;在同一个配置文件中,检查“支持”是否具有正确的拼写。如果您在运行composer之后刚刚应用了默认配置文件,那么有人会错误地键入“suported”,那么这可能是错误的!

看看JWTVerifier.php它看起来似乎是为了迎合拼写错误的密钥,但它默认为'HS256',Auth0指南明确指出你应该使用'RS256:

 'supported_algs'        => ['RS256'],

希望有所帮助!


1
投票

我阅读了上面的suported_algs问题,但是略读它并且错过了你必须纠正它的拼写才能工作的事实,花了一天时间试图在重新阅读之前解决它。希望下一个阅读它的人看到这个并且不会错过拼写问题!谢谢@ user1286856

© www.soinside.com 2019 - 2024. All rights reserved.