-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[NEW] Add JWT to uploaded files urls #15297
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
50f7ad3
Add JWT to uploaded files urls
MarcosSpessatto 749bcbb
Add JWT to files inside messages on REST endpoints
MarcosSpessatto bfa9413
Improve code
MarcosSpessatto 6f33df3
Move File Helper to utils folder
MarcosSpessatto 5608041
Move JWT to File Upload class and improve code
MarcosSpessatto 57c4466
Merge branch 'develop' into add-jwt-to-file-upload-urls
renatobecker e68da32
Code improve
MarcosSpessatto 1ac20e3
Rename function
MarcosSpessatto 9f50a05
Merge branch 'develop' into add-jwt-to-file-upload-urls
MarcosSpessatto cfdf556
Merge branch 'develop' into add-jwt-to-file-upload-urls
MarcosSpessatto 96a6c67
Improve code
MarcosSpessatto 84938a8
Fix undefined tokens and rename function
MarcosSpessatto 2c26139
Apply suggestions
MarcosSpessatto 26a2825
Final version.
renatobecker c31d52a
Merge branch 'develop' into add-jwt-to-file-upload-urls
MarcosSpessatto a0466bf
Merge branch 'develop' into add-jwt-to-file-upload-urls
renatobecker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import { settings } from '../../../settings'; | |
import { callbacks } from '../../../callbacks'; | ||
import { Messages, LivechatRooms } from '../../../models'; | ||
import { Livechat } from '../lib/Livechat'; | ||
import { normalizeMessageAttachments } from '../../../utils/server/functions/normalizeMessageAttachments'; | ||
|
||
const msgNavType = 'livechat_navigation_history'; | ||
|
||
|
@@ -55,7 +56,12 @@ function sendToCRM(type, room, includeMessages = true) { | |
msg.navigation = message.navigation; | ||
} | ||
|
||
postData.messages.push(msg); | ||
if (message.file) { | ||
msg.file = message.file; | ||
msg.attachments = message.attachments; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not call the |
||
} | ||
|
||
postData.messages.push(normalizeMessageAttachments(msg)); | ||
}); | ||
} | ||
|
||
|
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 | ||
}, | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new implementation is returning an array of messages, but the original implementation needs to return an object containing a property called by
messages
, which will contain the array of messages.