mirror of
https://github.com/jonasroussel/dart_jsonwebtoken.git
synced 2025-05-22 18:46:30 +08:00
v0.2.1
This commit is contained in:
@ -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
|
||||||
|
47
README.md
47
README.md
@ -1,2 +1,47 @@
|
|||||||
# JsonWebToken
|
# JsonWebToken
|
||||||
[](https://pub.dev/packages/jsonwebtoken)
|
[](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
|
||||||
|
}
|
||||||
|
```
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import 'package:jsonwebtoken/jsonwebtoken.dart';
|
import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
String token;
|
String token;
|
||||||
|
@ -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();
|
||||||
|
@ -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'};
|
||||||
|
@ -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
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user