jonasroussel / dart_jsonwebtoken

A dart implementation of the famous javascript library 'jsonwebtoken'
MIT License
87 stars 29 forks source link

Failure to sign with explicit header in token. #48

Closed RubenGarcia closed 11 months ago

RubenGarcia commented 11 months ago

dart_jsonwebtoken: ^2.12.0

This code

    Map<String, String> h = {
      "alg": "HS256",
      "typ": "JWT",
    };
    Map<String, dynamic> p = {
      "sub": "1234567890",
      "name": "John Doe",
      "iat": 1516239022
    };
    SecretKey s = SecretKey("");
    JWTAlgorithm a = JWTAlgorithm.HS256;

    final j = JWT (p, header: h);
    final t = j.sign(s, algorithm: a);

Fails with error

type '_Map<String, dynamic>' is not a subtype of type 'Map<String, String>' of 'other'

in line

      header!.addAll({'alg': algorithm.name, 'typ': 'JWT'});
RubenGarcia commented 11 months ago

Removing the header argument results in a correct token.

jonasroussel commented 11 months ago

Hi @RubenGarcia, It seems to be a map typing problem, I think the function addAll is not the right method to add the default values of alg and typ. I'll see if I can fix the problem.

jonasroussel commented 11 months ago

Fixed in the last version! https://pub.dev/packages/dart_jsonwebtoken/versions/2.12.1