@envelop/generic-auth 4.3.1 → 4.3.2-alpha-1eb54ef.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/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@envelop/generic-auth",
3
- "version": "4.3.1",
3
+ "version": "4.3.2-alpha-1eb54ef.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
- "@envelop/core": "^2.4.1",
6
+ "@envelop/core": "2.4.2-alpha-1eb54ef.0",
7
7
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"
8
8
  },
9
9
  "dependencies": {
10
- "@envelop/extended-validation": "^1.7.1"
10
+ "@envelop/extended-validation": "1.7.2-alpha-1eb54ef.0"
11
11
  },
12
12
  "repository": {
13
13
  "type": "git",
@@ -26,7 +26,7 @@
26
26
  "exports": {
27
27
  ".": {
28
28
  "require": {
29
- "types": "./typings/index.d.ts",
29
+ "types": "./typings/index.d.cts",
30
30
  "default": "./cjs/index.js"
31
31
  },
32
32
  "import": {
@@ -40,7 +40,7 @@
40
40
  },
41
41
  "./*": {
42
42
  "require": {
43
- "types": "./typings/*.d.ts",
43
+ "types": "./typings/*.d.cts",
44
44
  "default": "./cjs/*.js"
45
45
  },
46
46
  "import": {
@@ -0,0 +1,80 @@
1
+ import { DefaultContext, Maybe, Plugin, PromiseOrValue } from '@envelop/core';
2
+ import { DirectiveNode, FieldNode, GraphQLError, GraphQLObjectType } from 'graphql';
3
+ export declare class UnauthenticatedError extends GraphQLError {
4
+ }
5
+ export declare type ResolveUserFn<UserType, ContextType = DefaultContext> = (context: ContextType) => PromiseOrValue<Maybe<UserType>>;
6
+ export declare type ValidateUserFnParams<UserType> = {
7
+ /** The user object. */
8
+ user: UserType;
9
+ /** The field node from the operation that is being validated. */
10
+ fieldNode: FieldNode;
11
+ /** The object type which has the field that is being validated. */
12
+ objectType: GraphQLObjectType;
13
+ /** The directive node used for the authentication (If using an SDL flow). */
14
+ fieldAuthDirectiveNode: DirectiveNode | undefined;
15
+ /** The extensions used for authentication (If using an extension based flow). */
16
+ fieldAuthExtension: unknown | undefined;
17
+ };
18
+ export declare type ValidateUserFn<UserType> = (params: ValidateUserFnParams<UserType>) => void | UnauthenticatedError;
19
+ export declare const DIRECTIVE_SDL = "\n directive @auth on FIELD_DEFINITION\n";
20
+ export declare const SKIP_AUTH_DIRECTIVE_SDL = "\n directive @skipAuth on FIELD_DEFINITION\n";
21
+ export declare type GenericAuthPluginOptions<UserType extends {} = {}, ContextType = DefaultContext, CurrentUserKey extends string = 'currentUser'> = {
22
+ /**
23
+ * Here you can implement any custom sync/async code, and use the context built so far in Envelop and the HTTP request
24
+ * to find the current user.
25
+ * Common practice is to use a JWT token here, validate it, and use the payload as-is, or fetch the user from an external services.
26
+ * Make sure to either return `null` or the user object.
27
+ */
28
+ resolveUserFn: ResolveUserFn<UserType, ContextType>;
29
+ /**
30
+ * Overrides the default field name for injecting the user into the execution `context`.
31
+ * @default currentUser
32
+ */
33
+ contextFieldName?: CurrentUserKey;
34
+ } & ({
35
+ /**
36
+ * This mode offers complete protection for the entire API.
37
+ * It protects your entire GraphQL schema, by validating the user before executing the request.
38
+ * You can skip the validation using `@skipAuth` directive on a specific field.
39
+ */
40
+ mode: 'protect-all';
41
+ /**
42
+ * Overrides the default directive name or extension field for marking a field available for unauthorized users.
43
+ * @default skipAuth
44
+ */
45
+ directiveOrExtensionFieldName?: 'skipAuth' | string;
46
+ /**
47
+ * Customize how the user is validated. E.g. apply authorization role based validation.
48
+ * The validation is applied during the extended validation phase.
49
+ * @default `defaultProtectAllValidateFn`
50
+ */
51
+ validateUser?: ValidateUserFn<UserType>;
52
+ } | {
53
+ /**
54
+ * Just resolves the user and inject to authenticated user into the `context`.
55
+ * User validation needs to be implemented by you, in your resolvers.
56
+ */
57
+ mode: 'resolve-only';
58
+ } | {
59
+ /**
60
+ * resolves the user and inject to authenticated user into the `context`.
61
+ * And checks for `@auth` directives usages to run validation automatically.
62
+ */
63
+ mode: 'protect-granular';
64
+ /**
65
+ * Overrides the default directive name or extension field for marking a field available only for authorized users.
66
+ * @default auth
67
+ */
68
+ directiveOrExtensionFieldName?: 'auth' | string;
69
+ /**
70
+ * Customize how the user is validated. E.g. apply authorization role based validation.
71
+ * The validation is applied during the extended validation phase.
72
+ * @default `defaultProtectSingleValidateFn`
73
+ */
74
+ validateUser?: ValidateUserFn<UserType>;
75
+ });
76
+ export declare function defaultProtectAllValidateFn<UserType>(params: ValidateUserFnParams<UserType>): void | UnauthenticatedError;
77
+ export declare function defaultProtectSingleValidateFn<UserType>(params: ValidateUserFnParams<UserType>): void | UnauthenticatedError;
78
+ export declare const useGenericAuth: <UserType extends {} = {}, ContextType = DefaultContext, CurrentUserKey extends string = "currentUser">(options: GenericAuthPluginOptions<UserType, ContextType, CurrentUserKey>) => Plugin<{
79
+ validateUser: ValidateUserFn<UserType>;
80
+ } & Record<CurrentUserKey, UserType>>;