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

0.5.4.3 🚀 #398

Merged
merged 13 commits into from
Nov 9, 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
2 changes: 2 additions & 0 deletions apps/api/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,5 @@ dist
.tern-port

/uploads

logs.log
1 change: 1 addition & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"mailparser": "^3.6.5",
"nodemailer": "^6.9.7",
"openid-client": "^5.7.0",
"pino": "^9.5.0",
"posthog-node": "^3.1.3",
"prisma": "5.6.0",
"samlify": "^2.8.11",
Expand Down
24 changes: 20 additions & 4 deletions apps/api/src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ function generateRandomPassword(length: number): string {
return password;
}

async function tracking(event: string, properties: any) {
const client = track();

client.capture({
event: event,
properties: properties,
distinctId: "uuid",
});
}

export function authRoutes(fastify: FastifyInstance) {
// Register a new user
fastify.post(
Expand Down Expand Up @@ -175,7 +185,7 @@ export function authRoutes(fastify: FastifyInstance) {
const hog = track();

hog.capture({
event: "user_registered",
event: "user_registered_external",
distinctId: user.id,
});

Expand Down Expand Up @@ -339,6 +349,8 @@ export function authRoutes(fastify: FastifyInstance) {
},
});

await tracking("user_logged_in_password", {});

const data = {
id: user!.id,
email: user!.email,
Expand Down Expand Up @@ -523,13 +535,11 @@ export function authRoutes(fastify: FastifyInstance) {
// Retrieve user information
const userInfo = await oidcClient.userinfo(tokens.access_token);

console.log(userInfo);

let user = await prisma.user.findUnique({
where: { email: userInfo.email },
});

console.log(user);
await tracking("user_logged_in_oidc", {});

if (!user) {
// Create a new basic user
Expand Down Expand Up @@ -665,6 +675,8 @@ export function authRoutes(fastify: FastifyInstance) {
},
});

await tracking("user_logged_in_oauth", {});

// Send Response
reply.send({
token: signed_token,
Expand Down Expand Up @@ -757,6 +769,8 @@ export function authRoutes(fastify: FastifyInstance) {
external_user: user!.external_user,
};

await tracking("user_profile", {});

reply.send({
user: data,
});
Expand Down Expand Up @@ -1008,6 +1022,8 @@ export function authRoutes(fastify: FastifyInstance) {
},
});

await tracking("user_first_login", {});

reply.send({ success: true });
}
}
Expand Down
28 changes: 18 additions & 10 deletions apps/api/src/controllers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,18 @@ const nodemailer = require("nodemailer");

import { checkToken } from "../lib/jwt";
import { prisma } from "../prisma";
import { emit } from "process";
import { createTransportProvider } from "../lib/nodemailer/transport";
import { track } from "../lib/hog";

async function tracking(event: string, properties: any) {
const client = track();

client.capture({
event: event,
properties: properties,
distinctId: "uuid",
});
}

export function configRoutes(fastify: FastifyInstance) {
// Check auth method
Expand Down Expand Up @@ -87,6 +97,8 @@ export function configRoutes(fastify: FastifyInstance) {
});
}

await tracking("oidc_provider_updated", {});

reply.send({
success: true,
message: "OIDC config Provider updated!",
Expand Down Expand Up @@ -152,6 +164,8 @@ export function configRoutes(fastify: FastifyInstance) {
});
}

await tracking("oauth_provider_updated", {});

reply.send({
success: true,
message: "SSO Provider updated!",
Expand Down Expand Up @@ -183,6 +197,8 @@ export function configRoutes(fastify: FastifyInstance) {
// Delete the OAuth provider
await prisma.oAuthProvider.deleteMany({});

await tracking("sso_provider_deleted", {});

reply.send({
success: true,
message: "SSO Provider deleted!",
Expand Down Expand Up @@ -211,14 +227,7 @@ export function configRoutes(fastify: FastifyInstance) {
},
});

if (config === null) {
reply.send({
success: true,
active: false,
});
}

if (config?.active) {
if (config && config?.active) {
const provider = await createTransportProvider();

await new Promise((resolve, reject) => {
Expand Down Expand Up @@ -247,7 +256,6 @@ export function configRoutes(fastify: FastifyInstance) {
reply.send({
success: true,
active: false,
email: config,
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/controllers/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export function dataRoutes(fastify: FastifyInstance) {

if (token) {
const result = await prisma.ticket.count({
where: { userId: null, hidden: false },
where: { userId: null, hidden: false, isComplete: false },
});

reply.send({ count: result });
Expand Down
15 changes: 15 additions & 0 deletions apps/api/src/controllers/notebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import { checkToken } from "../lib/jwt";
import { checkSession } from "../lib/session";
import { prisma } from "../prisma";
import { track } from "../lib/hog";

async function tracking(event: string, properties: any) {
const client = track();

client.capture({
event: event,
properties: properties,
distinctId: "uuid",
});

client.shutdownAsync();
}

export function notebookRoutes(fastify: FastifyInstance) {
// Create a new entry
Expand All @@ -25,6 +38,8 @@ export function notebookRoutes(fastify: FastifyInstance) {
},
});

await tracking("note_created", {});

const { id } = data;

reply.status(200).send({ success: true, id });
Expand Down
72 changes: 53 additions & 19 deletions apps/api/src/controllers/queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,66 +3,100 @@
import { checkToken } from "../lib/jwt";
import { prisma } from "../prisma";
import { OAuth2Client } from "google-auth-library";
import { track } from "../lib/hog";

async function tracking(event: string, properties: any) {
const client = track();

client.capture({
event: event,
properties: properties,
distinctId: "uuid",
});

client.shutdownAsync();
}

export function emailQueueRoutes(fastify: FastifyInstance) {
// Create a new email queue
fastify.post(
"/api/v1/email-queue/create",

async (request: FastifyRequest, reply: FastifyReply) => {
const bearer = request.headers.authorization!.split(" ")[1];
const token = checkToken(bearer);

if (token) {
const {
name,
username,
password,
hostname,
tls,
serviceType,
clientId,
clientSecret,
redirectUri,
}: any = request.body;

const mailbox = await prisma.emailQueue.create({
data: {
name: name,
username,
password,
hostname,
tls,
serviceType,
clientId,
clientSecret,
redirectUri,
},
});

// generate redirect uri
if (serviceType === "gmail") {
const google = new OAuth2Client(clientId, clientSecret, redirectUri);

const authorizeUrl = google.generateAuthUrl({
access_type: "offline",
scope: "https://mail.google.com",
prompt: "consent",
state: mailbox.id,
});

reply.send({
success: true,
message: "Gmail imap provider created!",
authorizeUrl: authorizeUrl,
});
switch (serviceType) {
case "gmail":
const google = new OAuth2Client(
clientId,
clientSecret,
redirectUri
);

const authorizeUrl = google.generateAuthUrl({
access_type: "offline",
scope: "https://mail.google.com",
prompt: "consent",
state: mailbox.id,
});

tracking("gmail_provider_created", {
provider: "gmail",
});

reply.send({
success: true,
message: "Gmail imap provider created!",
authorizeUrl: authorizeUrl,
});
break;
case "other":
tracking("imap_provider_created", {
provider: "other",
});

reply.send({
success: true,
message: "Other service type created!",
});
break;
default:
reply.send({
success: false,
message: "Unsupported service type",
});
}

reply.send({
success: true,
});
}
}

Check failure

Code scanning / CodeQL

Missing rate limiting High

This route handler performs
authorization
, but is not rate-limited.
);

// Google oauth callback
Expand Down
29 changes: 19 additions & 10 deletions apps/api/src/controllers/ticket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,20 +443,29 @@ export function ticketRoutes(fastify: FastifyInstance) {
const { user, id }: any = request.body;

if (token) {
const assigned = await prisma.user.update({
where: { id: user },
data: {
tickets: {
connect: {
id: id,
if (user) {
const assigned = await prisma.user.update({
where: { id: user },
data: {
tickets: {
connect: {
id: id,
},
},
},
},
});
});

const { email } = assigned;
const { email } = assigned;

await sendAssignedEmail(email);
await sendAssignedEmail(email);
} else {
await prisma.ticket.update({
where: { id: id },
data: {
userId: null,
},
});
}

reply.send({
success: true,
Expand Down
Loading
Loading