graphql-auth
Advanced tools
Comparing version 0.1.2 to 0.2.0
@@ -6,2 +6,3 @@ 'use strict'; | ||
}); | ||
exports.AuthorizationError = exports.ContextError = undefined; | ||
@@ -28,2 +29,3 @@ var _promise = require('babel-runtime/core-js/promise'); | ||
exports.ContextError = ContextError; | ||
class AuthorizationError extends Error { | ||
@@ -37,7 +39,12 @@ constructor(message = 'Permission Denied!') { | ||
exports.AuthorizationError = AuthorizationError; | ||
function validateScope(required, provided) { | ||
let hasScope = false; | ||
required.map(scope => { | ||
if (provided.includes(scope)) hasScope = true; | ||
required.forEach(scope => { | ||
provided.forEach(function (perm) { | ||
// user:* -> user:create, user:view:self | ||
var permRe = new RegExp('^' + perm.replace('*', '.*') + '$'); | ||
if (permRe.exec(scope)) hasScope = true; | ||
}); | ||
}); | ||
@@ -53,3 +60,3 @@ | ||
return (() => { | ||
var _ref = (0, _asyncToGenerator3.default)(function* (_, __, context) { | ||
var _ref = (0, _asyncToGenerator3.default)(function* (_, __, context, info) { | ||
if (!context.auth) return new ContextError(); | ||
@@ -59,3 +66,3 @@ if (!context.auth.isAuthenticated) return new AuthorizationError('Not Authenticated!'); | ||
if (requiredScope && typeof requiredScope === 'function') requiredScope = yield _promise2.default.resolve().then(function () { | ||
return requiredScope(_, __, context); | ||
return requiredScope(_, __, context, info); | ||
}); | ||
@@ -67,6 +74,6 @@ | ||
return next(_, __, context); | ||
return next(_, __, context, info); | ||
}); | ||
return function (_x, _x2, _x3) { | ||
return function (_x, _x2, _x3, _x4) { | ||
return _ref.apply(this, arguments); | ||
@@ -73,0 +80,0 @@ }; |
@@ -35,3 +35,3 @@ export class ContextError extends Error { | ||
return async function(_, __, context) { | ||
return async function(_, __, context, info) { | ||
if (!context.auth) return new ContextError(); | ||
@@ -43,3 +43,3 @@ if (!context.auth.isAuthenticated) | ||
requiredScope = await Promise.resolve().then(() => | ||
requiredScope(_, __, context), | ||
requiredScope(_, __, context, info), | ||
); | ||
@@ -49,3 +49,4 @@ | ||
(requiredScope && requiredScope.length && !context.auth.scope) || | ||
(requiredScope && requiredScope.length && | ||
(requiredScope && | ||
requiredScope.length && | ||
!validateScope(requiredScope, context.auth.scope)) | ||
@@ -56,4 +57,4 @@ ) { | ||
return next(_, __, context); | ||
return next(_, __, context, info); | ||
}; | ||
} |
{ | ||
"name": "graphql-auth", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "GraphQL authentication and authorization middleware", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
@@ -95,3 +95,3 @@ # GraphQL Auth | ||
Query: { | ||
users: withAuth((root, args, context) => { ... }), | ||
users: withAuth((root, args, context, info) => { ... }), | ||
... | ||
@@ -109,3 +109,3 @@ } | ||
Query: { | ||
users: withAuth(['users:view'], (root, args, context) => { ... }), | ||
users: withAuth(['users:view'], (root, args, context, info) => { ... }), | ||
... | ||
@@ -124,4 +124,4 @@ } | ||
users: withAuth( | ||
(root, args, context) => { /* return scope based on resolver args */ }, | ||
(root, args, context) => { ... }), | ||
(root, args, context, info) => { /* return scope based on resolver args */ }, | ||
(root, args, context, info) => { ... }), | ||
... | ||
@@ -137,6 +137,6 @@ } | ||
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --> | ||
| [<img src="https://avatars1.githubusercontent.com/u/332115?v=4" width="100px;"/><br /><sub>artgibson</sub>](https://github.com/artgibson)<br />[💻](https://github.com/kkemple/graphql-auth/commits?author=artgibson "Code") | [<img src="https://avatars3.githubusercontent.com/u/1265681?v=4" width="100px;"/><br /><sub>HaNdTriX</sub>](http://henrikwenz.de/)<br />[💻](https://github.com/kkemple/graphql-auth/commits?author=HaNdTriX "Code") [📖](https://github.com/kkemple/graphql-auth/commits?author=HaNdTriX "Documentation") | | ||
| :---: | :---: | | ||
| [<img src="https://avatars1.githubusercontent.com/u/332115?v=4" width="100px;"/><br /><sub>artgibson</sub>](https://github.com/artgibson)<br />[💻](https://github.com/kkemple/graphql-auth/commits?author=artgibson "Code") | [<img src="https://avatars3.githubusercontent.com/u/1265681?v=4" width="100px;"/><br /><sub>HaNdTriX</sub>](http://henrikwenz.de/)<br />[💻](https://github.com/kkemple/graphql-auth/commits?author=HaNdTriX "Code") [📖](https://github.com/kkemple/graphql-auth/commits?author=HaNdTriX "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/841956?v=4" width="100px;"/><br /><sub>Pascal Birchler</sub>](https://pascalbirchler.com)<br />[💻](https://github.com/kkemple/graphql-auth/commits?author=swissspidy "Code") | | ||
| :---: | :---: | :---: | | ||
<!-- ALL-CONTRIBUTORS-LIST:END --> | ||
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome! |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
127851
174