This commit is contained in:
Jonas Roussel
2020-05-31 18:38:13 +02:00
parent 420b56a996
commit 3377ae8b0b
7 changed files with 63 additions and 10 deletions

View File

@ -1,3 +1,11 @@
## 0.2.1
- Formatting
## 0.2.0
- Better documentations
## 0.1.0 ## 0.1.0
- First version with every based features - First version with every based features

View File

@ -1,2 +1,47 @@
# JsonWebToken # 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
}
```

View File

@ -1,4 +1,4 @@
import 'package:jsonwebtoken/jsonwebtoken.dart'; import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
main() { main() {
String token; String token;

View File

@ -1,7 +1,7 @@
import 'dart:convert'; import 'dart:convert';
import 'package:crypto/crypto.dart'; import 'package:crypto/crypto.dart';
import 'package:jsonwebtoken/jsonwebtoken.dart'; import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
abstract class JWTAlgorithm { abstract class JWTAlgorithm {
static const HS256 = HS256Algorithm(); static const HS256 = HS256Algorithm();

View File

@ -1,6 +1,6 @@
import 'dart:convert'; import 'dart:convert';
import 'package:jsonwebtoken/jsonwebtoken.dart'; import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
import './utils.dart'; import './utils.dart';
@ -39,8 +39,8 @@ class JWT {
issuer: payload.remove('iss'), issuer: payload.remove('iss'),
subject: payload.remove('sub'), subject: payload.remove('sub'),
); );
} on JWTError catch (ex) { } on JWTError {
throw ex; rethrow;
} catch (ex) { } catch (ex) {
throw JWTInvalidError('jwt invalid'); throw JWTInvalidError('jwt invalid');
} }
@ -61,7 +61,7 @@ class JWT {
String sign( String sign(
String key, { String key, {
JWTAlgorithm algorithm = JWTAlgorithm.HS256, JWTAlgorithm algorithm = JWTAlgorithm.HS256,
Duration expiresIn = null, Duration expiresIn,
bool noTimestamp = false, bool noTimestamp = false,
}) { }) {
final header = {'alg': algorithm.name, 'typ': 'JWT'}; final header = {'alg': algorithm.name, 'typ': 'JWT'};

View File

@ -1,6 +1,6 @@
name: jsonwebtoken name: dart_jsonwebtoken
description: A dart implementation of JSON Web Tokens. description: A dart implementation of the famous javascript library 'jsonwebtoken'.
version: 0.1.0 version: 0.2.1
repository: https://github.com/jonasroussel/jsonwebtoken repository: https://github.com/jonasroussel/jsonwebtoken
homepage: https://github.com/jonasroussel/jsonwebtoken#readme homepage: https://github.com/jonasroussel/jsonwebtoken#readme