Skip to content

Commit

Permalink
Fix: OIDC (#699)
Browse files Browse the repository at this point in the history
* fix: use sub for OIDC RBAC

* fix: stop policies fetch loop after logout
  • Loading branch information
fabio-silva committed Sep 19, 2024
1 parent c9abd86 commit bdf985f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 10 additions & 5 deletions ui/apps/everest/src/contexts/auth/auth.provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import {
UserAuthStatus,
} from './auth.context.types';
import { isAfter } from 'date-fns';
import { initializeAuthorizerFetchLoop } from 'utils/rbac';
import {
initializeAuthorizerFetchLoop,
stopAuthorizerFetchLoop,
} from 'utils/rbac';

const Provider = ({
oidcConfig,
Expand Down Expand Up @@ -124,6 +127,7 @@ const AuthProvider = ({ children, isSsoEnabled }: AuthProviderProps) => {
await userManager.clearStaleState();
await userManager.removeUser();
}
stopAuthorizerFetchLoop();
}, [userManager]);

const silentlyRenewToken = useCallback(async () => {
Expand All @@ -140,7 +144,8 @@ const AuthProvider = ({ children, isSsoEnabled }: AuthProviderProps) => {
if (isSsoEnabled) {
userManager.events.addUserLoaded((user) => {
localStorage.setItem('everestToken', user.id_token || '');
setLoggedInStatus(user.profile.name || '');
const decoded = jwtDecode(user.id_token || '');
setLoggedInStatus(decoded.sub || '');
});

userManager.events.addAccessTokenExpiring(() => {
Expand All @@ -165,10 +170,10 @@ const AuthProvider = ({ children, isSsoEnabled }: AuthProviderProps) => {
const decoded = jwtDecode(token);
const iss = decoded.iss;
const exp = decoded.exp;
const username =
decoded.sub?.substring(0, decoded.sub.indexOf(':')) || '';
if (iss === EVEREST_JWT_ISSUER) {
const isTokenValid = await checkAuth(token);
const username =
decoded.sub?.substring(0, decoded.sub.indexOf(':')) || '';
if (isTokenValid) {
setLoggedInStatus(username);
} else {
Expand All @@ -185,7 +190,7 @@ const AuthProvider = ({ children, isSsoEnabled }: AuthProviderProps) => {
if (!user) {
setLogoutStatus();
} else {
setLoggedInStatus(username);
setLoggedInStatus(decoded.sub || '');
return;
}
}
Expand Down
4 changes: 4 additions & 0 deletions ui/apps/everest/src/utils/rbac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ export const initializeAuthorizerFetchLoop = async (user: string) => {
}, 5000);
};

export const stopAuthorizerFetchLoop = () => {
clearInterval(timeoutId);
};

export const can = async (
action: RBACAction,
resource: RBACResource,
Expand Down

0 comments on commit bdf985f

Please sign in to comment.