uruk-project / Jwt

JSON Web Token implementation for .Net & .Net Core
MIT License
84 stars 13 forks source link

Writer freezes while writing a token #421

Closed MortalFlesh closed 4 years ago

MortalFlesh commented 4 years ago

Hello,

I'm trying to generate a token, but it freezes at writing... am I doing something wrong?

I'm using F# with .netcore 3.1 on Mac OS (10.15.4).

printfn "JWT.create ..."
use key = new SymmetricJwk("R9MyWaEoyiMYViVWo8Fk4TUGWiSoaW6U1nOqXri8_XU", SignatureAlgorithm.HmacSha256)
printfn " -> key ok"

let descriptor =
    JwsDescriptor(
        KeyId = key.ToString(),
        JwtId = Guid.NewGuid().ToString(),
        IssuedAt = (DateTime.UtcNow |> Nullable),
        ExpirationTime = (DateTime.UtcNow.AddMinutes(30.0) |> Nullable),
        Issuer = (currentApp |> SoftwareComponent.value),
        Audience = (currentApp |> SoftwareComponent.value)
    )
    |> tee (fun d -> d.ToString() |> printfn " -> descriptor ok \n %A")

printfn " -> writer"
let writer = JwtWriter()
printfn " -> writing token ..."
let token = writer.WriteTokenString(descriptor)
printfn " -> Token:\n%A" token

Output is:

JWT.create ...
 -> key ok
 -> descriptor ok 
 "{
  "kid": "{\n  \u0022kty\u0022: \u0022oct\u0022,\n  \u0022alg\u0022: \u0022HS256\u0022,\n  \u0022k\u0022: \u0022R9MyWaEoyiMYViVWo8Fk4TUGWiSoaW6U1nOqXri8_XU\u0022\n}"
}
.
{
  "jti": "2e9996df-b8ca-4e7a-a369-8ae1b8d843ff",
  "iat": 1588085668,
  "exp": 1588087468,
  "iss": "sc-eligibilityConfigurator-common-local@dev1-services",
  "aud": "sc-eligibilityConfigurator-common-local@dev1-services"
}"
 -> writer
 -> writing token ...
ycrumeyrolle commented 4 years ago

Hi,

When running on Windows, I have the following exception: JsonWebToken.JwtDescriptorException: The header parameter 'alg' is required. I will try to identify the freezing issue with MacOS.

You are not using correctly the key, and the signing algorithm cannot be infered. You should replace the instruction

let descriptor =
    JwsDescriptor(
           KeyId = key.ToString(),

by

let descriptor =
    JwsDescriptor(
           SigningKey = key,

The KeyId property only the identifier of the key, and written to the kid header parameter. If you are the owner of the key, I recommend to define the kid on the key instead to specify directly on the descriptor.

MortalFlesh commented 4 years ago

Hello and thanks for a quick reply!

It works with a correct key SigningKey = key.

I used a KeyId because of the example, where there is a Key = key, but I couldn't find a Key property, so I though a KeyId is the closest and then I forget about it, so I didn't try a different property.

Thank you for help 👍

ycrumeyrolle commented 4 years ago

Good catch. The example in the readme is updated.