简体   繁体   中英

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/

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?

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)

Save the private to the project/micro-service where you generate the token.

Then in that project go to your config->jwt.php file

  1. Set your as 'algo' => env('JWT_ALGO', 'RS256') else it does not sign it.
  2. Set your "keys.private" to the path of your private.pem
  3. Set your keys.public to the path of your 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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