[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.pull/15392/head
parent
6b57f2288e
commit
bb61341ec1
@ -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; |
||||||
|
}; |
@ -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; |
||||||
|
} |
||||||
|
}; |
@ -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
|
||||||
|
}, |
||||||
|
}); |
Loading…
Reference in new issue