feat: add support for base64 encoded secrets (#54)

This commit is contained in:
Jakub Sosna
2024-04-25 15:27:39 +02:00
committed by GitHub
parent c59770c06a
commit adbd3b7046
2 changed files with 8 additions and 2 deletions

View File

@ -147,7 +147,12 @@ class HMACAlgorithm extends JWTAlgorithm {
assert(key is SecretKey, 'key must be a SecretKey'); assert(key is SecretKey, 'key must be a SecretKey');
final secretKey = key as SecretKey; final secretKey = key as SecretKey;
final hmac = Hmac(_getHash(name), utf8.encode(secretKey.key)); final hmac = Hmac(
_getHash(name),
secretKey.isBase64Encoded
? base64Decode(secretKey.key)
: utf8.encode(secretKey.key),
);
return Uint8List.fromList(hmac.convert(body).bytes); return Uint8List.fromList(hmac.convert(body).bytes);
} }

View File

@ -11,8 +11,9 @@ abstract class JWTKey {}
/// For HMAC algorithms /// For HMAC algorithms
class SecretKey extends JWTKey { class SecretKey extends JWTKey {
String key; String key;
bool isBase64Encoded;
SecretKey(this.key); SecretKey(this.key, {this.isBase64Encoded = false});
} }
/// For RSA algorithm, in sign method /// For RSA algorithm, in sign method