'How can I extract expire time from jwt token in Dart?
I receive jwt token from the api but i don't know how to extract the expire time from the token in Dart.
The token which is received
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InN1amVldGg5MTE3MUBnbWFpbC5jb20iLCJ1c2VySWQiOiI1ZThhZGFlNDIxMDg3MzM1ODBmNDA4NTgiLCJpYXQiOjE1ODYxNTgzMzYsImV4cCI6MTU4Njc2MzEzNn0.EwLTdRXaibNmcbuqVxzEDSfrW37z3eWYIxAifAUsT5I
Solution 1:[1]
An elegant solution would be using the jwt_decoder package.
flutter pub add jwt_decoder- Import it:
import 'package:jwt_decoder/jwt_decoder.dart';
- Get all JWT properties:
String yourToken = "Your JWT";
Map<String, dynamic> decodedToken = JwtDecoder.decode(yourToken);
or check only the expiration if that's the case:
String yourToken = "Your JWT";
bool hasExpired = JwtDecoder.isExpired(yourToken);
Solution 2:[2]
You can do it by decoding it, Speaking in general, JWT token conssist two part (objects), in the above JWT the result of decoding it is :
{
alg: "HS256",
typ: "JWT"
}.
{
email: "[email protected]",
userId: "5e8adae42108733580f40858",
iat: 1586158336,
exp: 1586763136
}.
So the expire date is a timestamp (1586763136) which stand for Monday, April 13, 2020 7:32:16 AM.
How ?
import 'dart:convert';
Map<String, dynamic> parseJwt(String token) {
final parts = token.split('.');
if (parts.length != 3) {
throw Exception('invalid token');
}
final payload = _decodeBase64(parts[1]);
final payloadMap = json.decode(payload);
if (payloadMap is! Map<String, dynamic>) {
throw Exception('invalid payload');
}
return payloadMap;
}
String _decodeBase64(String str) {
String output = str.replaceAll('-', '+').replaceAll('_', '/');
switch (output.length % 4) {
case 0:
break;
case 2:
output += '==';
break;
case 3:
output += '=';
break;
default:
throw Exception('Illegal base64url string!"');
}
return utf8.decode(base64Url.decode(output));
}
Dart code credits goes to :boformer
Solution 3:[3]
You should use dart:convert. With utf8 you will decode base64 and with json get Map object to call ["exp"] property
import 'dart:convert';
String decodeBase64(String toDecode) {
String res;
try {
while (toDecode.length * 6 % 8 != 0) {
toDecode += "=";
}
res = utf8.decode(base64.decode(toDecode));
} catch (error) {
throw Exception("decodeBase64([toDecode=$toDecode]) \n\t\terror: $error");
}
return res;
}
void main () {
final token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InN1amVldGg5MTE3MUBnbWFpbC5jb20iLCJ1c2VySWQiOiI1ZThhZGFlNDIxMDg3MzM1ODBmNDA4NTgiLCJpYXQiOjE1ODYxNTgzMzYsImV4cCI6MTU4Njc2MzEzNn0.EwLTdRXaibNmcbuqVxzEDSfrW37z3eWYIxAifAUsT5I';
final decoded = json.decode(decodeBase64(token.split(".")[1]));
int exp = decoded["exp"];
print(exp); // 1586763136
}
Solution 4:[4]
An alternative solution if you want to use a package:
Install corsac_jwt: https://pub.dev/packages/corsac_jwt#-installing-tab-
import 'package:corsac_jwt/corsac_jwt.dart';
import 'package:corsac_jwt/corsac_jwt.dart';
void main() {
final parsed = JWT.parse('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InN1amVldGg5MTE3MUBnbWFpbC5jb20iLCJ1c2VySWQiOiI1ZThhZGFlNDIxMDg3MzM1ODBmNDA4NTgiLCJpYXQiOjE1ODYxNTgzMzYsImV4cCI6MTU4Njc2MzEzNn0.EwLTdRXaibNmcbuqVxzEDSfrW37z3eWYIxAifAUsT5I');
print(DateTime.fromMillisecondsSinceEpoch(parsed.expiresAt * 1000, isUtc: true)); // 2020-04-13 07:32:16.000Z
}
Solution 5:[5]
You can easily use the jwt_decode package.
- install jwt_decode
flutter pub add jwt_decode
- check isExpired
bool hasExpired = Jwt.isExpired(token);
following is package URL https://pub.dev/packages/jwt_decode
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sen Alexandru |
| Solution 2 | gharabat |
| Solution 3 | Kirill Matrosov |
| Solution 4 | julemand101 |
| Solution 5 | fuzes |
