mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-06 16:27:46 +00:00
misc: add sync for passport middleware
This commit is contained in:
@@ -300,6 +300,7 @@ import { injectIdentity } from "../plugins/auth/inject-identity";
|
|||||||
import { injectPermission } from "../plugins/auth/inject-permission";
|
import { injectPermission } from "../plugins/auth/inject-permission";
|
||||||
import { injectRateLimits } from "../plugins/inject-rate-limits";
|
import { injectRateLimits } from "../plugins/inject-rate-limits";
|
||||||
import { registerV1Routes } from "./v1";
|
import { registerV1Routes } from "./v1";
|
||||||
|
import { initializeOauthConfigSync } from "./v1/sso-router";
|
||||||
import { registerV2Routes } from "./v2";
|
import { registerV2Routes } from "./v2";
|
||||||
import { registerV3Routes } from "./v3";
|
import { registerV3Routes } from "./v3";
|
||||||
|
|
||||||
@@ -2045,11 +2046,17 @@ export const registerRoutes = async (
|
|||||||
cronJobs.push(adminIntegrationsSyncJob);
|
cronJobs.push(adminIntegrationsSyncJob);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const configSyncJob = await superAdminService.initializeEnvConfigSync();
|
const configSyncJob = await superAdminService.initializeEnvConfigSync();
|
||||||
if (configSyncJob) {
|
if (configSyncJob) {
|
||||||
cronJobs.push(configSyncJob);
|
cronJobs.push(configSyncJob);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const oauthConfigSyncJob = await initializeOauthConfigSync();
|
||||||
|
if (oauthConfigSyncJob) {
|
||||||
|
cronJobs.push(oauthConfigSyncJob);
|
||||||
|
}
|
||||||
|
|
||||||
server.decorate<FastifyZodProvider["store"]>("store", {
|
server.decorate<FastifyZodProvider["store"]>("store", {
|
||||||
user: userDAL,
|
user: userDAL,
|
||||||
kmipClient: kmipClientDAL
|
kmipClient: kmipClientDAL
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
import { Authenticator } from "@fastify/passport";
|
import { Authenticator } from "@fastify/passport";
|
||||||
import fastifySession from "@fastify/session";
|
import fastifySession from "@fastify/session";
|
||||||
import RedisStore from "connect-redis";
|
import RedisStore from "connect-redis";
|
||||||
|
import { CronJob } from "cron";
|
||||||
import { Strategy as GitLabStrategy } from "passport-gitlab2";
|
import { Strategy as GitLabStrategy } from "passport-gitlab2";
|
||||||
import { Strategy as GoogleStrategy } from "passport-google-oauth20";
|
import { Strategy as GoogleStrategy } from "passport-google-oauth20";
|
||||||
import { Strategy as OAuth2Strategy } from "passport-oauth2";
|
import { Strategy as OAuth2Strategy } from "passport-oauth2";
|
||||||
@@ -25,27 +26,14 @@ import { AuthMethod } from "@app/services/auth/auth-type";
|
|||||||
import { OrgAuthMethod } from "@app/services/org/org-types";
|
import { OrgAuthMethod } from "@app/services/org/org-types";
|
||||||
import { getServerCfg } from "@app/services/super-admin/super-admin-service";
|
import { getServerCfg } from "@app/services/super-admin/super-admin-service";
|
||||||
|
|
||||||
export const registerSsoRouter = async (server: FastifyZodProvider) => {
|
const passport = new Authenticator({ key: "sso", userProperty: "passportUser" });
|
||||||
|
|
||||||
|
let serverInstance: FastifyZodProvider | null = null;
|
||||||
|
|
||||||
|
export const registerOauthMiddlewares = (server: FastifyZodProvider) => {
|
||||||
|
serverInstance = server;
|
||||||
const appCfg = getConfig();
|
const appCfg = getConfig();
|
||||||
|
|
||||||
const passport = new Authenticator({ key: "sso", userProperty: "passportUser" });
|
|
||||||
const redisStore = new RedisStore({
|
|
||||||
client: server.redis,
|
|
||||||
prefix: "oauth-session:",
|
|
||||||
ttl: 600 // 10 minutes
|
|
||||||
});
|
|
||||||
|
|
||||||
await server.register(fastifySession, {
|
|
||||||
secret: appCfg.COOKIE_SECRET_SIGN_KEY,
|
|
||||||
store: redisStore,
|
|
||||||
cookie: {
|
|
||||||
secure: appCfg.HTTPS_ENABLED,
|
|
||||||
sameSite: "lax" // we want cookies to be sent to Infisical in redirects originating from IDP server
|
|
||||||
}
|
|
||||||
});
|
|
||||||
await server.register(passport.initialize());
|
|
||||||
await server.register(passport.secureSession());
|
|
||||||
|
|
||||||
// passport oauth strategy for Google
|
// passport oauth strategy for Google
|
||||||
const isGoogleOauthActive = Boolean(appCfg.CLIENT_ID_GOOGLE_LOGIN && appCfg.CLIENT_SECRET_GOOGLE_LOGIN);
|
const isGoogleOauthActive = Boolean(appCfg.CLIENT_ID_GOOGLE_LOGIN && appCfg.CLIENT_SECRET_GOOGLE_LOGIN);
|
||||||
if (isGoogleOauthActive) {
|
if (isGoogleOauthActive) {
|
||||||
@@ -176,6 +164,49 @@ export const registerSsoRouter = async (server: FastifyZodProvider) => {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const refreshOauthConfig = () => {
|
||||||
|
if (!serverInstance) {
|
||||||
|
logger.warn("Cannot refresh OAuth config: server instance not available");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info("Refreshing OAuth configuration...");
|
||||||
|
registerOauthMiddlewares(serverInstance);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const initializeOauthConfigSync = async () => {
|
||||||
|
logger.info("Setting up background sync process for oauth configuration");
|
||||||
|
|
||||||
|
// sync every 5 minutes
|
||||||
|
const job = new CronJob("*/5 * * * *", refreshOauthConfig);
|
||||||
|
job.start();
|
||||||
|
|
||||||
|
return job;
|
||||||
|
};
|
||||||
|
|
||||||
|
export const registerSsoRouter = async (server: FastifyZodProvider) => {
|
||||||
|
const appCfg = getConfig();
|
||||||
|
|
||||||
|
const redisStore = new RedisStore({
|
||||||
|
client: server.redis,
|
||||||
|
prefix: "oauth-session:",
|
||||||
|
ttl: 600 // 10 minutes
|
||||||
|
});
|
||||||
|
|
||||||
|
await server.register(fastifySession, {
|
||||||
|
secret: appCfg.COOKIE_SECRET_SIGN_KEY,
|
||||||
|
store: redisStore,
|
||||||
|
cookie: {
|
||||||
|
secure: appCfg.HTTPS_ENABLED,
|
||||||
|
sameSite: "lax" // we want cookies to be sent to Infisical in redirects originating from IDP server
|
||||||
|
}
|
||||||
|
});
|
||||||
|
await server.register(passport.initialize());
|
||||||
|
await server.register(passport.secureSession());
|
||||||
|
|
||||||
|
registerOauthMiddlewares(server);
|
||||||
|
|
||||||
server.route({
|
server.route({
|
||||||
url: "/redirect/google",
|
url: "/redirect/google",
|
||||||
|
|||||||
Reference in New Issue
Block a user