Skip to content

Commit

Permalink
add support for referrer based login UI redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
skovati committed Dec 14, 2023
1 parent 713edec commit 5a4e3bb
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
16 changes: 11 additions & 5 deletions src/packages/auth/adapters/CAMAuthAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { getEnv } from '../../../env.js';
import { generateJwt, getUserRoles } from "../functions.js";
import type { AuthAdapter, AuthResponse, ValidateResponse } from "../types.js";

import { Request } from "express";

type CAMValidateResponse = {
validated?: boolean;
errorCode?: string;
Expand All @@ -22,10 +24,10 @@ type CAMLoginResponse = {

export const CAMAuthAdapter: AuthAdapter = {

logout: async (cookies: any): Promise<boolean> => {

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

const cookies = req.cookies;
const ssoToken = cookies[AUTH_SSO_TOKEN_NAME];

const body = JSON.stringify({ ssoToken });
Expand All @@ -36,10 +38,10 @@ export const CAMAuthAdapter: AuthAdapter = {
return invalidated;
},

validate: async (cookies: any): Promise<ValidateResponse> => {

validate: async (req: Request): Promise<ValidateResponse> => {
const { AUTH_SSO_TOKEN_NAME, AUTH_URL, AUTH_UI_URL } = getEnv();

const cookies = req.cookies;
const ssoToken = cookies[AUTH_SSO_TOKEN_NAME];

const body = JSON.stringify({ ssoToken });
Expand All @@ -49,10 +51,14 @@ export const CAMAuthAdapter: AuthAdapter = {

const { validated = false, errorCode = false } = json;

const redirectTo = req.headers.referrer;

const redirectURL = `${AUTH_UI_URL}/?goto=${redirectTo}`;

if (errorCode || !validated) {
return {
message: "invalid token, redirecting to login UI",
redirectURL: AUTH_UI_URL,
redirectURL,
success: false
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/packages/auth/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default (app: Express, auth: AuthAdapter) => {
* - Auth
*/
app.get('/auth/validateSSO', loginLimiter, async (req, res) => {
const { token, success, message, userId, redirectURL } = await auth.validate(req.cookies);
const { token, success, message, userId, redirectURL } = await auth.validate(req);
const resp = {
message,
redirectURL,
Expand Down Expand Up @@ -99,7 +99,7 @@ export default (app: Express, auth: AuthAdapter) => {
* - Auth
*/
app.get('/auth/logoutSSO', async (req, res) => {
const success = await auth.logout(req.cookies);
const success = await auth.logout(req);
res.json({ success });
});

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

export type JsonWebToken = string;

export type JwtDecode = {
Expand Down Expand Up @@ -45,6 +47,6 @@ export type ValidateResponse = {
};

export interface AuthAdapter {
validate(cookies: any): Promise<ValidateResponse>;
logout(cookies: any): Promise<boolean>;
validate(req: Request): Promise<ValidateResponse>;
logout(req: Request): Promise<boolean>;
};

0 comments on commit 5a4e3bb

Please sign in to comment.