5

can anyone explain how to decode a token in json using dart.

i done in android with this below code. But how to decode a token in dart.

public class JWTUtils {

    public static String  decoded(String JWTEncoded) throws Exception {
        String encode = "";
        try {
            String[] split = JWTEncoded.split("\\.");
            Log.d("JWT_DECODED", "Header: " + getJson(split[0]));
            encode = getJson(split[1]);
        } catch (UnsupportedEncodingException e) {
            //Error
        }
        return encode;
    }

    private static String getJson(String strEncoded) throws UnsupportedEncodingException{
        byte[] decodedBytes = Base64.decode(strEncoded, Base64.URL_SAFE);
        return new String(decodedBytes, "UTF-8");
    }
}

String encodeddata = JWTUtils.decoded(token);

kartheeki j
  • 2,206
  • 5
  • 27
  • 51
  • I think this plugin will help https://pub.dartlang.org/packages/jwt – Yamin Aug 14 '18 at 11:56
  • @Yamin Pub says that the package is not healthy. – creativecreatorormaybenot Aug 14 '18 at 16:08
  • Check this one too, https://pub.dartlang.org/packages/jaguar_jwt . if it's not working fine, I'll write another plugin. – Yamin Aug 18 '18 at 06:58
  • I have found not one of those packages useful -- for some reason they are demanding the secret key to decode the JWT .. this doesn't make any sense and is not realistic. I'm still searching on how to get the claims from a JWT I am receiving from a server... – sjmcdowall Aug 25 '18 at 12:21
  • Possible duplicate of [How to get the claims from a JWT in my Flutter Application](https://stackoverflow.com/questions/52017389/how-to-get-the-claims-from-a-jwt-in-my-flutter-application) – Herohtar Jan 12 '19 at 11:03
  • @bofomar posted an awesome response to, what appears, to be the same question I asked. Here is a link to that response, which is working perfectly for me: [Precise answer to decoding a JWT in Dart 2](https://stackoverflow.com/questions/52017389/how-to-get-the-claims-from-a-jwt-in-my-flutter-application) – sjmcdowall Aug 26 '18 at 15:01

1 Answers1

5

If you is interested in get the public part of token basically you have to split the token by '.' and decode the second part with base64

var text = token.split('.')[1];
var decoded = base64.decode(text);
return utf8.decode(decoded);


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));
 }