Skip to content

Commit

Permalink
Feat/org token (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
jazzgrewal authored Aug 8, 2024
1 parent 577692b commit f58ad0e
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 49 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ services:
profiles: ["native"]
build: ./backend
ports: ["8080:8080"]
<<: *backend
<<: *backend
4 changes: 4 additions & 0 deletions frontend/src/components/BCHeaderwSide/BCHeaderwSide.scss
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ button.#{vars.$bcgov-prefix}--header__action.#{vars.$bcgov-prefix}--header__menu
will-change: width;
}

.#{vars.$bcgov-prefix}--header-panel{
overflow-y: scroll;
}

.overlay-element {
position: fixed;
top: 3rem;
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/components/MyProfile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ const MyProfile = () => {
>
Log out
</SideNavLink>

</ul>
<hr className="divisory mt-5" />
</nav>
</>
);
Expand Down
48 changes: 6 additions & 42 deletions frontend/src/services/AuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
signOut
} from 'aws-amplify/auth';
import { env } from '../env';
import { UserClientRolesType } from '../types/UserRoleType';
import { CognitoUserSession } from 'amazon-cognito-identity-js';
import { formatRolesArray } from '../utils/famUtils';

// Define a global variable to store the ID token
let authIdToken: string | null = null;
Expand All @@ -17,7 +18,7 @@ export interface FamLoginUser {
username?: string;
idpProvider?: string;
roles?: string[];
clientIds?: string[]; // Add clientIds to FamLoginUser interface
authToken?: CognitoUserSession;
exp?: number;
}

Expand Down Expand Up @@ -158,41 +159,16 @@ function parseToken(idToken: JWT | undefined, accessToken: JWT | undefined): Fam
roles = decodedAccessToken['cognito:groups'] as Array<string>;

Check failure on line 159 in frontend/src/services/AuthService.ts

View workflow job for this annotation

GitHub Actions / Lint (Frontend)

'roles' is assigned a value but never used
}

// Extract client IDs from roles
const clientIds = parseClientIdsFromRoles(roles);

//hard coded data
// Define the roles array based on UserClientRolesType structure
const rolesArray: UserClientRolesType[] = [
{
clientId: '00132184',
roles: ['role1', 'role2'], // Example roles for clientId '00132184'
clientName: 'Client Name 1'
},
{
clientId: '00012797',
roles: ['role3'], // Example roles for clientId '00012797'
clientName: 'Client Name 2'
},
{
clientId: '00001012',
roles: ['role4', 'role5'], // Example roles for clientId '00001012'
clientName: 'Client Name 3'
},
{
clientId: '00149081',
roles: ['role6'], // Example roles for clientId '00149081'
clientName: 'Client Name 4'
}
];

//get the user roles from the FAM token
const rolesArray = formatRolesArray(decodedIdToken);

const famLoginUser = {
userName,
displayName,
email,
idpProvider,
clientRoles: rolesArray,
clientIds,
exp: idToken?.payload.exp,
firstName: sanitizedFirstName,
lastName
Expand All @@ -202,18 +178,6 @@ const rolesArray: UserClientRolesType[] = [
return famLoginUser;
}

/**
* Function to parse client IDs from roles
*/
function parseClientIdsFromRoles(roles: string[]): string[] {
// Implement logic to extract client IDs from roles here
// Placeholder implementation
return roles.map(role => {
const parts = role.split(':');
return parts[parts.length - 1];
});
}

/**
*
*/
Expand Down
7 changes: 1 addition & 6 deletions frontend/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@ import type { CognitoUserSession } from 'amazon-cognito-identity-js'
import { userDetailsReducer } from './reducers/userReducer'
import { UserClientRolesType } from './types/UserRoleType'
import { selectedClientRolesReducer } from './reducers/selectedClientRolesReducer'
import { FamLoginUser } from './services/AuthService'

const reducer = combineReducers({
userDetails: userDetailsReducer,
selectedClientRoles: selectedClientRolesReducer
});

export interface FamLoginUser {
username?: string;
idpProvider?: string;
roles?: string[];
authToken?: CognitoUserSession;
}
const FAM_LOGIN_USER = 'famLoginUser';

const userInfoFromStorage = JSON.parse(localStorage.getItem(FAM_LOGIN_USER) as string) as
Expand Down
28 changes: 28 additions & 0 deletions frontend/src/utils/famUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { UserClientRolesType } from "../types/UserRoleType";

export function formatRolesArray(decodedIdToken: any): UserClientRolesType[] {

Check warning on line 3 in frontend/src/utils/famUtils.ts

View workflow job for this annotation

GitHub Actions / Lint (Frontend)

Missing JSDoc comment

Check failure on line 3 in frontend/src/utils/famUtils.ts

View workflow job for this annotation

GitHub Actions / Lint (Frontend)

Unexpected any. Specify a different type
if (!decodedIdToken || !decodedIdToken['cognito:groups']) {
return [];
}

const cognitoGroups: string[] = decodedIdToken['cognito:groups'];
const rolesMap: { [key: string]: string[] } = {};

cognitoGroups.forEach(group => {
const [role, clientId] = group.split('_');
if (!rolesMap[clientId]) {
rolesMap[clientId] = [];
}
rolesMap[clientId].push(role);
});

const rolesArray: UserClientRolesType[] = Object.keys(rolesMap).map(clientId => ({
clientId,
roles: rolesMap[clientId],
clientName: `Client Number ${clientId}` // Placeholder for client name, modify as needed
}));
console.log(rolesArray)

return rolesArray;
}

0 comments on commit f58ad0e

Please sign in to comment.