function ownKeys(object, enumerableOnly) {
var keys = Object.keys(object);
if (Object.getOwnPropertySymbols) {
var symbols = Object.getOwnPropertySymbols(object);
if (enumerableOnly)
symbols = symbols.filter(function (sym) {
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
});
keys.push.apply(keys, symbols);
}
return keys;
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true,
});
} else {
obj[key] = value;
}
return obj;
}
function _objectSpread(target) {
for (var i = 1; i < arguments.length; i++) {
var source = arguments[i] != null ? arguments[i] : {};
if (i % 2) {
ownKeys(source, true).forEach(function (key) {
_defineProperty(target, key, source[key]);
});
} else if (Object.getOwnPropertyDescriptors) {
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
} else {
ownKeys(source).forEach(function (key) {
Object.defineProperty(
target,
key,
Object.getOwnPropertyDescriptor(source, key)
);
});
}
}
return target;
}
class MyHasScopeDirective extends graphqltools.SchemaDirectiveVisitor {
visitFieldDefinition(field) {
var expectedScopes = this.args.scopes;
var next = field.resolve; // wrap resolver with auth check
field.resolve = function (result, args, context, info) {
console.log(
"Field name " +
field.name +
" VFD - looking for scope " +
expectedScopes
);
var decoded = verifyAndDecodeToken({
context: context,
}); // FIXME: override with env var
var scopes =
decoded["Scopes"] ||
decoded["scopes"] ||
decoded["Scope"] ||
decoded["scope"] ||
[];
// if any requested scope matches
if (
expectedScopes.some(function (scope) {
return scopes.indexOf(scope) !== -1;
}) ||
expectedScopes.some(function (scope) {
return scopes.indexOf("[" + field.name + "]") !== -1;
})
) {
return next(
result,
args,
_objectSpread({}, context, {
user: decoded,
}),
info
);
}
throw new _errors.AuthorizationError({
message: "You are not authorized for this resource",
});
};
}
visitObject(obj) {
var fields = obj.getFields();
var expectedScopes = this.args.scopes;
Object.keys(fields).forEach(function (fieldName) {
var field = fields[fieldName];
var next = field.resolve;
field.resolve = function (result, args, context, info) {
console.log(
"Field name " +
field.name +
" VFD - looking for scope " +
expectedScopes
);
var decoded = verifyAndDecodeToken({
context: context,
}); // FIXME: override w/ env var
var scopes =
decoded["Scopes"] ||
decoded["scopes"] ||
decoded["Scope"] ||
decoded["scope"] ||
[];
if (
expectedScopes.some(function (role) {
return scopes.indexOf(role) !== -1;
})
) {
return next(
result,
args,
_objectSpread({}, context, {
user: decoded,
}),
info
);
}
throw new _errors.AuthorizationError({
message: "You are not authorized for this resource",
});
};
});
}
}
const schema = graphql.makeAugmentedSchema({
typeDefs,
mutations: true,
config: {
auth: {
isAuthenticated: true,
hasRole: true,
hasScope: true,
},
},
schemaDirectives: { hasScope: MyHasScopeDirective },
});