Repository navigation
[NEW] Add JWT to uploaded files urls - #15297
Conversation
renatobecker-zz
left a comment
There was a problem hiding this comment.
My suggestion is to create a method/property to get the file path from the FileUpload store, instead of testing the file system outside the FileUpload instance.
renatobecker-zz
left a comment
There was a problem hiding this comment.
@MarcosSpessatto, I left some comments on your PR. They're just improvements on the codebase, but in my opinion, it would uncouple the process of adding the JWT token to the URL.
| } | ||
|
|
||
| let { rc_uid, rc_token, rc_rid, rc_room_type } = query; | ||
| const { jwt } = query; |
There was a problem hiding this comment.
What about renaming the query parameter to token?
| import { hasPermission } from '../../../authorization/server/functions/hasPermission'; | ||
| import { canAccessRoom } from '../../../authorization/server/functions/canAccessRoom'; | ||
| import { fileUploadIsValidContentType } from '../../../utils/lib/fileUploadRestrictions'; | ||
| import { isValidJWT, generateJsonWebToken } from '../../../utils/server/lib/JWTHelper'; |
There was a problem hiding this comment.
What do you think of renaming to generateJWT, just like the same pattern on the isValidJWT ?
| request.get(fileUrl, (fileRes) => fileRes.pipe(res)); | ||
| }, | ||
|
|
||
| addJWTToFileUrlIfNecessary(message) { |
There was a problem hiding this comment.
IMHO, we should check the file field outside this method. I mean, to me it looks weird returning a message from a method that deals with JWT stuff.
So, then the method will return the JWT itself, not a message object.
| fileId: message.file._id, | ||
| }, settings.get('FileUpload_json_web_token_secret_for_files')); | ||
|
|
||
| if (message.attachments && Array.isArray(message.attachments) && message.attachments.length) { |
There was a problem hiding this comment.
...and this code looks to be a good helper method which would return the message object.
| } | ||
|
|
||
| return API.v1.success({ message }); | ||
| return API.v1.success({ message: FileUpload.addJWTToFileUrlIfNecessary(message) }); |
There was a problem hiding this comment.
As I commented before, IMO it would be better having a specific helper method to handle this process, and inside that method, I would call the addJWTToFileUrlIfNecessary method.
In that case, we could rename the addJWTToFileUrlIfNecessary to addJWTToFileUrl.
| return message; | ||
| } | ||
|
|
||
| message = FileUpload.addJWTToFileUrlIfNecessary(message); |
There was a problem hiding this comment.
As I commented before, IMO it would be better having a specific helper method to handle this process, and inside that method, I would call the addJWTToFileUrlIfNecessary method.
In that case, we could rename the addJWTToFileUrlIfNecessary to addJWTToFileUrl.
| return message; | ||
| } | ||
|
|
||
| message = FileUpload.addJWTToFileUrlIfNecessary(message); |
There was a problem hiding this comment.
As I commented before, IMO it would be better having a specific helper method to handle this process, and inside that method, I would call the addJWTToFileUrlIfNecessary method.
In that case, we could rename the addJWTToFileUrlIfNecessary to addJWTToFileUrl.
| } | ||
|
|
||
| postData.messages.push(msg); | ||
| postData.messages.push(message.file ? FileUpload.addJWTToFileUrlIfNecessary(message) : msg); |
There was a problem hiding this comment.
As I commented before, IMO it would be better having a specific helper method to handle this process, and inside that method, I would call the addJWTToFileUrlIfNecessary method.
In that case, we could rename the addJWTToFileUrlIfNecessary to addJWTToFileUrl.
| if (!(typeof room.t !== 'undefined' && room.t === 'l' && room.facebook && room.v && room.v.token)) { | ||
| return message; | ||
| } | ||
| FileUpload.addJWTToFileUrlIfNecessary(message); |
There was a problem hiding this comment.
As I commented before, IMO it would be better having a specific helper method to handle this process, and inside that method, I would call the addJWTToFileUrlIfNecessary method.
In that case, we could rename the addJWTToFileUrlIfNecessary to addJWTToFileUrl.
| return message; | ||
| } | ||
|
|
||
| FileUpload.addJWTToFileUrlIfNecessary(message); |
There was a problem hiding this comment.
As I commented before, IMO it would be better having a specific helper method to handle this process, and inside that method, I would call the addJWTToFileUrlIfNecessary method.
In that case, we could rename the addJWTToFileUrlIfNecessary to addJWTToFileUrl.
| return API.v1.success({ message }); | ||
| return API.v1.success({ message: normalizeMessageAttachments(message) }); | ||
| } catch (e) { | ||
| return API.v1.failure(e.error); |
There was a problem hiding this comment.
| return API.v1.failure(e.error); | |
| return API.v1.failure(e); |
| .map(normalizeMessageAttachments); | ||
| return API.v1.success(messages); | ||
| } catch (e) { | ||
| return API.v1.failure(e.error); |
There was a problem hiding this comment.
| return API.v1.failure(e.error); | |
| return API.v1.failure(e); |
| } | ||
|
|
||
| const messages = loadMessageHistory({ userId: guest._id, rid, end, limit, ls }); | ||
| const messages = loadMessageHistory({ userId: guest._id, rid, end, limit, ls }) |
There was a problem hiding this comment.
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.
|
|
||
| export const normalizeMessageAttachments = (message) => { | ||
| if (message.file && message.attachments && Array.isArray(message.attachments) && message.attachments.length) { | ||
| const jwt = FileUpload.addJWTToFileUrl({ rid: message.rid, userId: message.u._id, fileId: message.file._id }); |
There was a problem hiding this comment.
Since the FileUpload.addJWTToFileUrl may return null, you need to check the returned value before moving forward, don't you think?
Thanks.
| } | ||
|
|
||
| return API.v1.success({ message }); | ||
| return API.v1.success({ message: normalizeMessageAttachments(message) }); |
There was a problem hiding this comment.
Do you mind of checking the message.file before returning? I think we should always return the message instead of the normalizeMessageAttachments.
| if (result) { | ||
| const message = Messages.findOneById(_id); | ||
| return API.v1.success({ message }); | ||
| return API.v1.success({ message: normalizeMessageAttachments(message) }); |
There was a problem hiding this comment.
Do you mind of checking the message.file before returning? I think we should always return the message instead of the normalizeMessageAttachments.
| return message; | ||
| } | ||
|
|
||
| message = normalizeMessageAttachments(message); |
There was a problem hiding this comment.
Do you mind of checking the message.file before returning? I think we should always return the message instead of the normalizeMessageAttachments.
| postData.messages.push(msg); | ||
| if (message.file) { | ||
| msg.file = message.file; | ||
| msg.attachments = message.attachments; |
There was a problem hiding this comment.
Why not call the normalizer function inside this block of code?
| }); | ||
|
|
||
| return message; | ||
| return normalizeMessageAttachments(message); |
There was a problem hiding this comment.
Do you mind of checking the message.file before returning? I think we should always return the message instead of the normalizeMessageAttachments.
| SMSService.send(room.sms.from, visitor.phone[0].phoneNumber, message.msg); | ||
|
|
||
| return message; | ||
| return normalizeMessageAttachments(message); |
There was a problem hiding this comment.
Do you mind of checking the message.file before returning? I think we should always return the message instead of the normalizeMessageAttachments.
No description provided.