diff --git a/server-node/src/auth/apikey/apikey-controller.ts b/server-node/src/auth/apikey/apikey-controller.ts index b54de0765..608853096 100644 --- a/server-node/src/auth/apikey/apikey-controller.ts +++ b/server-node/src/auth/apikey/apikey-controller.ts @@ -1,6 +1,6 @@ import { Error as SequelizeError } from "sequelize"; -import { APIKey } from "./apikey-model"; import { User } from "../user/user-model"; +import { APIKey } from "./apikey-model"; export function findOne(params: {apikey?: string}, cb: (err: Error | null, apikey?: APIKey | undefined, info?: Object | undefined) => void): undefined { const query: any = params; @@ -16,7 +16,8 @@ export function findOne(params: {apikey?: string}, cb: (err: Error | null, apike } APIKey.findOne({ - where: query + where: query, + include: APIKey.associations.User }).then(apikey => { if(apikey) cb(null, apikey); @@ -27,9 +28,11 @@ export function findOne(params: {apikey?: string}, cb: (err: Error | null, apike ); } -export async function createAPIKey(user: User | undefined): Promise { +export async function createAPIKey(user: User | undefined): Promise { + if(!user) throw new Error("User was undefined"); + const apikey = crypto.randomUUID(); // TODO: Is this secure enough? - const apikeyEntry = await APIKey.create({ apikey: apikey }) - await user?.addAPIKey(apikeyEntry); + const apikeyEntry = await user.createAPIKey({ apikey: apikey }); + return apikeyEntry; } \ No newline at end of file diff --git a/server-node/src/auth/apikey/apikey-model.ts b/server-node/src/auth/apikey/apikey-model.ts index e09d20fe4..209130cda 100644 --- a/server-node/src/auth/apikey/apikey-model.ts +++ b/server-node/src/auth/apikey/apikey-model.ts @@ -7,8 +7,10 @@ export class APIKey extends Model, InferCreationAttribut declare id: CreationOptional; declare apikey: string; - declare ownerId: ForeignKey; - declare owner?: NonAttribute; + declare UserId: ForeignKey; + + // `User` is an eagerly-loaded association. + declare User?: NonAttribute; declare createdAt: CreationOptional; declare updatedAt: CreationOptional; diff --git a/server-node/src/auth/passport-config.ts b/server-node/src/auth/passport-config.ts index 63f0f77f7..3db5e3fcd 100644 --- a/server-node/src/auth/passport-config.ts +++ b/server-node/src/auth/passport-config.ts @@ -37,7 +37,7 @@ export function initialize(passport: typeof import("passport")) { if (!apikey) { return done(null, false, info); } - return done(null, apikey.owner); + return done(null, apikey.User); }); } )); diff --git a/server-node/src/auth/user/user-model.ts b/server-node/src/auth/user/user-model.ts index 28ab0dc21..e7b486ba8 100644 --- a/server-node/src/auth/user/user-model.ts +++ b/server-node/src/auth/user/user-model.ts @@ -30,7 +30,7 @@ export class User extends Model, InferCreationAttributes; declare hasAccessRules: HasManyHasAssociationsMixin; declare countAccessRules: HasManyCountAssociationsMixin; - declare createAccessRule: HasManyCreateAssociationMixin; + declare createAccessRule: HasManyCreateAssociationMixin; declare getAPIKeys: HasManyGetAssociationsMixin; // Note the null assertions! declare addAPIKey: HasManyAddAssociationMixin; @@ -41,7 +41,11 @@ export class User extends Model, InferCreationAttributes; declare hasAPIKeys: HasManyHasAssociationsMixin; declare countAPIKeys: HasManyCountAssociationsMixin; - declare createAPIKey: HasManyCreateAssociationMixin; + declare createAPIKey: HasManyCreateAssociationMixin; + + // You can also pre-declare possible inclusions, these will only be populated if you + // actively include a relation. + declare apikeys?: NonAttribute; declare createdAt: CreationOptional; declare updatedAt: CreationOptional; @@ -63,8 +67,8 @@ export class AccessRule extends Model, InferCreation declare id: CreationOptional; declare grants: string; - declare ownerId: ForeignKey; - declare owner?: NonAttribute; + declare UserId: ForeignKey; + declare User?: NonAttribute; declare createdAt: CreationOptional; declare updatedAt: CreationOptional; diff --git a/server-node/src/data/sequelize-relations.ts b/server-node/src/data/sequelize-relations.ts index 086b65155..f1f08ea9f 100644 --- a/server-node/src/data/sequelize-relations.ts +++ b/server-node/src/data/sequelize-relations.ts @@ -44,8 +44,8 @@ Password.init( }, { sequelize }, ); -Password.hasOne(User); User.hasOne(Password); +Password.belongsTo(User); AccessRule.init( { @@ -60,8 +60,8 @@ AccessRule.init( }, { sequelize }, ); -AccessRule.hasOne(User); User.hasMany(AccessRule); +AccessRule.belongsTo(User); APIKey.init( { @@ -76,8 +76,8 @@ APIKey.init( }, { sequelize }, ); -APIKey.hasOne(User); User.hasMany(APIKey); +APIKey.belongsTo(User); export default sequelize;