Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get "alg" from JWK? #86

Open
chulkilee opened this issue May 11, 2020 · 0 comments
Open

How to get "alg" from JWK? #86

chulkilee opened this issue May 11, 2020 · 0 comments

Comments

@chulkilee
Copy link

chulkilee commented May 11, 2020

How can I get alg to use in JWT from JOSE.JWK? Such as RSA256.

I figured out how to do it by checking byte sizes in n - but curious is there better way (e.g. avoid Base64 decoding)

# from jwt.io
public_key_pem = """
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnzyis1ZjfNB0bBgKFMSv
vkTtwlvBsaJq7S5wA+kzeVOVpVWwkWdVha4s38XM/pa/yr47av7+z3VTmvDRyAHc
aT92whREFpLv9cj5lTeJSibyr/Mrm/YtjCZVWgaOYIhwrXwKLqPr/11inWsAkfIy
tvHWTxZYEcXLgAXFuUuaS3uF9gEiNQwzGTU1v0FqkqTBr4B8nW3HCN47XUu0t8Y0
e+lf4s4OxQawWD79J9/5d3Ry0vbV3Am1FtGJiJvOwRsIfVChDpYStTcHTCMqtvWb
V6L11BWkpzGXSW4Hv43qa+GSYOD2QU68Mb59oSk2OB+BtOLpJofmbGEGgvmwyCI9
MwIDAQAB
-----END PUBLIC KEY-----
"""

{_, jwk_map} =
  public_key_pem
  |> JOSE.JWK.from_pem()
  |> JOSE.JWK.to_map()

alg =
  case jwk_map do
    %{"kty" => "RSA", "n" => n} ->
      "RS" <> (n |> Base.url_decode64!(padding: false) |> byte_size() |> Integer.to_string())
  end

Is there easier way to get the rsa key size at least?

@chulkilee chulkilee changed the title How to get "kty" from JWK? How to get "alg" from JWK? May 11, 2020
@potatosalad potatosalad added this to the jose 2.0.0 milestone Sep 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: To do
Development

No branches or pull requests

2 participants