refactor: simplify decode method

This commit is contained in:
Jonas Roussel
2025-02-03 10:10:30 +01:00
parent a3d2358ee3
commit c3a908d3d3

View File

@ -188,7 +188,7 @@ class JWT {
}
/// Decode a token without checking its signature
///
///
/// This also sets [JWT.audience], [JWT.subject], [JWT.issuer], and
/// [JWT.jwtId] even though they are not verified. Use with caution.
static JWT decode(String token) {
@ -204,18 +204,19 @@ class JWT {
payload = utf8.decode(base64.decode(base64Padded(parts[1])));
}
if (header == null || header is! Map<String, dynamic>) {
return JWT(payload);
} else {
return JWT(
payload,
header: header,
audience: _parseAud(payload['aud']),
issuer: payload['iss']?.toString(),
subject: payload['sub']?.toString(),
jwtId: payload['jti']?.toString(),
);
}
final audiance = _parseAud(payload['aud']);
final issuer = payload['iss']?.toString();
final subject = payload['sub']?.toString();
final jwtId = payload['jti']?.toString();
return JWT(
payload,
header: header is! Map<String, dynamic> ? null : header,
audience: audiance,
issuer: issuer,
subject: subject,
jwtId: jwtId,
);
} catch (ex, stackTrace) {
if (ex is Exception && ex is! JWTException) {
throw JWTUndefinedException(ex, stackTrace);