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

@envelop/generic-auth

Package Overview
Dependencies
Maintainers
1
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 0.0.1-alpha-629eb60.0 to 0.0.1-alpha-672fde7.0

utils.d.ts

61

index.cjs.js

@@ -5,2 +5,12 @@ 'use strict';

function hasDirective(info, name) {
const { parentType, fieldName, schema } = info;
const schemaType = schema.getType(parentType.name);
const field = schemaType.getFields()[fieldName];
const astNode = field.astNode;
const directives = astNode.directives;
const authDirective = directives.find(d => d.name.value === name);
return !!authDirective;
}
class UnauthenticatedError extends Error {

@@ -11,3 +21,53 @@ }

`;
function defaultValidateFn(user, contextType) {
if (!user) {
throw new UnauthenticatedError('Unauthenticated!');
}
}
const useGenericAuth = (options) => {
const fieldName = options.contextFieldName || 'currentUser';
const validateUser = options.validateUser || defaultValidateFn;
if (options.mode === 'authenticate-all') {
return {
async onContextBuilding({ context, extendContext }) {
const user = await options.extractUserFn(context);
await validateUser(user, context);
extendContext({
[fieldName]: user,
});
},
};
}
else if (options.mode === 'just-extract') {
return {
async onContextBuilding({ context, extendContext }) {
const user = await options.extractUserFn(context);
extendContext({
[fieldName]: user,
validateUser: () => validateUser(user, context),
});
},
};
}
else if (options.mode === 'auth-directive') {
return {
async onContextBuilding({ context, extendContext }) {
const user = await options.extractUserFn(context);
extendContext({
[fieldName]: user,
validateUser: () => validateUser(user, context),
});
},
onExecute() {
return {
async onResolverCalled({ context, info }) {
const shouldAuth = hasDirective(info, options.authDirectiveName || 'auth');
if (shouldAuth) {
await context.validateUser(context[fieldName], context);
}
},
};
},
};
}
return {};

@@ -18,3 +78,4 @@ };

exports.UnauthenticatedError = UnauthenticatedError;
exports.defaultValidateFn = defaultValidateFn;
exports.useGenericAuth = useGenericAuth;
//# sourceMappingURL=index.cjs.js.map

13

index.d.ts

@@ -1,2 +0,2 @@

import { Plugin } from '@envelop/types';
import { DefaultContext, Plugin } from '@envelop/types';
export declare class UnauthenticatedError extends Error {

@@ -24,3 +24,3 @@ }

*/
contextFieldName?: string;
contextFieldName?: 'currentUser' | string;
} & ({

@@ -45,7 +45,8 @@ /**

/**
* Extends the schema with the directive definition.
* @default false
* Overrides the default directive name
* @default auth
*/
addDirectiveToSchema?: boolean;
authDirectiveName?: 'auth' | string;
});
export declare const useGenericAuth: <UserType extends {}, ContextType = unknown>(options: GenericAuthPluginOptions<UserType, ContextType>) => Plugin;
export declare function defaultValidateFn<UserType, ContextType>(user: UserType, contextType: ContextType): void;
export declare const useGenericAuth: <UserType extends {}, ContextType extends DefaultContext = DefaultContext>(options: GenericAuthPluginOptions<UserType, ContextType>) => Plugin<ContextType>;

@@ -0,1 +1,11 @@

function hasDirective(info, name) {
const { parentType, fieldName, schema } = info;
const schemaType = schema.getType(parentType.name);
const field = schemaType.getFields()[fieldName];
const astNode = field.astNode;
const directives = astNode.directives;
const authDirective = directives.find(d => d.name.value === name);
return !!authDirective;
}
class UnauthenticatedError extends Error {

@@ -6,7 +16,57 @@ }

`;
function defaultValidateFn(user, contextType) {
if (!user) {
throw new UnauthenticatedError('Unauthenticated!');
}
}
const useGenericAuth = (options) => {
const fieldName = options.contextFieldName || 'currentUser';
const validateUser = options.validateUser || defaultValidateFn;
if (options.mode === 'authenticate-all') {
return {
async onContextBuilding({ context, extendContext }) {
const user = await options.extractUserFn(context);
await validateUser(user, context);
extendContext({
[fieldName]: user,
});
},
};
}
else if (options.mode === 'just-extract') {
return {
async onContextBuilding({ context, extendContext }) {
const user = await options.extractUserFn(context);
extendContext({
[fieldName]: user,
validateUser: () => validateUser(user, context),
});
},
};
}
else if (options.mode === 'auth-directive') {
return {
async onContextBuilding({ context, extendContext }) {
const user = await options.extractUserFn(context);
extendContext({
[fieldName]: user,
validateUser: () => validateUser(user, context),
});
},
onExecute() {
return {
async onResolverCalled({ context, info }) {
const shouldAuth = hasDirective(info, options.authDirectiveName || 'auth');
if (shouldAuth) {
await context.validateUser(context[fieldName], context);
}
},
};
},
};
}
return {};
};
export { DIRECTIVE_SDL, UnauthenticatedError, useGenericAuth };
export { DIRECTIVE_SDL, UnauthenticatedError, defaultValidateFn, useGenericAuth };
//# sourceMappingURL=index.esm.js.map
{
"name": "@envelop/generic-auth",
"version": "0.0.1-alpha-629eb60.0",
"version": "0.0.1-alpha-672fde7.0",
"sideEffects": false,

@@ -5,0 +5,0 @@ "peerDependencies": {

@@ -178,3 +178,3 @@ ## `@envelop/generic-auth`

> By defualt, we assume that you have the GraphQL directive defition as part of your GraphQL schema (`directive @auth on FIELD_DEFINITION`), if you wish this plugin to extend the schema with the directive, please specify `addDirectiveToSchema: true`.
> By defualt, we assume that you have the GraphQL directive defition as part of your GraphQL schema (`directive @auth on FIELD_DEFINITION`).

@@ -181,0 +181,0 @@ Then, in your GraphQL schema SDL, you can add `@auth` directive to your fields, and the `validateUser` will get called only while resolving that specific field:

Sorry, the diff of this file is not supported yet

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