简体   繁体   English

Laravel Auth0未经授权的用户

[英]Laravel Auth0 Unauthorized user

My application is a single page app using Angular 1.x on the client side and Laravel 5.3 for my server/api. 我的应用程序是一个单页面应用程序,在客户端使用Angular 1.x,在我的服务器/ api上使用Laravel 5.3。 I easily managed to make the Auth0 authentication working on my client side (angular) and it successfully generates a token. 我很容易设法使Auth0身份验证在我的客户端(角度)工作,并成功生成一个令牌。 Moving to my api (laravel), unfortunately I can't access any routes that is protected by the auth0.jwt middleware even though the Authorization header is present. 移动到我的api(laravel),遗憾的是我无法访问受auth0.jwt中间件保护的任何路由,即使存在Authorization标头。 Its response is a simple text that says Unauthorized user . 它的响应是一个简单的文字,说Unauthorized user

I'm using Chrome postman to test out the routes on my api. 我正在使用Chrome邮递员测试我的api上的路线。 Auth0中间件使用Chrome邮递员路由受保护的呼叫

I tried to trace down the function that is responsible for the response by checking the vendor\\auth0\\login\\src\\Auth0\\Login\\Middleware\\Auth0JWTMiddleware.php and found out that the CoreException is being thrown. 我试图通过检查vendor\\auth0\\login\\src\\Auth0\\Login\\Middleware\\Auth0JWTMiddleware.php来追踪负责响应的函数,并发现正在抛出CoreException

Here's the handle method of the Auth0JWTMIddleware: 这是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);
    }

My suspect is that the token that generates from Auth0 has newer algorithm or something and the Laravel Auth0 package doesn't already supports it. 我怀疑是从Auth0生成的令牌有更新的算法或什么,而Laravel Auth0包还没有支持它。

I followed exactly the documentation provided by Auth0 and I also cloned their sample projects just to make sure the configurations are correct but unfortunately it doesn't work also. 我完全遵循Auth0提供的文档,我还克隆了他们的示例项目,以确保配置正确但不幸的是它也不起作用。 Any thoughts or ideas on how can I solve my issue? 关于如何解决我的问题的任何想法或想法? Thanks in advance! 提前致谢!

I had the same problem. 我有同样的问题。 There were 2 things I had to change in order to resolve. 我必须改变两件事才能解决。 Credit to @Kangoo13 for the first suggestion: 感谢@ Kangoo13的第一个建议:

1; 1; Check that in your config/laravel-auth0.php file that secret_base64_encoded = false. 在config / laravel-auth0.php文件中检查secret_base64_encoded = false。 You will notice in your Auth0 dashboard next to your key it states "The client secret is not base64 encoded" 您会注意到您的密钥旁边的Auth0仪表板中显示“客户端密钥不是base64编码的”

 'secret_base64_encoded'  => false,

2; 2; in the same config file, Check that 'supported' has the correct spelling. 在同一个配置文件中,检查“支持”是否具有正确的拼写。 It would appear someone has incorrectly typed "suported", if you've just applied the default config file after running composer then chances are this is wrong! 如果您在运行composer之后刚刚应用了默认配置文件,那么有人会错误地键入“suported”,那么这可能是错误的!

Looking in JWTVerifier.php it does appear to cater for the misspelled key but it will default to 'HS256', Auth0 guide explicitly states you should be using 'RS256: 看看JWTVerifier.php它看起来似乎是为了迎合拼写错误的密钥,但它默认为'HS256',Auth0指南明确指出你应该使用'RS256:

 'supported_algs'        => ['RS256'],

Hope that helps! 希望有所帮助!

I read the suported_algs issue above but skim read it and missed the fact you have to correct the spelling for it to work, spent an extra day trying to figure it out before re-reading. 我阅读了上面的suported_algs问题,但是略读了它并且错过了你必须纠正拼写才能工作的事实,花了一天时间试图在重新阅读之前解决它。 Hopefully next person to read it sees this and doesn't miss the spelling issue! 希望下一个阅读它的人看到这个并且不会错过拼写问题! Thanks @user1286856 谢谢@ user1286856

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM