Skip to content

Commit

Permalink
v2.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasroussel committed Aug 26, 2023
1 parent 1206d1a commit 8b16229
Show file tree
Hide file tree
Showing 11 changed files with 441 additions and 56 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.11.0

- Removing `basic_utils` package that was incompatible with flutter web
- Moving utils to `helpers.dart` and key parsing functions into `key_parser.dart`
- Adding some new examples in `example/example.dart`

## 2.10.0

- New ECDSA algorithm (ES256K)
Expand Down
47 changes: 47 additions & 0 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ void main() {
print('----- RSA-PSS SHA-256 -----');
ps256();
print('---------------------------\n');

print('----- RSA Certificate -----');
rsaCert();
print('---------------------------\n');
}

// HMAC SHA-256 algorithm
Expand Down Expand Up @@ -232,3 +236,46 @@ void ps256() {
}
}
}

// RSA Certificate
void rsaCert() {
String token;

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

// Sign it
final pem = File('./example/rsa_cert_private.pem').readAsStringSync();
final key = RSAPrivateKey(pem);

token = jwt.sign(key, algorithm: JWTAlgorithm.RS256);

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

/* Verify */ {
try {
// Verify a token
final pem = File('./example/rsa_certificate.pem').readAsStringSync();
final key = RSAPublicKey.cert(pem);

final jwt = JWT.verify(token, key);

print('Payload: ${jwt.payload}');
} on JWTExpiredException {
print('jwt expired');
} on JWTException catch (ex) {
print(ex.message); // ex: invalid signature
}
}
}
28 changes: 28 additions & 0 deletions example/rsa_cert_private.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDHr1vavB+pZ0e/
0vp7fzElDST0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Bgk8UIOHAHzpFdMqzxAV9n8m2fC3JLgNTI0C5kwsXbryWusVDgthYyM
kq+W8KbreveVGLegsH1ZvXKMZXtg6pXmE3E58cM0YB4Yvc1WgjbLFvg0iwKBgQCY
2TdD1/b9BmLzXs0pr3TfKv+Gy/d3XENpTLFacHuRRi8kbTazNZntkGAR1rYqgN+o
M87om72LO+L19Oywai0LQjR5GgA76kT9OZOvnv6zgXBPlsPUb/h7aKap8rrE/Fkx
BUQ3kTzxHY5W3esGQdiSk33aMkV2BZa9NvPiG8erdQKBgDlpbRVv3ypjDQxdgccT
4K0zyWgG0597PXF+wBCV3+1FryarzlBCaSN53daE/s+XzJh8pZEN5zV8SkjKdI+W
ccDgCZ/FL15OB/AuE6I6/T4g5NWXDqqWt5qMP+SU7oK+IpzpRZyq56SSLCXzvMzk
+PD5J7pSXLRdv9DJURCvZ2ny
-----END PRIVATE KEY-----
9 changes: 9 additions & 0 deletions example/rsa_cert_public.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAx69b2rwfqWdHv9L6e38x
JQ0k9E336wBry76OkFJp/AHczRhIsrVthnmBgTBBp4vyPPf0untElrFamlyY6g5V
6nn2VMz828n3fVEcGodL3BxSpPmzO2bLzsb5UYSmMMd8GjczpXH3rZQeYaRQoh6L
aFCS/bG1Y6AcjG7NwUD/O4A0kQH5IHTYcre90HuJsZVwaOwMvt3USyWZlSHoCQ0N
ircZbINuZ2J/OH6TkB5c6FOATEikWb1X5SlG5SZZacFxEw7w+dhZbyE3qNqnXk2n
qwVJTllQGA99YSc3z1CMp7eC2e5OqH+95ZAR/d2TnFX2HsghrbeHxjDSYiccYx5F
ZwIDAQAB
-----END PUBLIC KEY-----
20 changes: 20 additions & 0 deletions example/rsa_certificate.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-----BEGIN CERTIFICATE-----
MIIDMjCCA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wAYqzLFK8m59KRCz+eD1Ho/cO9NC/rZ
vwLdy6e2
-----END CERTIFICATE-----
2 changes: 1 addition & 1 deletion lib/src/algorithms.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:pointycastle/pointycastle.dart' as pc;

import 'exceptions.dart';
import 'keys.dart';
import 'utils.dart';
import 'helpers.dart';

abstract class JWTAlgorithm {
/// HMAC using SHA-256 hash algorithm
Expand Down
20 changes: 6 additions & 14 deletions lib/src/utils.dart → lib/src/helpers.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:convert';

import 'dart:typed_data';

final jsonBase64 = json.fuse(utf8.fuse(base64Url));
Expand Down Expand Up @@ -90,18 +89,11 @@ BigInt bigIntFromBytes(Uint8List bytes) {
return bytes.fold(BigInt.zero, (a, b) => a * _b256 + BigInt.from(b));
}

Uint8List hexToUint8List(String hex) {
if (hex.length % 2 != 0) {
throw 'Odd number of hex digits';
}
var l = hex.length ~/ 2;
var result = Uint8List(l);
for (var i = 0; i < l; ++i) {
var x = int.parse(hex.substring(2 * i, 2 * (i + 1)), radix: 16);
if (x.isNaN) {
throw 'Expected hex string';
}
result[i] = x;
List<String> chunkString(String s, int chunkSize) {
var chunked = <String>[];
for (var i = 0; i < s.length; i += chunkSize) {
var end = (i + chunkSize < s.length) ? i + chunkSize : s.length;
chunked.add(s.substring(i, end));
}
return result;
return chunked;
}
2 changes: 1 addition & 1 deletion lib/src/jwt.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:collection/collection.dart';
import 'algorithms.dart';
import 'exceptions.dart';
import 'keys.dart';
import 'utils.dart';
import 'helpers.dart';

class JWT {
/// Verify a token.
Expand Down
Loading

0 comments on commit 8b16229

Please sign in to comment.