@envelop/generic-auth 0.2.0 → 1.1.0-alpha-e8998c9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -7
- package/index.js +1 -2
- package/index.mjs +1 -2
- package/package.json +2 -2
- package/index.js.map +0 -1
- package/index.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -78,7 +78,7 @@ To setup this mode, use the following config:
|
|
|
78
78
|
|
|
79
79
|
```ts
|
|
80
80
|
import { envelop } from '@envelop/core';
|
|
81
|
-
import { useGenericAuth,
|
|
81
|
+
import { useGenericAuth, ResolveUserFn, ValidateUserFn } from '@envelop/generic-auth';
|
|
82
82
|
|
|
83
83
|
type UserType = {
|
|
84
84
|
id: string;
|
|
@@ -94,7 +94,7 @@ const getEnveloped = envelop({
|
|
|
94
94
|
plugins: [
|
|
95
95
|
// ... other plugins ...
|
|
96
96
|
useGenericAuth({
|
|
97
|
-
|
|
97
|
+
resolveUserFn,
|
|
98
98
|
validateUser,
|
|
99
99
|
mode: 'protect-all',
|
|
100
100
|
}),
|
|
@@ -108,7 +108,7 @@ This mode uses the plugin to inject the authenticated user into the `context`, a
|
|
|
108
108
|
|
|
109
109
|
```ts
|
|
110
110
|
import { envelop } from '@envelop/core';
|
|
111
|
-
import { useGenericAuth,
|
|
111
|
+
import { useGenericAuth, ResolveUserFn, ValidateUserFn } from '@envelop/generic-auth';
|
|
112
112
|
|
|
113
113
|
type UserType = {
|
|
114
114
|
id: string;
|
|
@@ -124,7 +124,7 @@ const getEnveloped = envelop({
|
|
|
124
124
|
plugins: [
|
|
125
125
|
// ... other plugins ...
|
|
126
126
|
useGenericAuth({
|
|
127
|
-
|
|
127
|
+
resolveUserFn,
|
|
128
128
|
validateUser,
|
|
129
129
|
mode: 'resolve-only',
|
|
130
130
|
}),
|
|
@@ -153,7 +153,7 @@ This mode is similar to option #2, but it uses `@auth` SDL directive to automati
|
|
|
153
153
|
|
|
154
154
|
```ts
|
|
155
155
|
import { envelop } from '@envelop/core';
|
|
156
|
-
import { useGenericAuth,
|
|
156
|
+
import { useGenericAuth, ResolveUserFn, ValidateUserFn } from '@envelop/generic-auth';
|
|
157
157
|
|
|
158
158
|
type UserType = {
|
|
159
159
|
id: string;
|
|
@@ -169,7 +169,7 @@ const getEnveloped = envelop({
|
|
|
169
169
|
plugins: [
|
|
170
170
|
// ... other plugins ...
|
|
171
171
|
useGenericAuth({
|
|
172
|
-
|
|
172
|
+
resolveUserFn,
|
|
173
173
|
validateUser,
|
|
174
174
|
mode: 'protect-auth-directive',
|
|
175
175
|
}),
|
|
@@ -234,7 +234,7 @@ const validateUser: ValidateUserFn<UserType> = async (user, context, { root, arg
|
|
|
234
234
|
throw new Error(`Unauthenticated!`);
|
|
235
235
|
}
|
|
236
236
|
|
|
237
|
-
const valueNode =
|
|
237
|
+
const valueNode = directiveNode.arguments.find(arg => arg.name.value === 'role').value as EnumValueNode;
|
|
238
238
|
const role = valueNode.value;
|
|
239
239
|
|
|
240
240
|
if (role !== user.role) {
|
package/index.js
CHANGED
|
@@ -55,7 +55,7 @@ const useGenericAuth = (options) => {
|
|
|
55
55
|
const user = await options.resolveUserFn(context);
|
|
56
56
|
extendContext({
|
|
57
57
|
[fieldName]: user,
|
|
58
|
-
validateUser
|
|
58
|
+
validateUser,
|
|
59
59
|
});
|
|
60
60
|
},
|
|
61
61
|
onExecute() {
|
|
@@ -83,4 +83,3 @@ exports.UnauthenticatedError = UnauthenticatedError;
|
|
|
83
83
|
exports.defaultValidateFn = defaultValidateFn;
|
|
84
84
|
exports.getDirective = getDirective;
|
|
85
85
|
exports.useGenericAuth = useGenericAuth;
|
|
86
|
-
//# sourceMappingURL=index.js.map
|
package/index.mjs
CHANGED
|
@@ -51,7 +51,7 @@ const useGenericAuth = (options) => {
|
|
|
51
51
|
const user = await options.resolveUserFn(context);
|
|
52
52
|
extendContext({
|
|
53
53
|
[fieldName]: user,
|
|
54
|
-
validateUser
|
|
54
|
+
validateUser,
|
|
55
55
|
});
|
|
56
56
|
},
|
|
57
57
|
onExecute() {
|
|
@@ -75,4 +75,3 @@ const useGenericAuth = (options) => {
|
|
|
75
75
|
};
|
|
76
76
|
|
|
77
77
|
export { DIRECTIVE_SDL, UnauthenticatedError, defaultValidateFn, getDirective, useGenericAuth };
|
|
78
|
-
//# sourceMappingURL=index.mjs.map
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@envelop/generic-auth",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.1.0-alpha-e8998c9.0",
|
|
4
4
|
"sideEffects": false,
|
|
5
5
|
"peerDependencies": {
|
|
6
|
-
"graphql": "^14.0.0 || ^15.0.0"
|
|
6
|
+
"graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
|
|
7
7
|
},
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
package/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../dist/plugins/generic-auth/src/utils.js","../../../dist/plugins/generic-auth/src/index.js"],"sourcesContent":["export function getDirective(info, name) {\n var _a;\n const { parentType, fieldName, schema } = info;\n const schemaType = schema.getType(parentType.name);\n const field = schemaType.getFields()[fieldName];\n const astNode = field.astNode;\n const authDirective = (_a = astNode === null || astNode === void 0 ? void 0 : astNode.directives) === null || _a === void 0 ? void 0 : _a.find(d => d.name.value === name);\n return authDirective || null;\n}\n//# sourceMappingURL=utils.js.map","import { GraphQLError } from 'graphql';\nimport { getDirective } from './utils';\nexport * from './utils';\nexport class UnauthenticatedError extends GraphQLError {\n}\nexport const DIRECTIVE_SDL = /* GraphQL */ `\n directive @auth on FIELD_DEFINITION\n`;\nexport function defaultValidateFn(user, contextType) {\n if (!user) {\n throw new UnauthenticatedError('Unauthenticated!');\n }\n}\nexport const useGenericAuth = (options) => {\n const fieldName = options.contextFieldName || 'currentUser';\n const validateUser = options.validateUser || defaultValidateFn;\n if (options.mode === 'protect-all') {\n return {\n async onContextBuilding({ context, extendContext }) {\n const user = await options.resolveUserFn(context);\n await validateUser(user, context);\n extendContext({\n [fieldName]: user,\n });\n },\n };\n }\n else if (options.mode === 'resolve-only') {\n return {\n async onContextBuilding({ context, extendContext }) {\n const user = await options.resolveUserFn(context);\n extendContext({\n [fieldName]: user,\n validateUser: () => validateUser(user, context),\n });\n },\n };\n }\n else if (options.mode === 'protect-auth-directive') {\n return {\n async onContextBuilding({ context, extendContext }) {\n const user = await options.resolveUserFn(context);\n extendContext({\n [fieldName]: user,\n validateUser: () => validateUser(user, context),\n });\n },\n onExecute() {\n return {\n async onResolverCalled({ args, root, context, info }) {\n const authDirectiveNode = getDirective(info, options.authDirectiveName || 'auth');\n if (authDirectiveNode) {\n await context.validateUser(context[fieldName], context, {\n info,\n context: context,\n args,\n root,\n }, authDirectiveNode);\n }\n },\n };\n },\n };\n }\n return {};\n};\n//# sourceMappingURL=index.js.map"],"names":["GraphQLError"],"mappings":";;;;;;AAAO,SAAS,YAAY,CAAC,IAAI,EAAE,IAAI,EAAE;AACzC,IAAI,IAAI,EAAE,CAAC;AACX,IAAI,MAAM,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC;AACnD,IAAI,MAAM,UAAU,GAAG,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,CAAC;AACvD,IAAI,MAAM,KAAK,GAAG,UAAU,CAAC,SAAS,EAAE,CAAC,SAAS,CAAC,CAAC;AACpD,IAAI,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC;AAClC,IAAI,MAAM,aAAa,GAAG,CAAC,EAAE,GAAG,OAAO,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,CAAC,GAAG,KAAK,CAAC,GAAG,OAAO,CAAC,UAAU,MAAM,IAAI,IAAI,EAAE,KAAK,KAAK,CAAC,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,CAAC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,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../../../dist/plugins/generic-auth/src/utils.js","../../../dist/plugins/generic-auth/src/index.js"],"sourcesContent":["export function getDirective(info, name) {\n var _a;\n const { parentType, fieldName, schema } = info;\n const schemaType = schema.getType(parentType.name);\n const field = schemaType.getFields()[fieldName];\n const astNode = field.astNode;\n const authDirective = (_a = astNode === null || astNode === void 0 ? void 0 : astNode.directives) === null || _a === void 0 ? void 0 : _a.find(d => d.name.value === name);\n return authDirective || null;\n}\n//# sourceMappingURL=utils.js.map","import { GraphQLError } from 'graphql';\nimport { getDirective } from './utils';\nexport * from './utils';\nexport class UnauthenticatedError extends GraphQLError {\n}\nexport const DIRECTIVE_SDL = /* GraphQL */ `\n directive @auth on FIELD_DEFINITION\n`;\nexport function defaultValidateFn(user, contextType) {\n if (!user) {\n throw new UnauthenticatedError('Unauthenticated!');\n }\n}\nexport const useGenericAuth = (options) => {\n const fieldName = options.contextFieldName || 'currentUser';\n const validateUser = options.validateUser || defaultValidateFn;\n if (options.mode === 'protect-all') {\n return {\n async onContextBuilding({ context, extendContext }) {\n const user = await options.resolveUserFn(context);\n await validateUser(user, context);\n extendContext({\n [fieldName]: user,\n });\n },\n };\n }\n else if (options.mode === 'resolve-only') {\n return {\n async onContextBuilding({ context, extendContext }) {\n const user = await options.resolveUserFn(context);\n extendContext({\n [fieldName]: user,\n validateUser: () => validateUser(user, context),\n });\n },\n };\n }\n else if (options.mode === 'protect-auth-directive') {\n return {\n async onContextBuilding({ context, extendContext }) {\n const user = await options.resolveUserFn(context);\n extendContext({\n [fieldName]: user,\n validateUser: () => validateUser(user, context),\n });\n },\n onExecute() {\n return {\n async onResolverCalled({ args, root, context, info }) {\n const authDirectiveNode = getDirective(info, options.authDirectiveName || 'auth');\n if (authDirectiveNode) {\n await context.validateUser(context[fieldName], context, {\n info,\n context: context,\n args,\n root,\n }, authDirectiveNode);\n }\n },\n };\n },\n };\n }\n return {};\n};\n//# sourceMappingURL=index.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