Skip to content

Commit

Permalink
🏷️(frontend) change the jwt type to a more generic one
Browse files Browse the repository at this point in the history
The jwt type was specific to the lti token. We had to change it
to a more generic one to be able to use it with different tokens,
with the web token in this case.
We had to get some permissions directly from the ressources instead of
the jwt, because the jwt didn't have everytime the informations depend
the token type.
  • Loading branch information
AntoLC committed Mar 17, 2023
1 parent 67a72e2 commit 1d26033
Show file tree
Hide file tree
Showing 20 changed files with 351 additions and 285 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
checkLtiToken,
checkToken,
decodeJwt,
FULL_SCREEN_ERROR_ROUTE,
uploadState,
Expand Down Expand Up @@ -31,7 +31,7 @@ const PublicVideoDashboard = ({
return generateVideoWebsocketUrl(currentVideo.id, (url) => {
const { jwt } = useJwt.getState();

if (!checkLtiToken(decodeJwt(jwt))) {
if (!checkToken(decodeJwt(jwt))) {
const anonymousId = getOrInitAnonymousId();
url = `${url}&anonymous_id=${anonymousId}`;
}
Expand Down
12 changes: 11 additions & 1 deletion src/frontend/packages/lib_components/src/types/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@ export interface DecodedJwtUser {
user_fullname?: Nullable<string>;
}

export interface DecodedJwt extends ResourceContext {
export type DecodedJwt = DecodedJwtLTI | DecodedJwtWeb;

export interface DecodedJwtLTI extends ResourceContext {
locale: string;
maintenance: boolean;
session_id: string;
user?: DecodedJwtUser;
}

export interface DecodedJwtWeb {
token_type: string;
exp: number;
iat: number;
jti: string;
user_id: string;
}

export interface TokenResponse {
access: string;
refresh: string;
Expand Down

This file was deleted.

10 changes: 0 additions & 10 deletions src/frontend/packages/lib_components/src/utils/checkLtiToken.ts

This file was deleted.

120 changes: 120 additions & 0 deletions src/frontend/packages/lib_components/src/utils/checkToken.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { DecodedJwt } from 'types/jwt';

import { checkToken } from './checkToken';

describe('checkToken', () => {
describe('checkToken LTI', () => {
it('should return true when the JWT token is a LTI one', () => {
const token: DecodedJwt = {
context_id: 'course-v1:ufr+mathematics+0001',
consumer_site: '112cf553-b8c3-4b98-9d47-d0793284b9b3',
locale: 'en_US',
maintenance: false,
permissions: {
can_access_dashboard: false,
can_update: false,
},
resource_id: '26debfee-8c3b-4c23-b08f-67f223de9832',
roles: ['student'],
session_id: '6bbb8d1d-442d-4575-a0ad-d1e34f37cae3',
user: {
email: null,
id: 'aaace992-49e3-4e01-b809-7a84b1b55b72',
username: null,
user_fullname: null,
},
};

expect(checkToken(token)).toEqual(true);
});
it('should return false when it is a lti token and the context_id is missing', () => {
const token: DecodedJwt = {
consumer_site: '112cf553-b8c3-4b98-9d47-d0793284b9b3',
locale: 'en_US',
maintenance: false,
permissions: {
can_access_dashboard: false,
can_update: false,
},
resource_id: '26debfee-8c3b-4c23-b08f-67f223de9832',
roles: ['student'],
session_id: '6bbb8d1d-442d-4575-a0ad-d1e34f37cae3',
user: {
email: null,
id: 'aaace992-49e3-4e01-b809-7a84b1b55b72',
username: null,
user_fullname: null,
},
};

expect(checkToken(token)).toEqual(false);
});

it('should return false when it is a lti token and the consumer_site is missing', () => {
const token: DecodedJwt = {
context_id: 'course-v1:ufr+mathematics+0001',
locale: 'en_US',
maintenance: false,
permissions: {
can_access_dashboard: false,
can_update: false,
},
resource_id: '26debfee-8c3b-4c23-b08f-67f223de9832',
roles: ['student'],
session_id: '6bbb8d1d-442d-4575-a0ad-d1e34f37cae3',
user: {
email: null,
id: 'aaace992-49e3-4e01-b809-7a84b1b55b72',
username: null,
user_fullname: null,
},
};

expect(checkToken(token)).toEqual(false);
});

it('should return false when it is a lti token and the user is missing', () => {
const token: DecodedJwt = {
context_id: 'course-v1:ufr+mathematics+0001',
consumer_site: '112cf553-b8c3-4b98-9d47-d0793284b9b3',
locale: 'en_US',
maintenance: false,
permissions: {
can_access_dashboard: false,
can_update: false,
},
resource_id: '26debfee-8c3b-4c23-b08f-67f223de9832',
roles: ['student'],
session_id: '6bbb8d1d-442d-4575-a0ad-d1e34f37cae3',
};

expect(checkToken(token)).toEqual(false);
});
});

describe('checkToken WEB', () => {
it('should return true when the JWT token is a web one', () => {
const token: DecodedJwt = {
token_type: 'user_access',
exp: 1678973516,
iat: 1678971446,
jti: '525a39a45c7347b8af36a0c3b904309d',
user_id: '602fc5bf-377d-4295-8c26-e7c57a30c454',
};

expect(checkToken(token)).toEqual(true);
});

it('should return false when it is a web token and the user is missing', () => {
const token: DecodedJwt = {
token_type: 'user_access',
exp: 1678973516,
iat: 1678971446,
jti: '525a39a45c7347b8af36a0c3b904309d',
user_id: '',
};

expect(checkToken(token)).toEqual(false);
});
});
});
23 changes: 23 additions & 0 deletions src/frontend/packages/lib_components/src/utils/checkToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { DecodedJwt, DecodedJwtLTI, DecodedJwtWeb } from 'types/jwt';

import { isDecodedJwtLTI, isDecodedJwtWeb } from './decodeJwt';

const checkLtiToken = (jwt: DecodedJwtLTI) => {
return !!(
jwt.context_id &&
jwt.consumer_site &&
jwt.user !== undefined &&
jwt.user.id
);
};

const checkWebToken = (jwt: DecodedJwtWeb) => {
return !!jwt.user_id;
};

export const checkToken = (jwt: DecodedJwt) => {
return (
(isDecodedJwtLTI(jwt) && checkLtiToken(jwt)) ||
(isDecodedJwtWeb(jwt) && checkWebToken(jwt))
);
};
Loading

0 comments on commit 1d26033

Please sign in to comment.