From 30d5293612f859b35d0164920c98b24c9411c405 Mon Sep 17 00:00:00 2001 From: thosakwe Date: Wed, 6 Jul 2016 09:48:28 -0400 Subject: [PATCH] Next up is JWT, and finish local tests --- .gitignore | 1 + lib/angel_auth.dart | 2 ++ lib/middleware/require_auth.dart | 31 +++++++++++++++++++++++++------ lib/strategies/token.dart | 15 +++++++++++++++ pubspec.yaml | 4 ++-- 5 files changed, 45 insertions(+), 8 deletions(-) create mode 100644 lib/strategies/token.dart diff --git a/.gitignore b/.gitignore index 39d64b3c..a6816c59 100644 --- a/.gitignore +++ b/.gitignore @@ -85,3 +85,4 @@ fabric.properties # Don't commit pubspec lock file # (Library packages only! Remove pattern if developing an application package) +.idea \ No newline at end of file diff --git a/lib/angel_auth.dart b/lib/angel_auth.dart index 11b49503..43bf967c 100644 --- a/lib/angel_auth.dart +++ b/lib/angel_auth.dart @@ -14,6 +14,8 @@ part 'middleware/serialization.dart'; part 'strategies/local.dart'; +part 'strategies/token.dart'; + part 'strategies/oauth2.dart'; _validateString(String str) { diff --git a/lib/middleware/require_auth.dart b/lib/middleware/require_auth.dart index 19c60841..4a2c3948 100644 --- a/lib/middleware/require_auth.dart +++ b/lib/middleware/require_auth.dart @@ -3,11 +3,30 @@ part of angel_auth; /// Restricts access to a resource via authentication. Future requireAuth(RequestContext req, ResponseContext res, {bool throws: true}) async { + reject() { + if (throws) { + res.status(HttpStatus.UNAUTHORIZED); + throw new AngelHttpException.Forbidden(); + } else + return false; + } + if (req.session.containsKey('userId')) return true; - else if (throws) { - res.status(HttpStatus.UNAUTHORIZED); - throw new AngelHttpException.Forbidden(); - } - else return false; -} \ No newline at end of file + else if (req.headers.value("Authorization") != null) { + var jwt = req.headers + .value("Authorization") + .replaceAll(new RegExp(r"^Bearer", caseSensitive: false), "") + .trim(); + + var split = jwt.split("."); + if (split.length != 3) return reject(); + + Map header = JSON.decode(UTF8.decode(BASE64URL.decode(split[0]))); + + if (header['typ'] != "JWT" || header['alg'] != "HS256") return reject(); + + Map payload = JSON.decode(UTF8.decode(BASE64URL.decode(split[1]))); + } else + return reject(); +} diff --git a/lib/strategies/token.dart b/lib/strategies/token.dart new file mode 100644 index 00000000..b27f87cc --- /dev/null +++ b/lib/strategies/token.dart @@ -0,0 +1,15 @@ +part of angel_auth; + +class JwtAuthStrategy extends AuthStrategy { + + @override + Future authenticate(RequestContext req, ResponseContext res, + [AngelAuthOptions options]) { + + } + + @override + Future canLogout(RequestContext req, ResponseContext res) { + + } +} \ No newline at end of file diff --git a/pubspec.yaml b/pubspec.yaml index 9f2662e3..f8ebb4de 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -4,8 +4,8 @@ version: 1.0.0-dev+5 author: Tobe O homepage: https://github.com/angel-dart/angel_auth dependencies: - angel_framework: ">=0.0.0-dev < 0.1.0" - crypto: ">= 1.1.1 < 2.0.0" + angel_framework: ">=1.0.0-dev <2.0.0" + crypto: ">=2.0.0 <3.0.0" oauth2: ">= 1.0.2 < 2.0.0" dev_dependencies: http: ">= 0.11.3 < 0.12.0"