From 5aa48d076ef412b4f88064eb602c2187de893cb5 Mon Sep 17 00:00:00 2001 From: "(skovati) Luke" Date: Thu, 14 Dec 2023 14:49:46 -0800 Subject: [PATCH] rename DefaultAuthAdapter to FakeAuthAdapter, run prettier --- src/main.ts | 12 ++++---- src/packages/auth/adapters/CAMAuthAdapter.ts | 29 +++++++++---------- ...faultAuthAdapter.ts => FakeAuthAdapter.ts} | 13 ++++----- src/packages/auth/adapters/NoAuthAdapter.ts | 16 +++++----- src/packages/auth/types.ts | 4 +-- 5 files changed, 35 insertions(+), 39 deletions(-) rename src/packages/auth/adapters/{DefaultAuthAdapter.ts => FakeAuthAdapter.ts} (59%) diff --git a/src/main.ts b/src/main.ts index f693463..06d9b79 100644 --- a/src/main.ts +++ b/src/main.ts @@ -11,9 +11,9 @@ import initHealthRoutes from './packages/health/health.js'; import initSwaggerRoutes from './packages/swagger/swagger.js'; import cookieParser from 'cookie-parser'; import { AuthAdapter } from './packages/auth/types.js'; -import { NoAuthAdapter } from "./packages/auth/adapters/NoAuthAdapter.js"; -import { CAMAuthAdapter } from "./packages/auth/adapters/CAMAuthAdapter.js"; -import { DefaultAuthAdapter } from "./packages/auth/adapters/DefaultAuthAdapter.js"; +import { NoAuthAdapter } from './packages/auth/adapters/NoAuthAdapter.js'; +import { CAMAuthAdapter } from './packages/auth/adapters/CAMAuthAdapter.js'; +import { FakeAuthAdapter } from './packages/auth/adapters/FakeAuthAdapter.js'; async function main(): Promise { const logger = getLogger('main'); @@ -27,12 +27,12 @@ async function main(): Promise { await DbMerlin.init(); - let authHandler: AuthAdapter = DefaultAuthAdapter; + let authHandler: AuthAdapter = FakeAuthAdapter; switch (AUTH_TYPE) { - case "none": + case 'none': authHandler = NoAuthAdapter; break; - case "cam": + case 'cam': authHandler = CAMAuthAdapter; break; } diff --git a/src/packages/auth/adapters/CAMAuthAdapter.ts b/src/packages/auth/adapters/CAMAuthAdapter.ts index 13a83ed..6b929a5 100644 --- a/src/packages/auth/adapters/CAMAuthAdapter.ts +++ b/src/packages/auth/adapters/CAMAuthAdapter.ts @@ -1,8 +1,9 @@ import { getEnv } from '../../../env.js'; -import { generateJwt, getUserRoles } from "../functions.js"; -import type { AuthAdapter, AuthResponse, ValidateResponse } from "../types.js"; +import { generateJwt, getUserRoles } from '../functions.js'; +import fetch from 'node-fetch'; +import type { AuthAdapter, AuthResponse, ValidateResponse } from '../types.js'; -import { Request } from "express"; +import { Request } from 'express'; type CAMValidateResponse = { validated?: boolean; @@ -23,7 +24,6 @@ type CAMLoginResponse = { }; export const CAMAuthAdapter: AuthAdapter = { - logout: async (req: Request): Promise => { const { AUTH_SSO_TOKEN_NAME, AUTH_URL } = getEnv(); @@ -33,7 +33,7 @@ export const CAMAuthAdapter: AuthAdapter = { const body = JSON.stringify({ ssoToken }); const url = `${AUTH_URL}/ssoToken?action=invalidate`; const response = await fetch(url, { body, method: 'DELETE' }); - const { invalidated = false } = await response.json() as CAMInvalidateResponse; + const { invalidated = false } = (await response.json()) as CAMInvalidateResponse; return invalidated; }, @@ -47,7 +47,7 @@ export const CAMAuthAdapter: AuthAdapter = { const body = JSON.stringify({ ssoToken }); const url = `${AUTH_URL}/ssoToken?action=validate`; const response = await fetch(url, { body, method: 'POST' }); - const json = await response.json() as CAMValidateResponse; + const json = (await response.json()) as CAMValidateResponse; const { validated = false, errorCode = false } = json; @@ -57,23 +57,22 @@ export const CAMAuthAdapter: AuthAdapter = { if (errorCode || !validated) { return { - message: "invalid token, redirecting to login UI", + message: 'invalid token, redirecting to login UI', redirectURL, - success: false + success: false, }; } const loginResp = await loginSSO(ssoToken); return { - message: "valid SSO token", - redirectURL: "", + message: 'valid SSO token', + redirectURL: '', success: validated, token: loginResp.token ?? undefined, userId: loginResp.message, - } + }; }, - }; async function loginSSO(ssoToken: any): Promise { @@ -83,13 +82,13 @@ async function loginSSO(ssoToken: any): Promise { const body = JSON.stringify({ ssoToken }); const url = `${AUTH_URL}/userProfile`; const response = await fetch(url, { body, method: 'POST' }); - const json = await response.json() as CAMLoginResponse; - const { userId = "", errorCode = false } = json; + const json = (await response.json()) as CAMLoginResponse; + const { userId = '', errorCode = false } = json; if (errorCode) { const { errorMessage } = json; return { - message: errorMessage ?? "error logging into CAM", + message: errorMessage ?? 'error logging into CAM', success: false, token: null, }; diff --git a/src/packages/auth/adapters/DefaultAuthAdapter.ts b/src/packages/auth/adapters/FakeAuthAdapter.ts similarity index 59% rename from src/packages/auth/adapters/DefaultAuthAdapter.ts rename to src/packages/auth/adapters/FakeAuthAdapter.ts index 33ceec2..9dc56c0 100644 --- a/src/packages/auth/adapters/DefaultAuthAdapter.ts +++ b/src/packages/auth/adapters/FakeAuthAdapter.ts @@ -1,15 +1,14 @@ import { getEnv } from '../../../env.js'; -import type { AuthAdapter, ValidateResponse } from "../types.js"; +import type { AuthAdapter, ValidateResponse } from '../types.js'; -export const DefaultAuthAdapter: AuthAdapter = { +export const FakeAuthAdapter: AuthAdapter = { logout: async (): Promise => true, validate: async (): Promise => { const { AUTH_UI_URL } = getEnv(); return { - message: "SSO token auth is disabled", + message: 'SSO token auth is disabled', redirectURL: AUTH_UI_URL, success: false, - } - } -} - + }; + }, +}; diff --git a/src/packages/auth/adapters/NoAuthAdapter.ts b/src/packages/auth/adapters/NoAuthAdapter.ts index f08e52f..84c6338 100644 --- a/src/packages/auth/adapters/NoAuthAdapter.ts +++ b/src/packages/auth/adapters/NoAuthAdapter.ts @@ -1,22 +1,20 @@ import { getEnv } from '../../../env.js'; -import { generateJwt, getUserRoles } from "../functions.js"; -import type { AuthAdapter, ValidateResponse } from "../types.js"; +import { generateJwt, getUserRoles } from '../functions.js'; +import type { AuthAdapter, ValidateResponse } from '../types.js'; export const NoAuthAdapter: AuthAdapter = { logout: async (): Promise => true, validate: async (): Promise => { - const { DEFAULT_ROLE_NO_AUTH, ALLOWED_ROLES_NO_AUTH } = getEnv(); - console.log("auth disabled, returning default roles") - const userId = "default_user"; - const { allowed_roles, default_role } = await getUserRoles(userId, DEFAULT_ROLE_NO_AUTH, ALLOWED_ROLES_NO_AUTH) + console.log('auth disabled, returning default roles'); + const userId = 'default_user'; + const { allowed_roles, default_role } = await getUserRoles(userId, DEFAULT_ROLE_NO_AUTH, ALLOWED_ROLES_NO_AUTH); return { message: userId, success: true, token: generateJwt(userId, default_role, allowed_roles) ?? undefined, }; - } -} - + }, +}; diff --git a/src/packages/auth/types.ts b/src/packages/auth/types.ts index 22fd85e..ed1928d 100644 --- a/src/packages/auth/types.ts +++ b/src/packages/auth/types.ts @@ -1,4 +1,4 @@ -import { Request } from "express"; +import { Request } from 'express'; export type JsonWebToken = string; @@ -49,4 +49,4 @@ export type ValidateResponse = { export interface AuthAdapter { validate(req: Request): Promise; logout(req: Request): Promise; -}; +}