dart_jsonwebtoken 1.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 72

JSON Web Token #

pub package

A dart implementation of the famous javascript library jsonwebtoken.

JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims securely between two parties. https://jwt.io allows you to decode, verify and generate JWT.

Usage #

Import #

import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';

Sign #

// Create a json web token
final jwt = JWT(
  payload: {
    'id': 123,
    'server': {
      'id': '3e4fc296',
      'loc': 'euw-2',
    }
  },
  issuer: 'https://github.com/jonasroussel/jsonwebtoken',
);

// Sign it (default with HS256 algorithm)
token = jwt.sign(SecretKey('secret passphrase'));

print('Signed token: $token\n');

Verify #

try {
  // Verify a token
  final jwt = JWT.verify(token, SecretKey('secret passphrase'));

  print('Payload: ${jwt.payload}');
} on JWTExpiredError {
  print('jwt expired');
} on JWTError catch (ex) {
  print(ex.message); // ex: invalid signature
}

Supported Algorithms #

  • HS256 (HMAC / SHA256)
  • RS256 (RSA / SHA256)

1.0.0 #

  • New RSA Algorithm (RS256)
  • Keys a now using an abstract class 'Key' instead of a string
  • SecretKey: for HMAC (HS256)
  • PrivateKey & PublicKey: for RSA (RS256)

0.2.1 #

  • Formatting

0.2.0 #

  • Better documentations

0.1.0 #

  • First version with every based features

example/example.dart

import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';

main() {
  String token;

  /* Sign */ {
    // Create a json web token
    final jwt = JWT(
      payload: {
        'id': 123,
        'server': {
          'id': '3e4fc296',
          'loc': 'euw-2',
        }
      },
      issuer: 'https://github.com/jonasroussel/jsonwebtoken',
    );

    // Sign it
    token = jwt.sign(SecretKey('secret passphrase'));

    print('Signed token: $token\n');
  }

  /* Verify */ {
    try {
      // Verify a token
      final jwt = JWT.verify(token, SecretKey('secret passphrase'));

      print('Payload: ${jwt.payload}');
    } on JWTExpiredError {
      print('jwt expired');
    } on JWTError catch (ex) {
      print(ex.message); // ex: invalid signature
    }
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  dart_jsonwebtoken: ^1.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
45
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
72
Learn more about scoring.

We analyzed this package on Jul 10, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.15

Health issues and suggestions

Document public APIs. (-1 points)

52 out of 52 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Format lib/src/algorithms.dart.

Run dartfmt to format lib/src/algorithms.dart.

Format lib/src/jwt.dart.

Run dartfmt to format lib/src/jwt.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.3.0 <3.0.0
crypto ^2.1.5 2.1.5
pointycastle ^1.0.2 1.0.2
rsa_pkcs ^1.1.3 1.1.3
Transitive dependencies
asn1lib 0.6.5
charcode 1.1.3
collection 1.14.13 1.15.0-nullsafety
convert 2.1.1
typed_data 1.2.0 1.3.0-nullsafety