jaguar_jwt 3.0.0 jaguar_jwt: ^3.0.0 copied to clipboard
Provides JWT utilities for Dart including issuing a token, verifing a token and parsing a token.
jaguar_jwt #
JWT utilities for Dart and Jaguar.dart
This library can be used to generate and process JSON Web Tokens (JWT). For more information about JSON Web Tokens, see RFC 7519.
Currently, only the HMAC SHA-256 algorithm is supported to generate/process a JSON Web Signature (JWS).
Usage #
Issuing a JWT #
final key = 's3cr3t';
final claimSet = JwtClaim(
subject: 'kleak',
issuer: 'teja',
audience: <String>['audience1.example.com', 'audience2.example.com'],
otherClaims: <String,dynamic>{
'typ': 'authnresponse',
'pld': {'k': 'v'}},
maxAge: const Duration(minutes: 5));
String token = issueJwtHS256(claimSet, key);
print(token);
Processing a JWT #
To process a JWT:
- Verify the signature and extract the claim set.
- Validate the claim set.
- Extract claims from the claim set.
try {
final JwtClaim decClaimSet = verifyJwtHS256Signature(token, key);
// print(decClaimSet);
decClaimSet.validate(issuer: 'teja', audience: 'audience1.example.com');
if (claimSet.jwtId != null) {
print(claimSet.jwtId);
}
if (claimSet.containsKey('typ')) {
final v = claimSet['typ'];
if (v is String) {
print(v);
} else {
...
}
}
...
} on JwtException {
...
}
Configuration #
JwtClaimSet #
JwtClaimSet
is the model to holds JWT claim set information.
These are the registered claims:
issuer
Authority issuing the token. This will be used during authorization to verify that expected issuer has issued the token. Fills theiss
field of the JWT.subject
Subject of the token. Usually stores the user ID of the user to which the token is issued. Fills thesub
field of the JWT.audience
List of audience that accept this token. This will be used during authorization to verify that JWT has expected audience for the service. Fillsaud
field in JWT.expiry
Time when the token becomes no longer acceptable for process. Fillsexp
field in JWT.notBefore
Time when the token becomes acceptable for processing. Fills thenbf
field in the JWT.issuedAt
Time when the token was issued. Fills theiat
field in the JWT.jwtId
Unique identifier across services that identifies the token. Fillsjti
field in JWT.
Additional claims may also be included in the JWT.