From e6bdb1270b9cc416a30b0cac1c717dec4c024d87 Mon Sep 17 00:00:00 2001 From: Leonardo Viva Date: Fri, 7 Jun 2024 16:02:06 -0300 Subject: [PATCH] feat: login access log source --- src/imports/auth/login/index.ts | 13 ++++++++----- src/server/routes/rest/auth/authApi.ts | 4 +++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/imports/auth/login/index.ts b/src/imports/auth/login/index.ts index d1681e19..9218fef0 100644 --- a/src/imports/auth/login/index.ts +++ b/src/imports/auth/login/index.ts @@ -1,11 +1,11 @@ -import { hash as bcryptHash, compare as bcryptCompare } from 'bcryptjs'; -import { UAParser } from 'ua-parser-js'; +import { compare as bcryptCompare, hash as bcryptHash } from 'bcryptjs'; import get from 'lodash/get'; +import { UAParser } from 'ua-parser-js'; -import { MetaObject } from '@imports/model/MetaObject'; -import { BCRYPT_SALT_ROUNDS, DEFAULT_LOGIN_EXPIRATION } from '../../consts'; import { generateStampedLoginToken } from '@imports/auth/login/token'; +import { MetaObject } from '@imports/model/MetaObject'; import { ObjectId } from 'mongodb'; +import { BCRYPT_SALT_ROUNDS, DEFAULT_LOGIN_EXPIRATION } from '../../consts'; interface LoginParams { ip?: string | string[]; @@ -15,6 +15,7 @@ interface LoginParams { geolocation?: { longitude: number; latitude: number } | string; resolution?: { width: number; height: number } | string; userAgent?: string; + source?: string; } interface accessLog { @@ -29,6 +30,7 @@ interface accessLog { geolocation?: [number, number]; resolution?: { width: number; height: number }; reason?: string; + source?: string; _user?: [ { _id: string; @@ -38,7 +40,7 @@ interface accessLog { ]; } -export async function login({ ip, user, password, password_SHA256, geolocation, resolution, userAgent }: LoginParams) { +export async function login({ ip, user, password, password_SHA256, geolocation, resolution, userAgent, source }: LoginParams) { const ua = new UAParser(userAgent ?? 'API Call').getResult(); const accessLog: accessLog = { @@ -50,6 +52,7 @@ export async function login({ ip, user, password, password_SHA256, geolocation, browserVersion: ua.browser.version, os: ua.os.name, platform: ua.device.type, + source, }; if (resolution != null) { diff --git a/src/server/routes/rest/auth/authApi.ts b/src/server/routes/rest/auth/authApi.ts index 61dae989..ec8e41ca 100644 --- a/src/server/routes/rest/auth/authApi.ts +++ b/src/server/routes/rest/auth/authApi.ts @@ -54,10 +54,11 @@ export const authApi: FastifyPluginCallback = (fastify, _, done) => { geolocation?: { longitude: number; latitude: number }; resolution?: { width: number; height: number }; password_SHA256?: string; + source?: string; }; }>('/rest/auth/login', async function (req, reply) { // Map body parameters - const { user, password, ns, geolocation, resolution, password_SHA256 } = req.body; + const { user, password, ns, geolocation, resolution, password_SHA256, source } = req.body; const namespace = await MetaObject.MetaObject.findOne({ _id: 'MetaObject.Namespace' } as unknown as any); @@ -81,6 +82,7 @@ export const authApi: FastifyPluginCallback = (fastify, _, done) => { geolocation, resolution, userAgent, + source, }); if (get(loginResult, 'success', false) === true) {