operator [] method
Retrieves the value of a claim.
The claimName
can be the Claim Name for either registered claims or
non-registered claims. But for registered claims, for type-safety, it
may be better to use its corresponding member variables.
Returns null if the claim is not present or the Claim Value is the null value. Use the containsKey method to distinguish between the absence of a claim and the presence of a claim whose value is null.
Note: when the claim name is 'aud', this method returns null when there is no Audience Claim (unlike the audience member variable, which will be an empty list).
Implementation
dynamic? operator [](String claimName) {
if (!registeredClaimNames.contains(claimName)) {
// Non-registered claim
return _otherClaims[claimName];
} else {
// Registered claim
switch (claimName) {
case 'iss':
return issuer;
case 'sub':
return subject;
case 'aud':
return audience;
case 'exp':
return expiry;
case 'nbf':
return notBefore;
case 'iat':
return issuedAt;
case 'jti':
return jwtId;
default:
// coding error: all the registered claims should have been covered
throw UnsupportedError('bad non-registered claim: $claimName');
}
}
}