commit 0b5585e7ebfcd7eb75900cc941a8e26b87bfdbda Author: Jonas Roussel Date: Sat May 30 01:14:08 2020 +0200 push to github diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0c44ab0 --- /dev/null +++ b/.gitignore @@ -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/ diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..687440b --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,3 @@ +## 1.0.0 + +- Initial version, created by Stagehand diff --git a/README.md b/README.md new file mode 100644 index 0000000..2913721 --- /dev/null +++ b/README.md @@ -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 diff --git a/example/example.dart b/example/example.dart new file mode 100644 index 0000000..07258c0 --- /dev/null +++ b/example/example.dart @@ -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); +} diff --git a/lib/jsonwebtoken.dart b/lib/jsonwebtoken.dart new file mode 100644 index 0000000..9830f96 --- /dev/null +++ b/lib/jsonwebtoken.dart @@ -0,0 +1,3 @@ +library jsonwebtoken; + +export 'src/jsonwebtoken.dart'; diff --git a/lib/src/jsonwebtoken.dart b/lib/src/jsonwebtoken.dart new file mode 100644 index 0000000..d58452e --- /dev/null +++ b/lib/src/jsonwebtoken.dart @@ -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 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 _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) {} +} diff --git a/pubspec.yaml b/pubspec.yaml new file mode 100644 index 0000000..5f6902e --- /dev/null +++ b/pubspec.yaml @@ -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