-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🏷️(frontend) change the jwt type to a more generic one
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
Showing
20 changed files
with
351 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 0 additions & 92 deletions
92
src/frontend/packages/lib_components/src/utils/checkLtiToken.spec.ts
This file was deleted.
Oops, something went wrong.
10 changes: 0 additions & 10 deletions
10
src/frontend/packages/lib_components/src/utils/checkLtiToken.ts
This file was deleted.
Oops, something went wrong.
120 changes: 120 additions & 0 deletions
120
src/frontend/packages/lib_components/src/utils/checkToken.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
src/frontend/packages/lib_components/src/utils/checkToken.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
); | ||
}; |
Oops, something went wrong.