Skip to content

[NEW] Add JWT to uploaded files urls - #15297

Merged
renatobecker-zz merged 16 commits into
developfrom
add-jwt-to-file-upload-urls
Sep 17, 2019
Merged

renatobecker-zz merged 16 commits into
developfrom
add-jwt-to-file-upload-urls

Conversation

@MarcosSpessatto

Copy link
Copy Markdown
Contributor

No description provided.

@MarcosSpessatto MarcosSpessatto added this to the 2.1.0 milestone Sep 3, 2019
@MarcosSpessatto MarcosSpessatto changed the title [NEW] Add JWT to uploaded files urls [WIP][NEW] Add JWT to uploaded files urls Sep 3, 2019
@MarcosSpessatto MarcosSpessatto changed the title [WIP][NEW] Add JWT to uploaded files urls [NEW] Add JWT to uploaded files urls Sep 4, 2019
@RocketChat RocketChat deleted a comment from lgtm-com Bot Sep 4, 2019

@renatobecker-zz renatobecker-zz left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 renatobecker-zz left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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';

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...and this code looks to be a good helper method which would return the message object.

Comment thread app/livechat/server/api/v1/message.js Outdated
}

return API.v1.success({ message });
return API.v1.success({ message: FileUpload.addJWTToFileUrlIfNecessary(message) });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread app/livechat/server/hooks/sendToCRM.js Outdated
}

postData.messages.push(msg);
postData.messages.push(message.file ? FileUpload.addJWTToFileUrlIfNecessary(message) : msg);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread app/livechat/server/sendMessageBySMS.js Outdated
return message;
}

FileUpload.addJWTToFileUrlIfNecessary(message);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread app/livechat/server/api/v1/message.js Outdated
return API.v1.success({ message });
return API.v1.success({ message: normalizeMessageAttachments(message) });
} catch (e) {
return API.v1.failure(e.error);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return API.v1.failure(e.error);
return API.v1.failure(e);

Comment thread app/livechat/server/api/v1/message.js Outdated
.map(normalizeMessageAttachments);
return API.v1.success(messages);
} catch (e) {
return API.v1.failure(e.error);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 })

Copy link
Copy Markdown

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.


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 });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the FileUpload.addJWTToFileUrl may return null, you need to check the returned value before moving forward, don't you think?

Thanks.

Comment thread app/livechat/server/api/v1/message.js Outdated
}

return API.v1.success({ message });
return API.v1.success({ message: normalizeMessageAttachments(message) });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind of checking the message.file before returning? I think we should always return the message instead of the normalizeMessageAttachments.

Comment thread app/livechat/server/api/v1/message.js Outdated
if (result) {
const message = Messages.findOneById(_id);
return API.v1.success({ message });
return API.v1.success({ message: normalizeMessageAttachments(message) });

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not call the normalizer function inside this block of code?

});

return message;
return normalizeMessageAttachments(message);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind of checking the message.file before returning? I think we should always return the message instead of the normalizeMessageAttachments.

Comment thread app/livechat/server/sendMessageBySMS.js Outdated
SMSService.send(room.sms.from, visitor.phone[0].phoneNumber, message.msg);

return message;
return normalizeMessageAttachments(message);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mind of checking the message.file before returning? I think we should always return the message instead of the normalizeMessageAttachments.

@renatobecker-zz
renatobecker-zz merged commit bb61341 into develop Sep 17, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants