Skip to content
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

[FIX] Enable CORS for Restivus #8671

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 28 additions & 14 deletions packages/rocketchat-api/server/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class API extends Restivus {
this.authMethods.push(method);
}

success(result={}) {
success(result = {}) {
if (_.isObject(result)) {
result.success = true;
}
Expand Down Expand Up @@ -113,7 +113,7 @@ class API extends Restivus {
if (this.helperMethods) {
Object.keys(endpoints).forEach((method) => {
if (typeof endpoints[method] === 'function') {
endpoints[method] = { action: endpoints[method] };
endpoints[method] = {action: endpoints[method]};
}

//Add a try/catch for each endpoint
Expand Down Expand Up @@ -180,17 +180,31 @@ const getUserAuth = function _getUserAuth() {
};
};

RocketChat.API.v1 = new API({
version: 'v1',
useDefaultAuth: true,
prettyJson: true,
enableCors: false,
auth: getUserAuth()
});
const createApi = function(enableCors) {
if (!RocketChat.API.v1 || RocketChat.API.v1._config.enableCors !== enableCors) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't like accessing a "private" property for deciding about the buffering, particularly in an untyped world, but I preferred this to buffering the property in some other place.

RocketChat.API.v1 = new API({
version: 'v1',
useDefaultAuth: true,
prettyJson: true,
enableCors,
auth: getUserAuth()
});
}

if (!RocketChat.API.default || RocketChat.API.default._config.enableCors !== enableCors) {
RocketChat.API.default = new API({
useDefaultAuth: true,
prettyJson: true,
enableCors,
auth: getUserAuth()
});
}
};

RocketChat.API.default = new API({
useDefaultAuth: true,
prettyJson: true,
enableCors: false,
auth: getUserAuth()
// register the API to be re-created once the CORS-setting changes.
RocketChat.settings.get('API_Enable_CORS', (key, value) => {
createApi(value);
});

// also create the API immediately
createApi(!!RocketChat.settings.get('API_Enable_CORS'));