简体   繁体   English

尝试验证 JWT 签名 Tymon/jwt-auth laravel

[英]Trying to verify JWT signature Tymon/jwt-auth laravel

I am trying to build a micro service Single Sign On that generates a signed token from a private key.我正在尝试构建一个从私钥生成签名令牌的微服务单点登录。 This token will be used to access micro services.此令牌将用于访问微服务。 So far I have got this part working and a token gets generated.到目前为止,我已经让这部分工作并生成了一个令牌。

However, when I try to verify it with the public key it does not seem to work.但是,当我尝试使用公钥对其进行验证时,它似乎不起作用。

The documents doesnt reveal much.文件没有透露太多。 https://jwt-auth.readthedocs.io/en/develop/lumen-installation/ https://jwt-auth.readthedocs.io/en/develop/lumen-installation/

So the question is, can any other library to verify a JWT token if they have the public key that is associated to the private key?所以问题是,如果其他库拥有与私钥关联的公钥,是否可以验证 JWT 令牌?

Ok so this is how you do it.好的,这就是你的方式。

Follow the installation instructions as the link above.按照上面的链接安装说明。

Then, go to this page https://travistidwell.com/jsencrypt/demo/ Create a private and public key (remember to use key size 1024 - this is what got me)然后,转到此页面https://travistidwell.com/jsencrypt/demo/创建私钥和公钥(记住使用密钥大小 1024 - 这就是我的原因)

Save the private to the project/micro-service where you generate the token.将私有保存到生成令牌的项目/微服务中。

Then in that project go to your config->jwt.php file然后在该项目中转到您的 config->jwt.php 文件

  1. Set your as 'algo' => env('JWT_ALGO', 'RS256') else it does not sign it.将您设置为'algo' => env('JWT_ALGO', 'RS256')否则它不会签名。
  2. Set your "keys.private" to the path of your private.pem将您的“keys.private”设置为您的 private.pem 的路径
  3. Set your keys.public to the path of your public.pem将您的 keys.public 设置为您的 public.pem 的路径

Then to create your token you use this:然后创建你的令牌你使用这个:

$credentials = request(['email', 'password']);
try {
    $token = $JWTAuth->attempt($credentials);

    if(!$token) {
        throw new AccessDeniedHttpException();
    }

} catch (JWTException $e) {
    throw new HttpException(500);
}

return response()->json([
    'status' => 'ok',
    'token' => $token
]);

And you get your token and your token is signed with the private key.你得到你的令牌,你的令牌是用私钥签名的。

Now you can use this token for other micro service outside of your laravel set up.现在,您可以将此令牌用于 Laravel 设置之外的其他微服务。

Should you have another separate set up (other url) with laravel, that needed a trusted token for access, then have the same set up as above (private key is not important here) however your public key is.如果你有另一个单独的 laravel 设置(其他 url),需要一个受信任的令牌来访问,然后设置与上面相同的设置(私钥在这里不重要)但是你的公钥是。

To verify the token, you add this code.要验证令牌,请添加此代码。

JWTAuth::parseToken()->authenticate();

Obviously you can extend this by adding in your claims etc. This is a simple set up.显然,您可以通过添加声明等来扩展它。这是一个简单的设置。 I hope this helps someone.我希望这可以帮助别人。

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

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