Skip to content

Commit

Permalink
Misc refinements from sonar (#646)
Browse files Browse the repository at this point in the history
This contains a number of fixes and refinements recommended by sonar. Some are meaningful, others less so.

Additional recommendations remain, but this covers all current recommendations labeled a "reliability" and many more.
  • Loading branch information
mike-marcacci authored Apr 3, 2024
1 parent abb3821 commit 5e7b739
Show file tree
Hide file tree
Showing 36 changed files with 244 additions and 289 deletions.
15 changes: 10 additions & 5 deletions packages/authx/src/graphql/mutation/createAuthorizations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,16 @@ export const createAuthorizations: GraphQLFieldConfig<
});

if (
!administrationRoleBefore.isAccessibleBy(realm, a, executor, {
basic: "w",
scopes: "w",
users: "",
})
!(await administrationRoleBefore.isAccessibleBy(
realm,
a,
executor,
{
basic: "w",
scopes: "w",
users: "",
},
))
) {
throw new ForbiddenError(
`You do not have permission to modify the scopes of role ${roleId}.`,
Expand Down
15 changes: 10 additions & 5 deletions packages/authx/src/graphql/mutation/createClients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,16 @@ export const createClients: GraphQLFieldConfig<
});

if (
!administrationRoleBefore.isAccessibleBy(realm, a, executor, {
basic: "w",
scopes: "w",
users: "",
})
!(await administrationRoleBefore.isAccessibleBy(
realm,
a,
executor,
{
basic: "w",
scopes: "w",
users: "",
},
))
) {
throw new ForbiddenError(
`You do not have permission to modify the scopes of role ${roleId}.`,
Expand Down
15 changes: 10 additions & 5 deletions packages/authx/src/graphql/mutation/createGrants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,16 @@ export const createGrants: GraphQLFieldConfig<
});

if (
!administrationRoleBefore.isAccessibleBy(realm, a, executor, {
basic: "w",
scopes: "w",
users: "",
})
!(await administrationRoleBefore.isAccessibleBy(
realm,
a,
executor,
{
basic: "w",
scopes: "w",
users: "",
},
))
) {
throw new ForbiddenError(
`You do not have permission to modify the scopes of role ${roleId}.`,
Expand Down
15 changes: 10 additions & 5 deletions packages/authx/src/graphql/mutation/createRoles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,16 @@ export const createRoles: GraphQLFieldConfig<
});

if (
!administrationRoleBefore.isAccessibleBy(realm, a, executor, {
basic: "w",
scopes: "w",
users: "",
})
!(await administrationRoleBefore.isAccessibleBy(
realm,
a,
executor,
{
basic: "w",
scopes: "w",
users: "",
},
))
) {
throw new ForbiddenError(
`You do not have permission to modify the scopes of role ${roleId}.`,
Expand Down
15 changes: 10 additions & 5 deletions packages/authx/src/graphql/mutation/createUsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,16 @@ export const createUsers: GraphQLFieldConfig<
});

if (
!administrationRoleBefore.isAccessibleBy(realm, a, executor, {
basic: "w",
scopes: "w",
users: "",
})
!(await administrationRoleBefore.isAccessibleBy(
realm,
a,
executor,
{
basic: "w",
scopes: "w",
users: "",
},
))
) {
throw new ForbiddenError(
`You do not have permission to modify the scopes of role ${roleId}.`,
Expand Down
2 changes: 1 addition & 1 deletion packages/authx/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export class AuthX extends Router<any, { [x]: Context }> {

execute({
schema: config.processSchema
? (config.processSchema(createSchema(strategies)) as any)
? config.processSchema(createSchema(strategies))
: (createSchema(strategies) as any),
override: (ctx: any) => {
const contextValue: Context = ctx[x];
Expand Down
32 changes: 4 additions & 28 deletions packages/authx/src/model/Authorization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,7 @@ export class Authorization implements AuthorizationData {
}

public user(tx: Pool | ClientBase | DataLoaderExecutor): Promise<User> {
return (
// Some silliness to help typescript...
tx instanceof DataLoaderExecutor
? User.read(tx, this.userId)
: User.read(tx, this.userId)
);
return User.read(tx, this.userId);
}

public async grant(
Expand All @@ -134,12 +129,7 @@ export class Authorization implements AuthorizationData {
return null;
}

return (
// Some silliness to help typescript...
tx instanceof DataLoaderExecutor
? Grant.read(tx, this.grantId)
: Grant.read(tx, this.grantId)
);
return Grant.read(tx, this.grantId);
}

private async _access(
Expand Down Expand Up @@ -356,28 +346,14 @@ export class Authorization implements AuthorizationData {
);
}

// Read using an executor.
public static read(
tx: DataLoaderExecutor,
id: string,
options?: { forUpdate?: false },
): Promise<Authorization>;

public static read(
tx: DataLoaderExecutor,
id: readonly string[],
options?: { forUpdate?: false },
): Promise<Authorization[]>;

// Read using a connection.
public static read(
tx: Pool | ClientBase,
tx: Pool | ClientBase | DataLoaderExecutor,
id: string,
options?: { forUpdate?: boolean },
): Promise<Authorization>;

public static read(
tx: Pool | ClientBase,
tx: Pool | ClientBase | DataLoaderExecutor,
id: readonly string[],
options?: { forUpdate?: boolean },
): Promise<Authorization[]>;
Expand Down
18 changes: 2 additions & 16 deletions packages/authx/src/model/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,28 +267,14 @@ export class Client implements ClientData {
);
}

// Read using an executor.
public static read(
tx: DataLoaderExecutor,
id: string,
options?: { forUpdate?: false },
): Promise<Client>;

public static read(
tx: DataLoaderExecutor,
id: readonly string[],
options?: { forUpdate?: false },
): Promise<Client[]>;

// Read using a connection.
public static read(
tx: Pool | ClientBase,
tx: Pool | ClientBase | DataLoaderExecutor,
id: string,
options?: { forUpdate?: boolean },
): Promise<Client>;

public static read(
tx: Pool | ClientBase,
tx: Pool | ClientBase | DataLoaderExecutor,
id: readonly string[],
options?: { forUpdate?: boolean },
): Promise<Client[]>;
Expand Down
7 changes: 1 addition & 6 deletions packages/authx/src/model/Credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,7 @@ export abstract class Credential<C> implements CredentialData<C> {
): Promise<Authority<any>>;

public user(tx: Pool | ClientBase | DataLoaderExecutor): Promise<User> {
return (
// Some silliness to help typescript...
tx instanceof DataLoaderExecutor
? User.read(tx, this.userId)
: User.read(tx, this.userId)
);
return User.read(tx, this.userId);
}

public async records(tx: ClientBase): Promise<CredentialRecord[]> {
Expand Down
37 changes: 5 additions & 32 deletions packages/authx/src/model/Grant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,21 +116,11 @@ export class Grant implements GrantData {
}

public client(tx: Pool | ClientBase | DataLoaderExecutor): Promise<Client> {
return (
// Some silliness to help typescript...
tx instanceof DataLoaderExecutor
? Client.read(tx, this.clientId)
: Client.read(tx, this.clientId)
);
return Client.read(tx, this.clientId);
}

public user(tx: Pool | ClientBase | DataLoaderExecutor): Promise<User> {
return (
// Some silliness to help typescript...
tx instanceof DataLoaderExecutor
? User.read(tx, this.userId)
: User.read(tx, this.userId)
);
return User.read(tx, this.userId);
}

public async authorizations(
Expand All @@ -151,10 +141,7 @@ export class Grant implements GrantData {
)
).rows.map(({ id }) => id);

// Some silliness to help typescript...
return tx instanceof DataLoaderExecutor
? Authorization.read(tx, ids)
: Authorization.read(tx, ids);
return Authorization.read(tx, ids);
}

public async access(
Expand Down Expand Up @@ -296,28 +283,14 @@ export class Grant implements GrantData {
);
}

// Read using an executor.
public static read(
tx: DataLoaderExecutor,
id: string,
options?: { forUpdate?: false },
): Promise<Grant>;

public static read(
tx: DataLoaderExecutor,
id: readonly string[],
options?: { forUpdate?: false },
): Promise<Grant[]>;

// Read using a connection.
public static read(
tx: Pool | ClientBase,
tx: Pool | ClientBase | DataLoaderExecutor,
id: string,
options?: { forUpdate?: boolean },
): Promise<Grant>;

public static read(
tx: Pool | ClientBase,
tx: Pool | ClientBase | DataLoaderExecutor,
id: readonly string[],
options?: { forUpdate?: boolean },
): Promise<Grant[]>;
Expand Down
25 changes: 3 additions & 22 deletions packages/authx/src/model/Role.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,7 @@ export class Role implements RoleData {
}

public users(tx: Pool | ClientBase | DataLoaderExecutor): Promise<User[]> {
return (
// Some silliness to help typescript...
tx instanceof DataLoaderExecutor
? User.read(tx, [...this.userIds].sort())
: User.read(tx, [...this.userIds].sort())
);
return User.read(tx, [...this.userIds].sort());
}

public access(values: {
Expand Down Expand Up @@ -158,28 +153,14 @@ export class Role implements RoleData {
);
}

// Read using an executor.
public static read(
tx: DataLoaderExecutor,
id: string,
options?: { forUpdate?: false },
): Promise<Role>;

public static read(
tx: DataLoaderExecutor,
id: readonly string[],
options?: { forUpdate?: false },
): Promise<Role[]>;

// Read using a connection.
public static read(
tx: Pool | ClientBase,
tx: Pool | ClientBase | DataLoaderExecutor,
id: string,
options?: { forUpdate?: boolean },
): Promise<Role>;

public static read(
tx: Pool | ClientBase,
tx: Pool | ClientBase | DataLoaderExecutor,
id: readonly string[],
options?: { forUpdate?: boolean },
): Promise<Role[]>;
Expand Down
Loading

0 comments on commit 5e7b739

Please sign in to comment.