mirror of
https://github.com/jonasroussel/dart_jsonwebtoken.git
synced 2025-05-22 10:36:23 +08:00
push to github
This commit is contained in:
13
.gitignore
vendored
Normal file
13
.gitignore
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
# Files and directories created by pub
|
||||||
|
.dart_tool/
|
||||||
|
.packages
|
||||||
|
|
||||||
|
# Omit commiting pubspec.lock for library packages:
|
||||||
|
# https://dart.dev/guides/libraries/private-files#pubspeclock
|
||||||
|
pubspec.lock
|
||||||
|
|
||||||
|
# Conventional directory for build outputs
|
||||||
|
build/
|
||||||
|
|
||||||
|
# Directory created by dartdoc
|
||||||
|
doc/api/
|
3
CHANGELOG.md
Normal file
3
CHANGELOG.md
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
## 1.0.0
|
||||||
|
|
||||||
|
- Initial version, created by Stagehand
|
22
README.md
Normal file
22
README.md
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
A library for Dart developers.
|
||||||
|
|
||||||
|
Created from templates made available by Stagehand under a BSD-style
|
||||||
|
[license](https://github.com/dart-lang/stagehand/blob/master/LICENSE).
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
A simple usage example:
|
||||||
|
|
||||||
|
```dart
|
||||||
|
import 'package:jsonwebtoken/jsonwebtoken.dart';
|
||||||
|
|
||||||
|
main() {
|
||||||
|
var awesome = new Awesome();
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Features and bugs
|
||||||
|
|
||||||
|
Please file feature requests and bugs at the [issue tracker][tracker].
|
||||||
|
|
||||||
|
[tracker]: http://example.com/issues/replaceme
|
12
example/example.dart
Normal file
12
example/example.dart
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
import 'package:jsonwebtoken/jsonwebtoken.dart';
|
||||||
|
|
||||||
|
main() {
|
||||||
|
final jwt = JWT(payload: {
|
||||||
|
'hello': 'world',
|
||||||
|
'iat': 1590774250,
|
||||||
|
});
|
||||||
|
|
||||||
|
final token = jwt.sign(key: 'test');
|
||||||
|
|
||||||
|
print(token);
|
||||||
|
}
|
3
lib/jsonwebtoken.dart
Normal file
3
lib/jsonwebtoken.dart
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
library jsonwebtoken;
|
||||||
|
|
||||||
|
export 'src/jsonwebtoken.dart';
|
58
lib/src/jsonwebtoken.dart
Normal file
58
lib/src/jsonwebtoken.dart
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:crypto/crypto.dart';
|
||||||
|
|
||||||
|
enum Algorithm {
|
||||||
|
HS256,
|
||||||
|
RS256,
|
||||||
|
}
|
||||||
|
|
||||||
|
class JWT {
|
||||||
|
JWT({
|
||||||
|
this.payload,
|
||||||
|
});
|
||||||
|
|
||||||
|
static String secretKey = null;
|
||||||
|
|
||||||
|
Map<String, dynamic> payload;
|
||||||
|
String audience;
|
||||||
|
String subject;
|
||||||
|
String issuer;
|
||||||
|
|
||||||
|
String signedToken;
|
||||||
|
|
||||||
|
String sign({
|
||||||
|
String key,
|
||||||
|
Algorithm algorithm = Algorithm.HS256,
|
||||||
|
Duration expiresIn = const Duration(days: 7),
|
||||||
|
bool noTimestamp = false,
|
||||||
|
}) {
|
||||||
|
if (key == null && JWT.secretKey != null) key = JWT.secretKey;
|
||||||
|
assert(key != null);
|
||||||
|
|
||||||
|
final headers = {'alg': algorithm.toString().split('.').last, 'typ': 'JWT'};
|
||||||
|
final encodedHeader = _base64Unpadded(_jsonToBase64Url.encode(headers));
|
||||||
|
final encodedPayload = _base64Unpadded(_jsonToBase64Url.encode(payload));
|
||||||
|
|
||||||
|
final body = encodedHeader + '.' + encodedPayload;
|
||||||
|
|
||||||
|
final signature = _base64Unpadded(base64Url.encode(_sign(body, key)));
|
||||||
|
|
||||||
|
return body + '.' + signature;
|
||||||
|
}
|
||||||
|
|
||||||
|
List<int> _sign(String body, String key) {
|
||||||
|
final hmac = Hmac(sha256, utf8.encode(key));
|
||||||
|
return hmac.convert(utf8.encode(body)).bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
final _jsonToBase64Url = json.fuse(utf8.fuse(base64Url));
|
||||||
|
|
||||||
|
String _base64Unpadded(String value) {
|
||||||
|
if (value.endsWith('==')) return value.substring(0, value.length - 2);
|
||||||
|
if (value.endsWith('=')) return value.substring(0, value.length - 1);
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
static JWT verify(String token) {}
|
||||||
|
}
|
10
pubspec.yaml
Normal file
10
pubspec.yaml
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
name: jsonwebtoken
|
||||||
|
description: A starting point for Dart libraries or applications.
|
||||||
|
version: 1.0.0
|
||||||
|
homepage: https://www.example.com
|
||||||
|
|
||||||
|
environment:
|
||||||
|
sdk: '>=2.7.0 <3.0.0'
|
||||||
|
|
||||||
|
dependencies:
|
||||||
|
crypto: ^2.1.5
|
Reference in New Issue
Block a user