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

Resolve issue with api-keys and logger #658

Merged
merged 7 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
33 changes: 16 additions & 17 deletions backend/src/api/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ if (

const handlerToExpress =
(handler, message?: RecordMessage, action?: string) => async (req, res) => {
const logger = new Logger(req);
const { statusCode, body } = await handler(
{
pathParameters: req.params,
Expand All @@ -60,12 +59,12 @@ const handlerToExpress =
{}
);
// Add additional status codes that we may return for succesfull requests
if (statusCode === 200) {
if (message && action) {

if (message && action) {
const logger = new Logger(req);
if (statusCode === 200) {
logger.record(action, 'success', message, body);
}
} else {
if (message && action) {
} else {
logger.record(action, 'fail', message, body);
}
}
Expand Down Expand Up @@ -637,7 +636,7 @@ authenticatedRoute.post(
'/v2/organizations/:organizationId/users',
handlerToExpress(
organizations.addUserV2,
async (req, user) => {
async (req, token) => {
const orgId = req?.params?.organizationId;
const userId = req?.body?.userId;
const role = req?.body?.role;
Expand All @@ -646,15 +645,15 @@ authenticatedRoute.post(
const userRecord = await User.findOne({ where: { id: userId } });
return {
timestamp: new Date(),
userPerformedAssignment: user?.data?.id,
userPerformedAssignment: token?.id,
organization: orgRecord,
role: role,
user: userRecord
};
}
return {
timestamp: new Date(),
userId: user?.data?.id,
userId: token?.id,
updatePayload: req.body
};
},
Expand Down Expand Up @@ -689,8 +688,8 @@ authenticatedRoute.post(
'/users',
handlerToExpress(
users.invite,
async (req, user, responseBody) => {
const userId = user?.data?.id;
async (req, token, responseBody) => {
const userId = token?.id;
if (userId) {
const userRecord = await User.findOne({ where: { id: userId } });
return {
Expand All @@ -702,7 +701,7 @@ authenticatedRoute.post(
}
return {
timestamp: new Date(),
userId: user.data?.id,
userId: token?.id,
invitePayload: req.body,
createdUserRecord: responseBody
};
Expand All @@ -715,9 +714,9 @@ authenticatedRoute.delete(
'/users/:userId',
handlerToExpress(
users.del,
async (req, user, res) => {
async (req, token, res) => {
const userId = req?.params?.userId;
const userPerformedRemovalId = user?.data?.id;
const userPerformedRemovalId = token?.id;
if (userId && userPerformedRemovalId) {
const userPerformdRemovalRecord = await User.findOne({
where: { id: userPerformedRemovalId }
Expand All @@ -730,7 +729,7 @@ authenticatedRoute.delete(
}
return {
timestamp: new Date(),
userPerformedRemoval: user.data?.id,
userPerformedRemoval: token?.id,
userRemoved: req.params.userId
};
},
Expand Down Expand Up @@ -763,10 +762,10 @@ authenticatedRoute.put(
checkGlobalAdminOrRegionAdmin,
handlerToExpress(
users.registrationApproval,
async (req, user) => {
async (req, token) => {
return {
timestamp: new Date(),
userId: user?.data?.id,
userId: token?.id,
userToApprove: req.params.userId
};
},
Expand Down
73 changes: 34 additions & 39 deletions backend/src/tools/logger.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import { Request } from 'express';
import { decode } from 'jsonwebtoken';
import { User } from '../models';
import { attempt, unescape } from 'lodash';
import * as jwt from 'jsonwebtoken';
import { ApiKey, User } from '../models';
import { Log } from '../models/log';
import { getRepository, Repository } from 'typeorm';
import { UserToken } from 'src/api/auth';
import { createHash } from 'crypto';

type AccessTokenPayload = {
id: string;
email: string;
iat: string;
exp: string;
};

type LoggerUserState = {
Expand All @@ -25,7 +23,7 @@
export type RecordMessage =
| ((
request: Request,
user: LoggerUserState,
token: AccessTokenPayload | undefined,
responseBody?: object
) => Promise<RecordPayload>)
| RecordPayload;
Expand All @@ -48,15 +46,15 @@
responseBody?: object | string
) {
try {
if (!this.user.ready && this.user.attempts > 0) {
await this.fetchUser();
}

if (!this.logRep) {
const logRepository = getRepository(Log);
this.logRep = logRepository;
}

if (!this.token) {
await this.parseToken();
}

const parsedResponseBody =
typeof responseBody === 'string' &&
responseBody !== 'User registration approved.'
Expand All @@ -65,8 +63,9 @@

const payload =
typeof messageOrCB === 'function'
? await messageOrCB(this.request, this.user, parsedResponseBody)
? await messageOrCB(this.request, this.token, parsedResponseBody)
: messageOrCB;

const logRecord = await this.logRep.create({
payload: payload as object,
createdAt: payload?.timestamp,
Expand All @@ -80,40 +79,36 @@
}
}

async fetchUser() {
if (this.token) {
const user = await User.findOne({ id: this.token.id });
if (user) {
this.user = {
data: user,
ready: true,
attempts: 0
};
async parseToken() {
const atoapk = this.request.headers.authorization;
// Test if API key, e.g. a 32 digit hex string
if (atoapk && /^[A-Fa-f0-9]{32}$/.test(atoapk ?? '')) {
const apiKey = await ApiKey.findOne(
{
hashedKey: createHash('sha256').update(atoapk).digest('hex')
},
{ relations: ['user'] }
);
if (!apiKey) throw 'Invalid API key';
this.token = { id: apiKey.user.id };
apiKey.lastUsed = new Date();
apiKey.save();
} else {
if (atoapk) {
Fixed Show fixed Hide fixed
const parsedUserFromJwt = jwt.verify(
atoapk,
process.env.JWT_SECRET!
) as UserToken;
this.token = { id: parsedUserFromJwt.id };
} else {
return 'Missing token/api key';
}
this.user = {
data: undefined,
ready: false,
attempts: this.user.attempts + 1
};
}
}

// Constructor takes a request and sets it to a class variable
constructor(req: Request) {
this.request = req;
this.logId = '123123123123';
const authToken = req.headers.authorization;
if (authToken) {
const tokenPayload = decode(
authToken as string
) as unknown as AccessTokenPayload;
this.token = tokenPayload;
User.findOne({ id: this.token.id }).then((user) => {
if (user) {
this.user = { data: user, ready: true, attempts: 0 };
}
});
}
}
}

Expand Down
Loading