Skip to content

Commit

Permalink
rename DefaultAuthAdapter to FakeAuthAdapter, run prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
skovati committed Dec 14, 2023
1 parent 5a4e3bb commit 5aa48d0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 39 deletions.
12 changes: 6 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
const logger = getLogger('main');
Expand All @@ -27,12 +27,12 @@ async function main(): Promise<void> {

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;
}
Expand Down
29 changes: 14 additions & 15 deletions src/packages/auth/adapters/CAMAuthAdapter.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -23,7 +24,6 @@ type CAMLoginResponse = {
};

export const CAMAuthAdapter: AuthAdapter = {

logout: async (req: Request): Promise<boolean> => {
const { AUTH_SSO_TOKEN_NAME, AUTH_URL } = getEnv();

Expand All @@ -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;
},
Expand All @@ -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;

Expand All @@ -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<AuthResponse> {
Expand All @@ -83,13 +82,13 @@ async function loginSSO(ssoToken: any): Promise<AuthResponse> {
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,
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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<boolean> => true,
validate: async (): Promise<ValidateResponse> => {
const { AUTH_UI_URL } = getEnv();
return {
message: "SSO token auth is disabled",
message: 'SSO token auth is disabled',
redirectURL: AUTH_UI_URL,
success: false,
}
}
}

};
},
};
16 changes: 7 additions & 9 deletions src/packages/auth/adapters/NoAuthAdapter.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> => true,
validate: async (): Promise<ValidateResponse> => {

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,
};
}
}

},
};
4 changes: 2 additions & 2 deletions src/packages/auth/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Request } from "express";
import { Request } from 'express';

export type JsonWebToken = string;

Expand Down Expand Up @@ -49,4 +49,4 @@ export type ValidateResponse = {
export interface AuthAdapter {
validate(req: Request): Promise<ValidateResponse>;
logout(req: Request): Promise<boolean>;
};
}

0 comments on commit 5aa48d0

Please sign in to comment.