-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NEW] Add JWT to uploaded files urls (#15297)
* Add JWT to uploaded files urls * Add JWT to files inside messages on REST endpoints * Improve code * Move File Helper to utils folder * Move JWT to File Upload class and improve code * Code improve * Rename function * Improve code * Fix undefined tokens and rename function * Apply suggestions * Final version.
- Loading branch information
1 parent
6b57f22
commit bb61341
Showing
14 changed files
with
178 additions
and
11 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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { FileUpload } from '../../../file-upload/server'; | ||
|
||
export const normalizeMessageAttachments = (message) => { | ||
if (message.file && message.attachments && Array.isArray(message.attachments) && message.attachments.length) { | ||
const jwt = FileUpload.generateJWTToFileUrls({ rid: message.rid, userId: message.u._id, fileId: message.file._id }); | ||
if (jwt) { | ||
message.attachments.forEach((attachment) => { | ||
if (attachment.title_link) { | ||
attachment.title_link = `${ attachment.title_link }?token=${ jwt }`; | ||
} | ||
if (attachment.image_url) { | ||
attachment.image_url = `${ attachment.image_url }?token=${ jwt }`; | ||
} | ||
}); | ||
} | ||
} | ||
return message; | ||
}; |
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,28 @@ | ||
import { jws } from 'jsrsasign'; | ||
|
||
const HEADER = { | ||
typ: 'JWT', | ||
alg: 'HS256', | ||
}; | ||
|
||
export const generateJWT = (payload, secret) => { | ||
const tokenPayload = { | ||
iat: jws.IntDate.get('now'), | ||
nbf: jws.IntDate.get('now'), | ||
exp: jws.IntDate.get('now + 1hour'), | ||
aud: 'RocketChat', | ||
context: payload, | ||
}; | ||
|
||
const header = JSON.stringify(HEADER); | ||
|
||
return jws.JWS.sign(HEADER.alg, header, JSON.stringify(tokenPayload), { rstr: secret }); | ||
}; | ||
|
||
export const isValidJWT = (jwt, secret) => { | ||
try { | ||
return jws.JWS.verify(jwt, secret, HEADER); | ||
} catch (error) { | ||
return false; | ||
} | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,4 +154,5 @@ import './v153'; | |
import './v154'; | ||
import './v155'; | ||
import './v156'; | ||
import './v157'; | ||
import './xrun'; |
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,44 @@ | ||
import { Random } from 'meteor/random'; | ||
|
||
import { Migrations } from '../../../app/migrations/server'; | ||
import { Settings } from '../../../app/models/server'; | ||
import { settings } from '../../../app/settings/server'; | ||
|
||
Migrations.add({ | ||
version: 157, | ||
up() { | ||
Settings.upsert({ | ||
_id: 'FileUpload_Enable_json_web_token_for_files', | ||
}, | ||
{ | ||
_id: 'FileUpload_Enable_json_web_token_for_files', | ||
value: settings.get('FileUpload_ProtectFiles'), | ||
type: 'boolean', | ||
group: 'FileUpload', | ||
i18nLabel: 'FileUpload_Enable_json_web_token_for_files', | ||
i18nDescription: 'FileUpload_Enable_json_web_token_for_files_description', | ||
enableQuery: { | ||
_id: 'FileUpload_ProtectFiles', | ||
value: true, | ||
}, | ||
}); | ||
Settings.upsert({ | ||
_id: 'FileUpload_json_web_token_secret_for_files', | ||
}, | ||
{ | ||
_id: 'FileUpload_json_web_token_secret_for_files', | ||
value: Random.secret(), | ||
type: 'string', | ||
group: 'FileUpload', | ||
i18nLabel: 'FileUpload_json_web_token_secret_for_files', | ||
i18nDescription: 'FileUpload_json_web_token_secret_for_files_description', | ||
enableQuery: { | ||
_id: 'FileUpload_Enable_json_web_token_for_files', | ||
value: true, | ||
}, | ||
}); | ||
}, | ||
down() { | ||
// Down migration does not apply in this case | ||
}, | ||
}); |