Skip to content

Commit

Permalink
♻️(frontend) rename simple_jwt attributes
Browse files Browse the repository at this point in the history
As we now have only playlist tokens, the simple_jwt attributes must be
renamed for reflecting it.
  • Loading branch information
kernicPanel committed Aug 22, 2023
1 parent 4faa46d commit 545a559
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('<AppContentLoader />', () => {

it('initialize current user before loading content', async () => {
useJwt.setState({
jwt: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2lkIjoic29tZV9zZXNzaW9uX2lkIiwidXNlciI6eyJhbm9ueW1vdXNfaWQiOiJhbm9ueW1vdXMgaWQiLCJlbWFpbCI6InNvbWUgZW1haWwiLCJpZCI6ImlkIiwidXNlcm5hbWUiOiJ1c2VyIG5hbWUiLCJ1c2VyX2Z1bGxuYW1lIjoidXNlciBmdWxsIG5hbWUifSwibG9jYWxlIjoicGwiLCJtYWludGVuYW5jZSI6ZmFsc2UsInBlcm1pc3Npb25zIjp7ImNhbl9hY2Nlc3NfZGFzaGJvYXJkIjpmYWxzZSwiY2FuX3VwZGF0ZSI6ZmFsc2V9LCJyZXNvdXJjZV9pZCI6InJlc291cmNlIGlkIiwicm9sZXMiOltdfQ.gv0kmitQfOv93TQuFTHsiqQJFWeTkbmb1h8J8uMVX70',
jwt: 'ewogICJhbGciOiAiSFMyNTYiLAogICJ0eXAiOiAiSldUIgp9.ewogICJzZXNzaW9uX2lkIjogInNvbWVfc2Vzc2lvbl9pZCIsCiAgInVzZXIiOiB7CiAgICAiYW5vbnltb3VzX2lkIjogImFub255bW91cyBpZCIsCiAgICAiZW1haWwiOiAic29tZSBlbWFpbCIsCiAgICAiaWQiOiAiaWQiLAogICAgInVzZXJuYW1lIjogInVzZXIgbmFtZSIsCiAgICAidXNlcl9mdWxsbmFtZSI6ICJ1c2VyIGZ1bGwgbmFtZSIKICB9LAogICJsb2NhbGUiOiAicGwiLAogICJtYWludGVuYW5jZSI6IGZhbHNlLAogICJwZXJtaXNzaW9ucyI6IHsKICAgICJjYW5fYWNjZXNzX2Rhc2hib2FyZCI6IGZhbHNlLAogICAgImNhbl91cGRhdGUiOiBmYWxzZQogIH0sCiAgInBsYXlsaXN0X2lkIjogInBsYXlsaXN0IGlkIiwKICAicm9sZXMiOiBbXQp9.gv0kmitQfOv93TQuFTHsiqQJFWeTkbmb1h8J8uMVX70',
});

const playlist = playlistMockFactory({ id: '488db2d0' });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ const AppContent = () => {
};

const AppContentLoader = () => {
const appData = useAppConfig();
const [isLoaded, setIsLoaded] = useState(false);
const queryClient = new QueryClient();

Expand All @@ -141,10 +142,14 @@ const AppContentLoader = () => {
};

if (decodedJwt) {
return { ...decodedJwt };
return {
...decodedJwt,
resource_id: appData.resource?.id || '',
playlist_id: (decodedJwt.port_to_playlist_id as string) || '',
};
}
return defaultResourceContext;
}, [decodedJwt]);
}, [appData.resource?.id, decodedJwt]);

useEffect(() => {
const loadIntl = async () => {
Expand Down
14 changes: 7 additions & 7 deletions src/frontend/apps/lti_site/components/ClaimLink/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ describe('<ClaimLink />', () => {
<ClaimLink
decodedJwt={
{
playlist_id: '488db2d0',
resource_id: 'e8c0b8d0',
port_to_playlist_id: '488db2d0',
playlist_id: 'e8c0b8d0',
consumer_site: '32a1c2d0',
user: { id: '6b45a4d6' },
} as DecodedJwtLTI
Expand All @@ -82,7 +82,7 @@ describe('<ClaimLink />', () => {
'Please login to manage this resource on http://localhost:8000/',
);
expect(link.getAttribute('href')).toEqual(
'http://localhost:8000//claim-resource?lti_consumer_site_id=32a1c2d0&resource_id=e8c0b8d0&modelName=videos&playlist_id=488db2d0&lti_user_id=6b45a4d6',
`http://localhost:8000//claim-resource?lti_consumer_site_id=32a1c2d0&resource_id=${video.id}&modelName=videos&playlist_id=488db2d0&lti_user_id=6b45a4d6`,
);
expect(link.getAttribute('target')).toEqual('_blank');
expect(link.getAttribute('rel')).toEqual('noopener noreferrer');
Expand Down Expand Up @@ -135,8 +135,8 @@ describe('<ClaimLink />', () => {
<ClaimLink
decodedJwt={
{
playlist_id: '488db2d0',
resource_id: 'e8c0b8d0',
port_to_playlist_id: '488db2d0',
playlist_id: 'e8c0b8d0',
consumer_site: '32a1c2d0',
user: { id: '6b45a4d6' },
} as DecodedJwtLTI
Expand Down Expand Up @@ -197,8 +197,8 @@ describe('<ClaimLink />', () => {
<ClaimLink
decodedJwt={
{
playlist_id: '488db2d0',
resource_id: 'e8c0b8d0',
port_to_playlist_id: '488db2d0',
playlist_id: 'e8c0b8d0',
consumer_site: '32a1c2d0',
user: { id: '6b45a4d6' },
} as DecodedJwtLTI
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/apps/lti_site/components/ClaimLink/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const ClaimLink = ({ decodedJwt }: ClaimLinkProps) => {
let claimUrl = `${appConfig.frontend_home_url}/claim-resource`;
if (showClaimLink) {
claimUrl += `?lti_consumer_site_id=${decodedJwt.consumer_site}`;
claimUrl += `&resource_id=${decodedJwt.resource_id}`;
claimUrl += `&resource_id=${resource.id}`;
claimUrl += `&modelName=${appConfig.modelName}`;
claimUrl += `&playlist_id=${resource.playlist.id}`;
claimUrl += `&lti_user_id=${decodedJwt.user?.id}`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ describe('<useAuthenticator />', () => {
featureContentLoader([]);
const classroom = classroomMockFactory();
const legacyInvite =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbl90eXBlIjoicmVzb3VyY2VfYWNjZXNzIiwiZXhwIjoxNjg5MjA2NDAwLCJpYXQiOjE2ODY1ODYwMzgsImp0aSI6ImNsYXNzcm9vbS1pbnZpdGUtMGJjZWIxZDItM2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chMock.get('/api/users/whoami/', whoAmIResponse200);

const { result } = renderHook(() => useAuthenticator(), {
Expand Down
3 changes: 2 additions & 1 deletion src/frontend/packages/lib_components/src/types/jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ export interface DecodedJwtUser {

export type DecodedJwt = DecodedJwtLTI | DecodedJwtWeb;

export interface DecodedJwtLTI extends ResourceContext {
export interface DecodedJwtLTI extends Omit<ResourceContext, 'resource_id'> {
locale: string;
maintenance: boolean;
port_to_playlist_id?: string;
session_id: string;
user?: DecodedJwtUser;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('checkToken', () => {
can_access_dashboard: false,
can_update: false,
},
resource_id: '26debfee-8c3b-4c23-b08f-67f223de9832',
playlist_id: '26debfee-8c3b-4c23-b08f-67f223de9832',
roles: ['student'],
session_id: '6bbb8d1d-442d-4575-a0ad-d1e34f37cae3',
user: {
Expand All @@ -36,7 +36,7 @@ describe('checkToken', () => {
can_access_dashboard: false,
can_update: false,
},
resource_id: '26debfee-8c3b-4c23-b08f-67f223de9832',
playlist_id: '26debfee-8c3b-4c23-b08f-67f223de9832',
roles: ['student'],
session_id: '6bbb8d1d-442d-4575-a0ad-d1e34f37cae3',
user: {
Expand All @@ -59,7 +59,7 @@ describe('checkToken', () => {
can_access_dashboard: false,
can_update: false,
},
resource_id: '26debfee-8c3b-4c23-b08f-67f223de9832',
playlist_id: '26debfee-8c3b-4c23-b08f-67f223de9832',
roles: ['student'],
session_id: '6bbb8d1d-442d-4575-a0ad-d1e34f37cae3',
user: {
Expand All @@ -83,7 +83,7 @@ describe('checkToken', () => {
can_access_dashboard: false,
can_update: false,
},
resource_id: '26debfee-8c3b-4c23-b08f-67f223de9832',
playlist_id: '26debfee-8c3b-4c23-b08f-67f223de9832',
roles: ['student'],
session_id: '6bbb8d1d-442d-4575-a0ad-d1e34f37cae3',
};
Expand Down
20 changes: 10 additions & 10 deletions src/frontend/packages/lib_components/src/utils/decodeJwt.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ describe('decodeJwt', () => {
// "sub": "1234567890",
// "name": "John Doe",
// "iat": 1516239022
// "resource_id": ""
// "playlist_id": ""
const jwt =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJyZXNvdXJjZV9pZCI6IiJ9.2AAf0eZ75jFyAdREUMXr3CyOt9QPoqSuIZ5lNj3c7CE';
'ewogICJhbGciOiAiSFMyNTYiLAogICJ0eXAiOiAiSldUIgp9.ewogICJzdWIiOiAiMTIzNDU2Nzg5MCIsCiAgIm5hbWUiOiAiSm9obiBEb2UiLAogICJpYXQiOiAxNTE2MjM5MDIyLAogICJwbGF5bGlzdF9pZCI6ICIiCn0.2AAf0eZ75jFyAdREUMXr3CyOt9QPoqSuIZ5lNj3c7CE';

expect(() => {
decodeJwt(jwt);
Expand All @@ -41,13 +41,13 @@ describe('decodeJwt', () => {
// "sub": "1234567890",
// "name": "John Doe",
// "iat": 1516239022,
// "resource_id": "2c85dc9e-4ee0-11ed-b9c1-635afde8841a"
// "playlist_id": "2c85dc9e-4ee0-11ed-b9c1-635afde8841a"
const jwt =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJyZXNvdXJjZV9pZCI6IjJjODVkYzllLTRlZTAtMTFlZC1iOWMxLTYzNWFmZGU4ODQxYSJ9.fQuKBTccQyt9fJGctJsLK5KL5lRisAGDZGFrBRdVPg8';
'ewogICJhbGciOiAiSFMyNTYiLAogICJ0eXAiOiAiSldUIgp9.ewogICJzdWIiOiAiMTIzNDU2Nzg5MCIsCiAgIm5hbWUiOiAiSm9obiBEb2UiLAogICJpYXQiOiAxNTE2MjM5MDIyLAogICJwbGF5bGlzdF9pZCI6ICIyYzg1ZGM5ZS00ZWUwLTExZWQtYjljMS02MzVhZmRlODg0MWEiCn0.fQuKBTccQyt9fJGctJsLK5KL5lRisAGDZGFrBRdVPg8';

const decodedJwt = decodeJwt(jwt);
expect(isDecodedJwtLTI(decodedJwt)).toBeTruthy();
expect((decodedJwt as DecodedJwtLTI).resource_id).toEqual(
expect((decodedJwt as DecodedJwtLTI).playlist_id).toEqual(
'2c85dc9e-4ee0-11ed-b9c1-635afde8841a',
);
});
Expand All @@ -56,16 +56,16 @@ describe('decodeJwt', () => {
// "sub": "1234567890",
// "name": "John Doe",
// "iat": 1516239022,
// "resource_id": "",
// "playlist_id": "8cb4b9fa-4ee0-11ed-a972-87a03ecac56f",
// "playlist_id": "",
// "port_to_playlist_id": "8cb4b9fa-4ee0-11ed-a972-87a03ecac56f",
// "user": {"id": "8d6d0ec4-4ee0-11ed-8d9b-97f53b688cde"}
const jwt =
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyLCJyZXNvdXJjZV9pZCI6IiIsInBsYXlsaXN0X2lkIjoiOGNiNGI5ZmEtNGVlMC0xMWVkLWE5NzItODdhMDNlY2FjNTZmIiwidXNlciI6eyJpZCI6IjhkNmQwZWM0LTRlZTAtMTFlZC04ZDliLTk3ZjUzYjY4OGNkZSJ9fQ._ha6x03gxBMYXPpofIAVDB3HBJXg0JRnmPUr2YPdG-A';
'ewogICJhbGciOiAiSFMyNTYiLAogICJ0eXAiOiAiSldUIgp9.ewogICJzdWIiOiAiMTIzNDU2Nzg5MCIsCiAgIm5hbWUiOiAiSm9obiBEb2UiLAogICJpYXQiOiAxNTE2MjM5MDIyLAogICJwbGF5bGlzdF9pZCI6ICIiLAogICJwb3J0X3RvX3BsYXlsaXN0X2lkIjogIjhjYjRiOWZhLTRlZTAtMTFlZC1hOTcyLTg3YTAzZWNhYzU2ZiIsCiAgInVzZXIiOiB7CiAgICAiaWQiOiAiOGQ2ZDBlYzQtNGVlMC0xMWVkLThkOWItOTdmNTNiNjg4Y2RlIgogIH0KfQ._ha6x03gxBMYXPpofIAVDB3HBJXg0JRnmPUr2YPdG-A';

const decodedJwt = decodeJwt(jwt);
expect(isDecodedJwtLTI(decodedJwt)).toBeTruthy();
expect((decodedJwt as DecodedJwtLTI).resource_id).toEqual('');
expect((decodedJwt as DecodedJwtLTI).playlist_id).toEqual(
expect((decodedJwt as DecodedJwtLTI).playlist_id).toEqual('');
expect((decodedJwt as DecodedJwtLTI).port_to_playlist_id).toEqual(
'8cb4b9fa-4ee0-11ed-a972-87a03ecac56f',
);
expect((decodedJwt as DecodedJwtLTI).user!.id).toEqual(
Expand Down
6 changes: 3 additions & 3 deletions src/frontend/packages/lib_components/src/utils/decodeJwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import {

export const isDecodedJwtLTI = (jwt: unknown): jwt is DecodedJwtLTI => {
if (jwt && typeof jwt === 'object') {
const resourceId = (jwt as DecodedJwtLTI).resource_id;
const playlistId = (jwt as DecodedJwtLTI).playlist_id;
const portToPlaylistId = (jwt as DecodedJwtLTI).port_to_playlist_id;
const userId = (jwt as DecodedJwtLTI).user?.id;
return (
// A resource is defined
// Or we are in a portability request context and a playlist and user ID are mandatory
(!!resourceId && typeof resourceId === 'string') ||
(!resourceId && !!playlistId && !!userId)
(!!playlistId && typeof playlistId === 'string') ||
(!playlistId && !!portToPlaylistId && !!userId)
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export const ltiPublicTokenMockFactory = (
can_update: false,
...permission,
},
resource_id: faker.datatype.uuid(),
playlist_id: faker.datatype.uuid(),
roles: ['none'],
session_id: faker.datatype.uuid(),
...token,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('<RegistrationForm />', () => {
can_access_dashboard: false,
can_update: false,
},
resource_id: 'resource id',
playlist_id: 'playlist id',
roles: ['student'],
session_id: 'session id',
user: {
Expand Down Expand Up @@ -121,7 +121,7 @@ describe('<RegistrationForm />', () => {
can_access_dashboard: false,
can_update: false,
},
resource_id: 'resource id',
playlist_id: 'playlist_id',
roles: ['student'],
session_id: 'session id',
user: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('<StudentLiveRegistration />', () => {
can_access_dashboard: false,
can_update: false,
},
resource_id: 'ressource_id',
playlist_id: 'playlist_id',
roles: [],
session_id: 'session_id',
user: {
Expand Down Expand Up @@ -254,7 +254,7 @@ describe('<StudentLiveRegistration />', () => {
can_access_dashboard: false,
can_update: false,
},
resource_id: 'ressource_id',
playlist_id: 'playlist_id',
roles: [],
session_id: 'session_id',
user: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jest.mock('lib-components', () => ({
can_access_dashboard: false,
can_update: false,
},
resource_id: 'ressource_id',
playlist_id: 'playlist_id',
roles: [],
session_id: 'session_id',
user: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { decodeJwt, useCurrentUser, useJwt } from 'lib-components';
import {
DecodedJwtLTI,
decodeJwt,
useCurrentUser,
useJwt,
} from 'lib-components';
import { v4 as uuidv4 } from 'uuid';

import { getOrInitAnonymousId } from './getOrInitAnonymousId';
Expand Down Expand Up @@ -34,7 +39,7 @@ const webToken = {
user_id: '1324',
};

const ltiToken = {
const ltiToken: DecodedJwtLTI = {
context_id: 'course-v1:ufr+mathematics+0001',
consumer_site: '112cf553-b8c3-4b98-9d47-d0793284b9b3',
locale: 'en_US',
Expand All @@ -43,7 +48,7 @@ const ltiToken = {
can_access_dashboard: false,
can_update: false,
},
resource_id: '26debfee-8c3b-4c23-b08f-67f223de9832',
playlist_id: '26debfee-8c3b-4c23-b08f-67f223de9832',
roles: ['student'],
session_id: '6bbb8d1d-442d-4575-a0ad-d1e34f37cae3',
user: {
Expand Down

0 comments on commit 545a559

Please sign in to comment.