Skip to content

Commit

Permalink
Merge pull request #218 from InseeFrLab/list_buckets_from_jwt
Browse files Browse the repository at this point in the history
List buckets from jwt
  • Loading branch information
garronej authored Mar 30, 2021
2 parents da26838 + dbf54fc commit 6dad3d2
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"homepage": "https://onyxia.lab.sspcloud.fr/",
"name": "onyxia-ui",
"version": "0.7.13",
"version": "0.7.14",
"license": "MIT",
"devDependencies": {
"@storybook/addon-actions": "^6.1.11",
Expand Down
2 changes: 1 addition & 1 deletion src/js/components/mes-fichiers/MyBuckets.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export function MyBuckets() {

useEffect(() => {
if (idep && !buckets) {
dispatch(myFilesActions.loadUserBuckets({ idep }));
dispatch(myFilesActions.loadUserBuckets());
}
}, [idep, dispatch, buckets]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export function NavigationFile(

if (!userBuckets) {

dispatch(actions.loadUserBuckets({ "idep": idep }));
dispatch(actions.loadUserBuckets());

return;

Expand Down
41 changes: 28 additions & 13 deletions src/js/redux/myFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { id } from "evt/tools/typeSafety/id";
import { assert } from "evt/tools/typeSafety/assert";
import * as minio from "js/minio-client/minio-tools";
import { PUSHER } from "js/components/notifications";
import type { AppThunk } from "lib/setup";
import { parseOidcAccessToken } from "lib/ports/OidcClient";


export type State = {
Expand Down Expand Up @@ -195,7 +197,28 @@ const asyncThunks = {
};


})()
})(),
"loadUserBuckets":
(): AppThunk => async (...args) => {

const [dispatch, , { oidcClient }] = args;

assert(oidcClient.isUserLoggedIn);

const { idep, groups } = await parseOidcAccessToken(oidcClient);

dispatch(
syncActions.loadUserBuckets({
"buckets":
[idep, ...groups].map((id, i) => ({
id,
"description": i === 0 ? "bucket personnel" : "", //TODO: Franglish
"isPublic": false
}))
})
);

}
};


Expand All @@ -210,20 +233,12 @@ const slice = createSlice({
"reducers": {
"loadUserBuckets": (
state,
{ payload }: PayloadAction<{ idep: State.Bucket["id"]; }>
{ payload }: PayloadAction<{ buckets: State.Bucket[]; }>
) => {

const { idep } = payload;
const { buckets } = payload;

assert(typeof idep === "string");

state.userBuckets = [
{
"id": idep,
"description": "bucket personnel", //TODO: Franglish
"isPublic": false
}
];
state.userBuckets = buckets;

},
"emptyCurrentBucket": state => {
Expand Down Expand Up @@ -270,10 +285,10 @@ const slice = createSlice({
}
});


const { actions: syncActions } = slice;

export const actions = {
...id<Pick<typeof syncActions, "loadUserBuckets">>(syncActions),
...asyncThunks
};

Expand Down
18 changes: 13 additions & 5 deletions src/lib/ports/OidcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,31 +57,39 @@ export declare namespace OidcClient {
export type ParsedOidcAccessToken = {
idep: string;
email: string;
groups: string[];
};

export type ParsedJwt = {
gitlab_group: string[] | null;
preferred_username: string;
name: string;
email: string;
groups: string[];
};

export async function parseOidcAccessToken(
oidcClient: Pick<OidcClient.LoggedIn, "evtOidcTokens" | "renewOidcTokensIfExpiresSoonOrRedirectToLoginIfAlreadyExpired">
): Promise<ParsedOidcAccessToken> {

const {
email,
preferred_username,
} = jwtSimple.decode(
const parsedJwt = jwtSimple.decode(
(await oidcClient.evtOidcTokens.waitFor(nonNullable())).accessToken,
"",
true
) as ParsedJwt;

console.log(JSON.stringify(parsedJwt,null,2));

const {
email,
preferred_username,
groups
} = parsedJwt;

return {
"idep": preferred_username,
email
email,
groups
};

}
3 changes: 2 additions & 1 deletion src/lib/secondaryAdapters/phonyOidcClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ function createFakeTokenApi(
id<ParsedJwt>({
...parsedJwt,
"gitlab_group": null,
"name": ""
"name": "",
"groups": [ "sspcloud-admin" ]
}),
"xxx"
),
Expand Down

0 comments on commit 6dad3d2

Please sign in to comment.