Skip to content

Commit

Permalink
Add keycloak token verification
Browse files Browse the repository at this point in the history
  • Loading branch information
Derstilon committed Oct 31, 2023
1 parent 1deb061 commit c71a54d
Showing 1 changed file with 57 additions and 42 deletions.
99 changes: 57 additions & 42 deletions src/services/AuthService.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Backdrop, CircularProgress, Theme, Typography } from '@mui/material';
import { KeycloakTokenParsed } from 'keycloak-js';
import ky, { HTTPError } from 'ky';
import { KyInstance } from 'ky/distribution/types/ky';
import { useSnackbar } from 'notistack';
Expand Down Expand Up @@ -177,52 +178,66 @@ const Auth = ({ children }: GenericContextProviderProps) => {
}, [demoMode, isServerReachable, user]);

const tokenVerification = useCallback(() => {
if (!initialized || !keycloak.authenticated) return;
const username = keycloak.tokenParsed?.preferred_username;
const checkPlgridAccessServices: (
token: KeycloakTokenParsed | undefined
) => Promise<void> = token => {
const yaptideServices = ['PLG_YAPTIDE_ACCESS'];

/**
* TODO: Check if user is authorized to use this application (e.g. by checking if user is in a certain group)
*/
const validUser = true;
const validUser = yaptideServices.every(
service => token?.plgridAccessServices?.includes(service)
);

if (!validUser)
openRejectKeycloakDialog({
reason: 'You are not authorized to use this application.',
keycloakAuth: { keycloak, initialized }
});
else if (initialized)
kyRef
.post(`auth/keycloak`, {
headers: {
Authorization: `Bearer ${keycloak.token}`
},
json: {
username
}
})
.json<ResponseAuthLogin>()
.then(({ accessExp }) => {
setUser(prev =>
prev?.username === username
? prev
: {
username,
source: 'keycloak'
}
);
setRefreshInterval(getRefreshDelay(accessExp));
})
.catch((err: HTTPError) => {
setUser(null);
setRefreshInterval(undefined);
openRejectKeycloakDialog({
reason:
err.response?.status === 403
? 'You are not authorized to use this application.'
: err.message,
keycloakAuth: { keycloak, initialized }
if (validUser) return Promise.resolve();
else
return Promise.reject(
`Your account does not have access to appropriate services. Services required: ${yaptideServices}`
);
};

if (!initialized || !keycloak.authenticated) return;

checkPlgridAccessServices(keycloak.tokenParsed)
.then(() => {
const username = keycloak.tokenParsed?.preferred_username;
kyRef
.post(`auth/keycloak`, {
headers: {
Authorization: `Bearer ${keycloak.token}`
},
json: {
username
}
})
.json<ResponseAuthLogin>()
.then(({ accessExp }) => {
setUser(prev =>
prev?.username === username
? prev
: {
username,
source: 'keycloak'
}
);
setRefreshInterval(getRefreshDelay(accessExp));
})
.catch((err: HTTPError) => {
setUser(null);
setRefreshInterval(undefined);
openRejectKeycloakDialog({
reason:
err.response?.status === 403
? 'You are not authorized to use this application.'
: err.message,
keycloakAuth: { keycloak, initialized }
});
});
})
.catch(reason => {
openRejectKeycloakDialog({
reason,
keycloakAuth: { keycloak, initialized }
});
});
}, [initialized, keycloak, kyRef, openRejectKeycloakDialog]);

useEffect(() => {
Expand Down

0 comments on commit c71a54d

Please sign in to comment.