Skip to content

Commit

Permalink
sso touch up
Browse files Browse the repository at this point in the history
  • Loading branch information
potts99 committed Dec 2, 2023
1 parent d025d18 commit fc1179a
Show file tree
Hide file tree
Showing 4 changed files with 385 additions and 140 deletions.
3 changes: 2 additions & 1 deletion apps/api/src/controllers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function authRoutes(fastify: FastifyInstance) {
}
);

// User login route
// User password login route
fastify.post(
"/api/v1/auth/login",
{
Expand Down Expand Up @@ -186,6 +186,7 @@ export function authRoutes(fastify: FastifyInstance) {
}
);

// SSO api callback route
fastify.get(
"/api/v1/auth/sso/login/callback",
async (request: FastifyRequest, reply: FastifyReply) => {
Expand Down
96 changes: 90 additions & 6 deletions apps/api/src/controllers/config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,94 @@
import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import { checkToken } from "../lib/jwt";
import { prisma } from "../prisma";

// Check Github Version
// Check Github Version
// Add outbound email provider
// Email Verification
// SSO Provider
// Portal Locale
// Feature Flags
// Feature Flags

import { FastifyInstance, FastifyReply, FastifyRequest } from "fastify";
import { checkToken } from "../lib/jwt";
import { prisma } from "../prisma";

export function configRoutes(fastify: FastifyInstance) {
// Check if SSO is enabled
fastify.get(
"/api/v1/config/sso/enabled",

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

if (token) {
const config = await prisma.config.findFirst();

//@ts-expect-error
const { sso_active } = config;

if (sso_active) {
const provider = await prisma.provider.findFirst({});

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

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

// Update SSO Provider Settings
fastify.post(
"/api/v1/config/sso/provider",

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

if (token) {
const {
name,
client_id,
client_secret,
redirect_uri,
tenantId,
issuer,
}: any = request.body;

const conf = await prisma.config.findFirst();

//update config to true
await prisma.config.update({
where: { id: conf!.id },
data: {
sso_active: true,
sso_provider: name,
},
});

// UPDATE PROVIDER
await prisma.provider.create({
data: {
name: name,
clientId: client_id,
clientSecret: client_secret,
active: true,
redirectUri: redirect_uri,
tenantId: tenantId,
issuer: issuer,
},
});

reply.send({
success: true,
message: "SSO Provider updated!",
});
}
}
);
}
2 changes: 2 additions & 0 deletions apps/api/src/routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FastifyInstance } from "fastify";
import { authRoutes } from "./controllers/auth";
import { clientRoutes } from "./controllers/clients";
import { configRoutes } from "./controllers/config";
import { dataRoutes } from "./controllers/data";
import { notebookRoutes } from "./controllers/notebook";
import { emailQueueRoutes } from "./controllers/queue";
Expand All @@ -19,4 +20,5 @@ export function registerRoutes(fastify: FastifyInstance) {
notebookRoutes(fastify);
clientRoutes(fastify);
webhookRoutes(fastify);
configRoutes(fastify);
}
Loading

0 comments on commit fc1179a

Please sign in to comment.