Rails - How to get public key from JSON web keys?
Asked Answered
B

1

5

I obtained the following jwks from the server

    "keys": [
        {
            "kty": "RSA",
            "e": "AQAB",
            "use": "sig",
            "alg": "RS256",
            "kid": "92595710205972640",
            "n": "l75bY3woSGVAYYDR6qE0tbzxqpYLr1-zz_ShSiFOOpa-taJenFWipHm7AR2GyG36bJeHgAo8OtqcTprWwHi3NjhxQZ0bj-hyr02ZxufvGnOIJzQgPsKym9jlWIXZSZ4apJI06RbaJ95Z9ekQ-36IR8NZ5VuV3aQZd753FDVjMciyqin6ofNSE5kq-fkWf4hbyEqgKFz4SeqjFhThhUaMrzs_uyNAZnaIzyAgxRmtMxJ5Mci8L7aX9DLhAMiUAOvSGGyRI1r9KlRxEcqNlFCdYi8G74ehPorwTY6-sW3ZG6U_sA5Ns9GUmuLIkSIbueqWv_tGxYcx4ntAO6zRQocinw"
        }
    ]
}

I have an encoded_token. I want to decrypt it with the above jwk. How to get the public key from the above jwk. I followed the steps from https://auth0.com/docs/quickstart/backend/rails/01-authorization. But x5c (certificate) is missing in the above jwk.

I want to know how to fetch the public key without x5c in the jwk.

Blubber answered 2/4, 2020 at 9:47 Comment(0)
V
9

How to get the public key from the above jwk.

I could get public key from jwt lib.

irb(main):001:0> require 'jwt'
=> true
irb(main):002:0> jwk = {
irb(main):003:1*   "kty": "RSA",
irb(main):004:1*   "e": "AQAB",
irb(main):005:1*   "use": "sig",
irb(main):006:1*   "alg": "RS256",
irb(main):007:1*   "kid": "92595710205972640",
irb(main):008:1*   "n": "l75bY3woSGVAYYDR6qE0tbzxqpYLr1-zz_ShSiFOOpa-taJenFWipHm7AR2GyG36bJeHgAo8OtqcTprWwHi3NjhxQZ0bj-hyr02ZxufvGnOIJzQgPsKym9jlWIXZSZ4apJI06RbaJ95Z9ekQ-36IR8NZ5VuV3aQZd753FDVjMciyqin6ofNSE5kq-fkWf4hbyEqgKFz4SeqjFhThhUaMrzs_uyNAZnaIzyAgxRmtMxJ5Mci8L7aX9DLhAMiUAOvSGGyRI1r9KlRxEcqNlFCdYi8G74ehPorwTY6-sW3ZG6U_sA5Ns9GUmuLIkSIbueqWv_tGxYcx4ntAO6zRQocinw"
irb(main):009:1> }
irb(main):010:0>
irb(main):011:0> public_key = JWT::JWK::RSA.import(jwk).public_key
=> #<OpenSSL::PKey::RSA:0x00007f970287b810>
irb(main):012:0>
irb(main):013:0> puts public_key.to_pem
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAl75bY3woSGVAYYDR6qE0
tbzxqpYLr1+zz/ShSiFOOpa+taJenFWipHm7AR2GyG36bJeHgAo8OtqcTprWwHi3
NjhxQZ0bj+hyr02ZxufvGnOIJzQgPsKym9jlWIXZSZ4apJI06RbaJ95Z9ekQ+36I
R8NZ5VuV3aQZd753FDVjMciyqin6ofNSE5kq+fkWf4hbyEqgKFz4SeqjFhThhUaM
rzs/uyNAZnaIzyAgxRmtMxJ5Mci8L7aX9DLhAMiUAOvSGGyRI1r9KlRxEcqNlFCd
Yi8G74ehPorwTY6+sW3ZG6U/sA5Ns9GUmuLIkSIbueqWv/tGxYcx4ntAO6zRQoci
nwIDAQAB
-----END PUBLIC KEY-----
Vespucci answered 12/8, 2020 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.