我如何在Dart中从jwt令牌中提取过期时间?

5sxhfpxr  于 2022-12-16  发布在  其他
关注(0)|答案(5)|浏览(227)

我从API接收到jwt令牌,但我不知道如何从Dart中的令牌提取过期时间。
接收到的令牌
依吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉吉

wqnecbli

wqnecbli1#

一个优雅的解决方案是使用jwt_decoder包。https://pub.dev/packages/jwt_decoder

  1. flutter pub add jwt_decoder
    1.汇入:
    import 'package:jwt_decoder/jwt_decoder.dart';
    1.获取所有JWT属性:
String yourToken = "Your JWT";
    Map<String, dynamic> decodedToken = JwtDecoder.decode(yourToken);

或者在这种情况下只检查过期日期:

String yourToken = "Your JWT";
bool hasExpired = JwtDecoder.isExpired(yourToken);
dwbf0jvd

dwbf0jvd2#

您可以轻松地使用jwt_decode包。
1.安装jwt_解码

flutter pub add jwt_decode

1.支票已过期

bool hasExpired = Jwt.isExpired(token);

以下是软件包URL https://pub.dev/packages/jwt_decode

pod7payv

pod7payv3#

你可以通过解码它来完成,一般来说,JWT令牌由两部分(对象)组成,在上面的JWT中,解码它的结果是:

{
  alg: "HS256",
  typ: "JWT"
}.
{
  email: "sujeeth91171@gmail.com",
  userId: "5e8adae42108733580f40858",
  iat: 1586158336,
  exp: 1586763136
}.

因此,失效日期是时间戳(1586763136),代表2020年4月13日(星期一)7:32:16 AM。
怎么做?

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代码信用分配给:boformer

brc7rcf0

brc7rcf04#

应使用dart:convert。使用utf8将解码base64,使用json获取Map对象以调用["exp"]属性

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
}
3mpgtkmj

3mpgtkmj5#

如果您想使用软件包,另一种解决方案是:
安装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
}

相关问题