Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@envelop/generic-auth

Package Overview
Dependencies
Maintainers
0
Versions
1325
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@envelop/generic-auth - npm Package Compare versions

Comparing version 8.0.0-alpha-20240810114324-912296f8 to 8.0.0-alpha-20240812113133-9e6b4a07

49

cjs/index.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useGenericAuth = exports.defaultProtectSingleValidateFn = exports.defaultProtectAllValidateFn = exports.createUnauthenticatedError = exports.SKIP_AUTH_DIRECTIVE_SDL = exports.DIRECTIVE_SDL = void 0;
const types_1 = require("util/types");
const graphql_1 = require("graphql");

@@ -27,3 +28,3 @@ const extended_validation_1 = require("@envelop/extended-validation");

function defaultProtectAllValidateFn(params) {
if (params.user == null && !params.fieldAuthDirectiveNode && !params.fieldAuthExtension) {
if (params.user == null && !params.fieldAuthArgs && !params.typeAuthArgs) {
return createUnauthenticatedError({

@@ -37,3 +38,3 @@ fieldNode: params.fieldNode,

function defaultProtectSingleValidateFn(params) {
if (params.user == null && (params.fieldAuthDirectiveNode || params.fieldAuthExtension)) {
if (params.user == null && (params.fieldAuthArgs || params.typeAuthArgs)) {
return createUnauthenticatedError({

@@ -55,8 +56,2 @@ fieldNode: params.fieldNode,

: defaultProtectSingleValidateFn);
const extractAuthMeta = (input) => {
return {
fieldAuthExtension: input.extensions?.[directiveOrExtensionFieldName],
fieldAuthDirectiveNode: input.astNode?.directives?.find(directive => directive.name.value === directiveOrExtensionFieldName),
};
};
const rejectUnauthenticated = 'rejectUnauthenticated' in options ? options.rejectUnauthenticated !== false : true;

@@ -87,3 +82,8 @@ return {

}
const { fieldAuthExtension, fieldAuthDirectiveNode } = extractAuthMeta(field);
const schema = context.getSchema();
// @ts-expect-error - Fix this
const typeDirectives = parentType && (0, utils_1.getDirectiveExtensions)(parentType, schema);
const typeAuthArgs = typeDirectives[directiveOrExtensionFieldName]?.[0];
const fieldDirectives = (0, utils_1.getDirectiveExtensions)(field, schema);
const fieldAuthArgs = fieldDirectives[directiveOrExtensionFieldName]?.[0];
const resolvePath = [];

@@ -101,6 +101,8 @@ let curr = args.document;

parentType,
fieldAuthDirectiveNode,
fieldAuthExtension,
typeAuthArgs,
typeDirectives,
executionArgs: args,
field,
fieldDirectives,
fieldAuthArgs,
path: resolvePath,

@@ -153,7 +155,22 @@ });

},
async onContextBuilding({ context, extendContext }) {
const user = await options.resolveUserFn(context);
extendContext({
[contextFieldName]: user,
});
onContextBuilding({ context, extendContext }) {
const user$ = options.resolveUserFn(context);
if ((0, types_1.isPromise)(user$)) {
return user$.then(user => {
// @ts-expect-error - Fix this
if (context[contextFieldName] !== user) {
// @ts-expect-error - Fix this
extendContext({
[contextFieldName]: user,
});
}
});
}
// @ts-expect-error - Fix this
if (context[contextFieldName] !== user$) {
// @ts-expect-error - Fix this
extendContext({
[contextFieldName]: user$,
});
}
},

@@ -160,0 +177,0 @@ };

@@ -0,4 +1,5 @@

import { isPromise } from 'util/types';
import { getNamedType, isInterfaceType, isIntrospectionType, isObjectType, isUnionType, } from 'graphql';
import { useExtendedValidation } from '@envelop/extended-validation';
import { createGraphQLError, shouldIncludeNode } from '@graphql-tools/utils';
import { createGraphQLError, getDirectiveExtensions, shouldIncludeNode, } from '@graphql-tools/utils';
export const DIRECTIVE_SDL = /* GraphQL */ `

@@ -23,3 +24,3 @@ directive @authenticated on FIELD_DEFINITION

export function defaultProtectAllValidateFn(params) {
if (params.user == null && !params.fieldAuthDirectiveNode && !params.fieldAuthExtension) {
if (params.user == null && !params.fieldAuthArgs && !params.typeAuthArgs) {
return createUnauthenticatedError({

@@ -32,3 +33,3 @@ fieldNode: params.fieldNode,

export function defaultProtectSingleValidateFn(params) {
if (params.user == null && (params.fieldAuthDirectiveNode || params.fieldAuthExtension)) {
if (params.user == null && (params.fieldAuthArgs || params.typeAuthArgs)) {
return createUnauthenticatedError({

@@ -49,8 +50,2 @@ fieldNode: params.fieldNode,

: defaultProtectSingleValidateFn);
const extractAuthMeta = (input) => {
return {
fieldAuthExtension: input.extensions?.[directiveOrExtensionFieldName],
fieldAuthDirectiveNode: input.astNode?.directives?.find(directive => directive.name.value === directiveOrExtensionFieldName),
};
};
const rejectUnauthenticated = 'rejectUnauthenticated' in options ? options.rejectUnauthenticated !== false : true;

@@ -81,3 +76,8 @@ return {

}
const { fieldAuthExtension, fieldAuthDirectiveNode } = extractAuthMeta(field);
const schema = context.getSchema();
// @ts-expect-error - Fix this
const typeDirectives = parentType && getDirectiveExtensions(parentType, schema);
const typeAuthArgs = typeDirectives[directiveOrExtensionFieldName]?.[0];
const fieldDirectives = getDirectiveExtensions(field, schema);
const fieldAuthArgs = fieldDirectives[directiveOrExtensionFieldName]?.[0];
const resolvePath = [];

@@ -95,6 +95,8 @@ let curr = args.document;

parentType,
fieldAuthDirectiveNode,
fieldAuthExtension,
typeAuthArgs,
typeDirectives,
executionArgs: args,
field,
fieldDirectives,
fieldAuthArgs,
path: resolvePath,

@@ -147,7 +149,22 @@ });

},
async onContextBuilding({ context, extendContext }) {
const user = await options.resolveUserFn(context);
extendContext({
[contextFieldName]: user,
});
onContextBuilding({ context, extendContext }) {
const user$ = options.resolveUserFn(context);
if (isPromise(user$)) {
return user$.then(user => {
// @ts-expect-error - Fix this
if (context[contextFieldName] !== user) {
// @ts-expect-error - Fix this
extendContext({
[contextFieldName]: user,
});
}
});
}
// @ts-expect-error - Fix this
if (context[contextFieldName] !== user$) {
// @ts-expect-error - Fix this
extendContext({
[contextFieldName]: user$,
});
}
},

@@ -154,0 +171,0 @@ };

{
"name": "@envelop/generic-auth",
"version": "8.0.0-alpha-20240810114324-912296f8",
"version": "8.0.0-alpha-20240812113133-9e6b4a07",
"sideEffects": false,

@@ -10,4 +10,4 @@ "peerDependencies": {

"dependencies": {
"@envelop/extended-validation": "4.1.0-alpha-20240810114324-912296f8",
"@graphql-tools/utils": "^10.0.6",
"@envelop/extended-validation": "4.1.0-alpha-20240812113133-9e6b4a07",
"@graphql-tools/utils": "^10.5.1",
"tslib": "^2.5.0"

@@ -14,0 +14,0 @@ },

@@ -152,3 +152,5 @@ ## `@envelop/generic-auth`

extensions: {
skipAuth: true
directives: {
skipAuth: true
}
}

@@ -281,3 +283,5 @@ }

extensions: {
authenticated: true
directives: {
authenticated: true
}
}

@@ -376,4 +380,6 @@ }

extensions: {
authenticated: {
role: 'USER'
directives: {
authenticated: {
role: 'USER'
}
}

@@ -416,7 +422,9 @@ }

extensions: {
authenticated: {
validate: ({ user, variables, context }) => {
// We can now have access to the operation and variables to decide if the user can execute the query
if (user.id !== variables.userId) {
return new Error(`Unauthorized`)
directives: {
authenticated: {
validate: ({ user, variables, context }) => {
// We can now have access to the operation and variables to decide if the user can execute the query
if (user.id !== variables.userId) {
return new Error(`Unauthorized`)
}
}

@@ -423,0 +431,0 @@ }

@@ -1,3 +0,4 @@

import { DirectiveNode, ExecutionArgs, FieldNode, GraphQLError, GraphQLField, GraphQLInterfaceType, GraphQLObjectType } from 'graphql';
import { ExecutionArgs, FieldNode, GraphQLError, GraphQLField, GraphQLInterfaceType, GraphQLObjectType } from 'graphql';
import { DefaultContext, Maybe, Plugin, PromiseOrValue } from '@envelop/core';
import { getDirectiveExtensions } from '@graphql-tools/utils';
export type ResolveUserFn<UserType, ContextType = DefaultContext> = (context: ContextType) => PromiseOrValue<Maybe<UserType>>;

@@ -11,8 +12,12 @@ export type ValidateUserFnParams<UserType> = {

parentType: GraphQLObjectType | GraphQLInterfaceType;
/** The auth directive arguments for the type */
typeAuthArgs?: Record<string, any>;
/** The directives for the type */
typeDirectives?: ReturnType<typeof getDirectiveExtensions>;
/** The object field */
field: GraphQLField<any, any>;
/** The directive node used for the authentication (If using an SDL flow). */
fieldAuthDirectiveNode: DirectiveNode | undefined;
/** The extensions used for authentication (If using an extension based flow). */
fieldAuthExtension: unknown | undefined;
/** The auth directive arguments for the field */
fieldAuthArgs?: Record<string, any>;
/** The directives for the field */
fieldDirectives?: ReturnType<typeof getDirectiveExtensions>;
/** The args passed to the execution function (including operation context and variables) **/

@@ -71,5 +76,5 @@ executionArgs: ExecutionArgs;

* Overrides the default directive name or extension field for marking a field available only for authorized users.
* @default auth
* @default authenticated
*/
directiveOrExtensionFieldName?: 'auth' | string;
directiveOrExtensionFieldName?: 'authenticated' | string;
/**

@@ -76,0 +81,0 @@ * Customize how the user is validated. E.g. apply authorization role based validation.

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc