From 3377ae8b0b0353f4f23c10c03f67806ead1f8535 Mon Sep 17 00:00:00 2001 From: Jonas Roussel Date: Sun, 31 May 2020 18:38:13 +0200 Subject: [PATCH] v0.2.1 --- CHANGELOG.md | 8 ++++ README.md | 47 ++++++++++++++++++- example/example.dart | 2 +- ...onwebtoken.dart => dart_jsonwebtoken.dart} | 0 lib/src/algorithms.dart | 2 +- lib/src/jwt.dart | 8 ++-- pubspec.yaml | 6 +-- 7 files changed, 63 insertions(+), 10 deletions(-) rename lib/{jsonwebtoken.dart => dart_jsonwebtoken.dart} (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 02dcd1a..e6e9c17 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## 0.2.1 + +- Formatting + +## 0.2.0 + +- Better documentations + ## 0.1.0 - First version with every based features diff --git a/README.md b/README.md index beaca04..7e9083b 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,47 @@ # JsonWebToken -[![pub package](https://img.shields.io/pub/v/jsonwebtoken.svg)](https://pub.dev/packages/jsonwebtoken) +[![pub package](https://img.shields.io/pub/v/dart_jsonwebtoken.svg)](https://pub.dev/packages/dart_jsonwebtoken) + +A dart implementation of the famous javascript library `jsonwebtoken`. + +## Usage + +### Import +```dart +import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart'; +``` + +### Sign + +```dart +// 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('secret-key'); + +print('Signed token: $token\n'); +``` + +### Verify + +```dart +try { + // Verify a token + final jwt = JWT.verify(token, 'secret-key'); + + print('Payload: ${jwt.payload}'); +} on JWTExpiredError { + print('jwt expired'); +} on JWTError catch (ex) { + print(ex.message); // ex: invalid signature +} +``` diff --git a/example/example.dart b/example/example.dart index 7c4682f..8dd45f1 100644 --- a/example/example.dart +++ b/example/example.dart @@ -1,4 +1,4 @@ -import 'package:jsonwebtoken/jsonwebtoken.dart'; +import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart'; main() { String token; diff --git a/lib/jsonwebtoken.dart b/lib/dart_jsonwebtoken.dart similarity index 100% rename from lib/jsonwebtoken.dart rename to lib/dart_jsonwebtoken.dart diff --git a/lib/src/algorithms.dart b/lib/src/algorithms.dart index be653ba..c3d87c5 100644 --- a/lib/src/algorithms.dart +++ b/lib/src/algorithms.dart @@ -1,7 +1,7 @@ import 'dart:convert'; import 'package:crypto/crypto.dart'; -import 'package:jsonwebtoken/jsonwebtoken.dart'; +import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart'; abstract class JWTAlgorithm { static const HS256 = HS256Algorithm(); diff --git a/lib/src/jwt.dart b/lib/src/jwt.dart index f723957..fb22bd1 100644 --- a/lib/src/jwt.dart +++ b/lib/src/jwt.dart @@ -1,6 +1,6 @@ import 'dart:convert'; -import 'package:jsonwebtoken/jsonwebtoken.dart'; +import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart'; import './utils.dart'; @@ -39,8 +39,8 @@ class JWT { issuer: payload.remove('iss'), subject: payload.remove('sub'), ); - } on JWTError catch (ex) { - throw ex; + } on JWTError { + rethrow; } catch (ex) { throw JWTInvalidError('jwt invalid'); } @@ -61,7 +61,7 @@ class JWT { String sign( String key, { JWTAlgorithm algorithm = JWTAlgorithm.HS256, - Duration expiresIn = null, + Duration expiresIn, bool noTimestamp = false, }) { final header = {'alg': algorithm.name, 'typ': 'JWT'}; diff --git a/pubspec.yaml b/pubspec.yaml index 718bd84..75d2d78 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ -name: jsonwebtoken -description: A dart implementation of JSON Web Tokens. -version: 0.1.0 +name: dart_jsonwebtoken +description: A dart implementation of the famous javascript library 'jsonwebtoken'. +version: 0.2.1 repository: https://github.com/jonasroussel/jsonwebtoken homepage: https://github.com/jonasroussel/jsonwebtoken#readme