Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: login access log source #141

Merged
merged 1 commit into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/imports/auth/login/index.ts
Original file line number Diff line number Diff line change
@@ -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[];
Expand All @@ -15,6 +15,7 @@ interface LoginParams {
geolocation?: { longitude: number; latitude: number } | string;
resolution?: { width: number; height: number } | string;
userAgent?: string;
source?: string;
}

interface accessLog {
Expand All @@ -29,6 +30,7 @@ interface accessLog {
geolocation?: [number, number];
resolution?: { width: number; height: number };
reason?: string;
source?: string;
_user?: [
{
_id: string;
Expand All @@ -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 = {
Expand All @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion src/server/routes/rest/auth/authApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -81,6 +82,7 @@ export const authApi: FastifyPluginCallback = (fastify, _, done) => {
geolocation,
resolution,
userAgent,
source,
});

if (get(loginResult, 'success', false) === true) {
Expand Down