diff --git a/backend/api/controllers/ape-keys.ts b/backend/api/controllers/ape-keys.ts index 87456a722..ab78340fb 100644 --- a/backend/api/controllers/ape-keys.ts +++ b/backend/api/controllers/ape-keys.ts @@ -46,6 +46,7 @@ class ApeKeysController { hash: saltyHash, createdOn: Date.now(), modifiedOn: Date.now(), + lastUsedOn: -1, }; const apeKeyId = await ApeKeysDAO.addApeKey(uid, apeKey); diff --git a/backend/dao/ape-keys.ts b/backend/dao/ape-keys.ts index 118a971da..20e948af2 100644 --- a/backend/dao/ape-keys.ts +++ b/backend/dao/ape-keys.ts @@ -72,6 +72,16 @@ class ApeKeysDAO { const apeKeys = _.omit(user.apeKeys, keyId); await UsersDAO.setApeKeys(uid, apeKeys); } + + static async updateLastUsedOn( + user: MonkeyTypes.User, + keyId: string + ): Promise { + checkIfKeyExists(user.apeKeys, keyId); + + user.apeKeys[keyId].lastUsedOn = Date.now(); + await UsersDAO.setApeKeys(user.uid, user.apeKeys); + } } export default ApeKeysDAO; diff --git a/backend/middlewares/auth.ts b/backend/middlewares/auth.ts index 7e749c080..2c4d3d304 100644 --- a/backend/middlewares/auth.ts +++ b/backend/middlewares/auth.ts @@ -1,6 +1,7 @@ import _ from "lodash"; import { compare } from "bcrypt"; import UsersDAO from "../dao/user"; +import ApeKeysDAO from "../dao/ape-keys"; import MonkeyError from "../utils/error"; import { verifyIdToken } from "../utils/auth"; import { base64UrlDecode } from "../utils/misc"; @@ -165,6 +166,8 @@ async function authenticateWithApeKey( throw new MonkeyError(400, "Invalid ApeKey"); } + await ApeKeysDAO.updateLastUsedOn(keyOwner, keyId); + return { uid, email: keyOwner.email, diff --git a/backend/types/types.d.ts b/backend/types/types.d.ts index c8c9c1401..269c2d976 100644 --- a/backend/types/types.d.ts +++ b/backend/types/types.d.ts @@ -70,6 +70,7 @@ declare namespace MonkeyTypes { hash: string; createdOn: number; modifiedOn: number; + lastUsedOn: number; enabled: boolean; }