mirror of
https://github.com/jonasroussel/dart_jsonwebtoken.git
synced 2025-05-19 17:16:17 +08:00
v2.0.0-nullsafety.2
This commit is contained in:
@ -1,3 +1,9 @@
|
||||
## 2.0.0-nullsafety.2
|
||||
|
||||
- New EdDSA Algorithm (EdDSA)
|
||||
- EdDSAPrivateKey and EdDSAPublicKey, two new keys for EdDSA algorithm
|
||||
- `ed25519_edwards` package has been added
|
||||
|
||||
## 2.0.0-nullsafety.1
|
||||
|
||||
- Null safety migration of this package
|
||||
|
@ -63,3 +63,4 @@ RS512 | RSASSA-PKCS1-v1_5 using SHA-512 hash algorithm
|
||||
ES256 | ECDSA using P-256 curve and SHA-256 hash algorithm
|
||||
ES384 | ECDSA using P-384 curve and SHA-384 hash algorithm
|
||||
ES512 | ECDSA using P-521 curve and SHA-512 hash algorithm
|
||||
EdDSA | EdDSA using edwards25519 curve and SHA-512 hash algorithm
|
||||
|
@ -38,7 +38,7 @@ abstract class JWTAlgorithm {
|
||||
static const ES512 = _ECDSAAlgorithm('ES512');
|
||||
|
||||
/// EdDSA using Ed25519 curve algorithm
|
||||
static const Ed25519 = _EdDSAAlgorithm('EdDSA');
|
||||
static const EdDSA = _EdDSAAlgorithm('EdDSA');
|
||||
|
||||
/// Return the `JWTAlgorithm` from his string name
|
||||
static JWTAlgorithm fromName(String name) {
|
||||
@ -62,7 +62,7 @@ abstract class JWTAlgorithm {
|
||||
case 'ES512':
|
||||
return JWTAlgorithm.ES512;
|
||||
case 'EdDSA':
|
||||
return JWTAlgorithm.Ed25519;
|
||||
return JWTAlgorithm.EdDSA;
|
||||
default:
|
||||
throw JWTInvalidError('unknown algorithm');
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
name: dart_jsonwebtoken
|
||||
description: A dart implementation of the famous javascript library 'jsonwebtoken' (JWT).
|
||||
version: 2.0.0-nullsafety.1
|
||||
version: 2.0.0-nullsafety.2
|
||||
repository: https://github.com/jonasroussel/jsonwebtoken
|
||||
homepage: https://github.com/jonasroussel/jsonwebtoken#readme
|
||||
|
||||
|
@ -1,70 +0,0 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
|
||||
import 'package:dart_jsonwebtoken/src/jwt.dart';
|
||||
import 'package:ed25519_edwards/ed25519_edwards.dart' as ed;
|
||||
import 'package:test/test.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
List<int> _base64ToBytes(String encoded) {
|
||||
encoded += List.filled((4 - encoded.length % 4) % 4, '=').join();
|
||||
return base64Url.decode(encoded);
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('JWT Examples', () {
|
||||
String token;
|
||||
|
||||
var d = _base64ToBytes('nWGxne_9WmC6hEr0kuwsxERJxWl7MmkZcDusAxyuf2A');
|
||||
var x = _base64ToBytes('11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo');
|
||||
var privateKeyBytes = <int>[];
|
||||
privateKeyBytes.addAll(d);
|
||||
privateKeyBytes.addAll(x);
|
||||
var publicKey = EdDSAPublicKey(x);
|
||||
var privateKey = EdDSAPrivateKey(privateKeyBytes);
|
||||
{
|
||||
final jwt = JWT('Example of Ed25519 signing');
|
||||
|
||||
token = jwt.sign(privateKey,
|
||||
algorithm: JWTAlgorithm.Ed25519, noIssueAt: true);
|
||||
|
||||
print('Signed token: $token\n');
|
||||
expect(token,
|
||||
'eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCJ9.RXhhbXBsZSBvZiBFZDI1NTE5IHNpZ25pbmc.x2iH_vGNptaSYh3czRpZpW_W37qTwu63pHxesEjw367bDHZ44uVma-ZrH31QSJmJCpPdA7kAlWBTIgwfKGO4CA');
|
||||
}
|
||||
|
||||
try {
|
||||
final jwt = JWT.verify(token, publicKey);
|
||||
|
||||
print('Payload: ${jwt.payload}');
|
||||
} on JWTExpiredError {
|
||||
print('jwt expired');
|
||||
} on JWTError catch (ex) {
|
||||
print(ex.message); // ex: invalid signature
|
||||
}
|
||||
});
|
||||
|
||||
test('test sign & verify', () {
|
||||
var keyPair = ed.generateKey();
|
||||
var public = EdDSAPublicKey(keyPair.publicKey!.bytes);
|
||||
var private = EdDSAPrivateKey(keyPair.privateKey!.bytes);
|
||||
final jwt = JWT({
|
||||
'uid': Uuid().v4(),
|
||||
'sid': Uuid().v4(),
|
||||
'iat': (DateTime.now().millisecondsSinceEpoch / 1000).floor(),
|
||||
'exp': (DateTime.now().add(Duration(days: 365)).millisecondsSinceEpoch /
|
||||
1000)
|
||||
.floor(),
|
||||
'jti': Uuid().v4(),
|
||||
'sig': Uuid().v4(),
|
||||
'scp': 'FULL',
|
||||
});
|
||||
|
||||
var token = jwt.sign(private, algorithm: JWTAlgorithm.Ed25519);
|
||||
print('Signed token: $token\n');
|
||||
|
||||
final verifiedJwt = JWT.verify(token, public);
|
||||
|
||||
print('VerifiedJwt Payload: ${verifiedJwt.payload}');
|
||||
});
|
||||
}
|
Reference in New Issue
Block a user