Last active
September 24, 2022 10:21
-
-
Save ConfidenceYobo/cbb6c293871fd97b4beb870a0ca08353 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:convert'; | |
import 'dart:math'; | |
import 'dart:io'; | |
class Token { | |
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)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment