feat: add async suffix eslint rule (#485)

This commit is contained in:
Manuel
2024-05-18 12:25:33 +02:00
committed by GitHub
parent 5723295856
commit dcaff1d91c
60 changed files with 296 additions and 225 deletions

View File

@@ -14,7 +14,7 @@ import {
sessionTokenCookieName,
} from "./session";
export const getCurrentUserPermissions = async (
export const getCurrentUserPermissionsAsync = async (
db: Database,
userId: string,
) => {
@@ -47,7 +47,7 @@ export const createSessionCallback = (
...session.user,
id: user.id,
name: user.name,
permissions: await getCurrentUserPermissions(db, user.id),
permissions: await getCurrentUserPermissionsAsync(db, user.id),
},
};
};

View File

@@ -20,4 +20,7 @@ export * from "./security";
export const createHandlers = (isCredentialsRequest: boolean) =>
createConfiguration(isCredentialsRequest);
export { getSessionFromToken, sessionTokenCookieName } from "./session";
export {
getSessionFromTokenAsync as getSessionFromToken,
sessionTokenCookieName,
} from "./session";

View File

@@ -22,6 +22,7 @@ export const createCredentialsConfiguration = (db: Database) =>
type: "password",
},
},
// eslint-disable-next-line no-restricted-syntax
async authorize(credentials) {
const data = await validation.user.signIn.parseAsync(credentials);

View File

@@ -4,18 +4,18 @@ import { createId } from "@homarr/db";
import { users } from "@homarr/db/schema/sqlite";
import { createDb } from "@homarr/db/test";
import { createSalt, hashPassword } from "../../security";
import { createSaltAsync, hashPasswordAsync } from "../../security";
import { createCredentialsConfiguration } from "../credentials";
describe("Credentials authorization", () => {
it("should authorize user with correct credentials", async () => {
const db = createDb();
const userId = createId();
const salt = await createSalt();
const salt = await createSaltAsync();
await db.insert(users).values({
id: userId,
name: "test",
password: await hashPassword("test", salt),
password: await hashPasswordAsync("test", salt),
salt,
});
const result = await createCredentialsConfiguration(db).authorize({
@@ -38,11 +38,11 @@ describe("Credentials authorization", () => {
it(`should not authorize user with incorrect credentials (${password})`, async () => {
const db = createDb();
const userId = createId();
const salt = await createSalt();
const salt = await createSaltAsync();
await db.insert(users).values({
id: userId,
name: "test",
password: await hashPassword("test", salt),
password: await hashPasswordAsync("test", salt),
salt,
});
const result = await createCredentialsConfiguration(db).authorize({

View File

@@ -1,9 +1,9 @@
import bcrypt from "bcrypt";
export const createSalt = async () => {
export const createSaltAsync = async () => {
return bcrypt.genSalt(10);
};
export const hashPassword = async (password: string, salt: string) => {
export const hashPasswordAsync = async (password: string, salt: string) => {
return bcrypt.hash(password, salt);
};

View File

@@ -3,7 +3,7 @@ import type { Session } from "@auth/core/types";
import type { Database } from "@homarr/db";
import { getCurrentUserPermissions } from "./callbacks";
import { getCurrentUserPermissionsAsync } from "./callbacks";
export const sessionMaxAgeInSeconds = 30 * 24 * 60 * 60; // 30 days
export const sessionTokenCookieName = "next-auth.session-token";
@@ -16,7 +16,7 @@ export const generateSessionToken = () => {
return randomUUID();
};
export const getSessionFromToken = async (
export const getSessionFromTokenAsync = async (
db: Database,
token: string | undefined,
): Promise<Session | null> => {
@@ -48,7 +48,7 @@ export const getSessionFromToken = async (
return {
user: {
...session.user,
permissions: await getCurrentUserPermissions(db, session.user.id),
permissions: await getCurrentUserPermissionsAsync(db, session.user.id),
},
expires: session.expires.toISOString(),
};

View File

@@ -18,7 +18,7 @@ import * as definitions from "@homarr/definitions";
import {
createSessionCallback,
createSignInCallback,
getCurrentUserPermissions,
getCurrentUserPermissionsAsync,
} from "../callbacks";
describe("getCurrentUserPermissions", () => {
@@ -30,7 +30,7 @@ describe("getCurrentUserPermissions", () => {
});
const userId = "1";
const result = await getCurrentUserPermissions(db, userId);
const result = await getCurrentUserPermissionsAsync(db, userId);
expect(result).toEqual([]);
});
test("should return permissions for user", async () => {
@@ -56,7 +56,7 @@ describe("getCurrentUserPermissions", () => {
permission: "admin",
});
const result = await getCurrentUserPermissions(db, mockId);
const result = await getCurrentUserPermissionsAsync(db, mockId);
expect(result).toEqual(["board-create"]);
expect(getPermissionsWithChildrenMock).toHaveBeenCalledWith(["admin"]);
});

View File

@@ -1,16 +1,16 @@
import { describe, expect, it } from "vitest";
import { createSalt, hashPassword } from "../security";
import { createSaltAsync, hashPasswordAsync } from "../security";
describe("createSalt should return a salt", () => {
it("should return a salt", async () => {
const result = await createSalt();
const result = await createSaltAsync();
expect(result).toBeDefined();
expect(result.length).toBeGreaterThan(25);
});
it("should return a different salt each time", async () => {
const result1 = await createSalt();
const result2 = await createSalt();
const result1 = await createSaltAsync();
const result2 = await createSaltAsync();
expect(result1).not.toEqual(result2);
});
});
@@ -18,8 +18,8 @@ describe("createSalt should return a salt", () => {
describe("hashPassword should return a hash", () => {
it("should return a hash", async () => {
const password = "password";
const salt = await createSalt();
const result = await hashPassword(password, salt);
const salt = await createSaltAsync();
const result = await hashPasswordAsync(password, salt);
expect(result).toBeDefined();
expect(result.length).toBeGreaterThan(55);
expect(result).not.toEqual(password);
@@ -27,20 +27,20 @@ describe("hashPassword should return a hash", () => {
it("should return a different hash each time", async () => {
const password = "password";
const password2 = "another password";
const salt = await createSalt();
const salt = await createSaltAsync();
const result1 = await hashPassword(password, salt);
const result2 = await hashPassword(password2, salt);
const result1 = await hashPasswordAsync(password, salt);
const result2 = await hashPasswordAsync(password2, salt);
expect(result1).not.toEqual(result2);
});
it("should return a different hash for the same password with different salts", async () => {
const password = "password";
const salt1 = await createSalt();
const salt2 = await createSalt();
const salt1 = await createSaltAsync();
const salt2 = await createSaltAsync();
const result1 = await hashPassword(password, salt1);
const result2 = await hashPassword(password, salt2);
const result1 = await hashPasswordAsync(password, salt1);
const result2 = await hashPasswordAsync(password, salt2);
expect(result1).not.toEqual(result2);
});