How to get the claims from a JWT in my Flutter Application

后端 未结 3 762
旧时难觅i
旧时难觅i 2020-12-15 18:55

I am writing a Flutter/Dart application and am getting a JWT back from an auth server that has some claims I need to use. I have looked at various (4 so far) Dart JWT libra

3条回答
  •  情歌与酒
    2020-12-15 19:47

    As of this writing, the jaguar_jwt package is being actively maintained. Although it is not clearly documented, it does have a public method that will decode Base64Url encoding. It does basically the same thing as the accepted answer.

    //import 'package:jaguar_jwt/jaguar_jwt.dart';
    
    final String token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1NTQ4MjAxNjIsImlhdCI6MTU1NDc3Njk2MiwiaXNzIjoiU3VyYWdjaCIsInN1YiI6IjQifQ.bg5B_k9WCmxiu2epuZo_Tpt_KZC4N9ve_2GEdrulcXM';
    final parts = token.split('.');
    final payload = parts[1];
    final String decoded = B64urlEncRfc7515.decodeUtf8(payload);
    

    This gives a JSON string, which for this particular example is:

    {
      "exp":1554820162,
      "iat":1554776962,
      "iss":"Suragch",
      "sub":"4"
    }
    

    See also:

    • JWT: The Complete Guide to JSON Web Tokens
    • String based data encoding: Base64 vs Base64url

提交回复
热议问题