Skip to content

Commit

Permalink
..
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Aug 12, 2024
1 parent e6a7b40 commit c400329
Showing 1 changed file with 33 additions and 25 deletions.
58 changes: 33 additions & 25 deletions packages/plugins/generic-auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export function defaultProtectAllValidateFn<UserType>(
path: params.path,
});
}
return validateScopes(params);
return validateScopesAndPolicies(params);
}

function areRolesValid(requiredRoles: string[][], userRoles: string[]) {
Expand All @@ -210,33 +210,45 @@ function areRolesValid(requiredRoles: string[][], userRoles: string[]) {
return false;
}

function validateScopes<UserType>(params: ValidateUserFnParams<UserType>): void | GraphQLError {
if (params.typeScopes && !areRolesValid(params.typeScopes, params.userScopes)) {
return createUnauthenticatedError({
fieldNode: params.fieldNode,
path: params.path,
});
}
if (params.fieldScopes && !areRolesValid(params.fieldScopes, params.userScopes)) {
function validateRoles<UserType>(
params: ValidateUserFnParams<UserType>,
requiredRoles: string[][],
userRoles: string[],
): void | GraphQLError {
if (!areRolesValid(requiredRoles, userRoles)) {
return createUnauthenticatedError({
fieldNode: params.fieldNode,
path: params.path,
});
}
}

function validatePolicies<UserType>(params: ValidateUserFnParams<UserType>): void | GraphQLError {
if (params.typePolicies && !areRolesValid(params.typePolicies, params.userPolicies)) {
return createUnauthenticatedError({
fieldNode: params.fieldNode,
path: params.path,
});
function validateScopesAndPolicies<UserType>(
params: ValidateUserFnParams<UserType>,
): void | GraphQLError {
if (params.typeScopes) {
const error = validateRoles(params, params.typeScopes, params.userScopes);
if (error) {
return error;
}
}
if (params.fieldPolicies && !areRolesValid(params.fieldPolicies, params.userPolicies)) {
return createUnauthenticatedError({
fieldNode: params.fieldNode,
path: params.path,
});
if (params.typePolicies?.length) {
const error = validateRoles(params, params.typePolicies, params.userPolicies);
if (error) {
return error;
}
}
if (params.fieldScopes?.length) {
const error = validateRoles(params, params.fieldScopes, params.userScopes);
if (error) {
return error;
}
}
if (params.fieldPolicies?.length) {
const error = validateRoles(params, params.fieldPolicies, params.userPolicies);
if (error) {
return error;
}
}
}
export function defaultProtectSingleValidateFn<UserType>(
Expand All @@ -248,11 +260,7 @@ export function defaultProtectSingleValidateFn<UserType>(
path: params.path,
});
}
const error = validateScopes(params);
if (error) {
return error;
}
return validatePolicies(params);
return validateScopesAndPolicies(params);
}

export function defaultExtractScopes<UserType>(user: UserType): string[] {
Expand Down

0 comments on commit c400329

Please sign in to comment.