From 7047773f6a1395a9490955247fa5db76ee1d2a87 Mon Sep 17 00:00:00 2001 From: Hudell Date: Wed, 9 May 2018 16:10:03 -0300 Subject: [PATCH 1/2] Improve wordpress OAuth settings --- packages/rocketchat-i18n/i18n/en.i18n.json | 8 +++ packages/rocketchat-wordpress/common.js | 66 +++++++++++++++++----- packages/rocketchat-wordpress/startup.js | 53 +++++++++++++++++ 3 files changed, 114 insertions(+), 13 deletions(-) diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index 373b452e85f0b..a333feab7f38e 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -130,6 +130,14 @@ "Accounts_OAuth_Wordpress_callback_url": "WordPress Callback URL", "Accounts_OAuth_Wordpress_id": "WordPress Id", "Accounts_OAuth_Wordpress_secret": "WordPress Secret", + "Accounts_OAuth_Wordpress_server_type_wordpress_com": "Wordpress.com", + "Accounts_OAuth_Wordpress_server_type_wp_oauth_server": "WP OAuth Server Plugin", + "Accounts_OAuth_Wordpress_server_type_custom": "Custom", + "Accounts_OAuth_Wordpress_identity_path": "Identity Path", + "Accounts_OAuth_Wordpress_identity_token_sent_via": "Identity Token Sent Via", + "Accounts_OAuth_Wordpress_token_path": "Token Path", + "Accounts_OAuth_Wordpress_authorize_path": "Authorize Path", + "Accounts_OAuth_Wordpress_scope": "Scope", "Accounts_PasswordReset": "Password Reset", "Accounts_Registration_AuthenticationServices_Default_Roles": "Default Roles for Authentication Services", "Accounts_Registration_AuthenticationServices_Default_Roles_Description": "Default roles (comma-separated) users will be given when registering through authentication services", diff --git a/packages/rocketchat-wordpress/common.js b/packages/rocketchat-wordpress/common.js index d670bb31ddf05..f173830a1d0b9 100644 --- a/packages/rocketchat-wordpress/common.js +++ b/packages/rocketchat-wordpress/common.js @@ -2,11 +2,8 @@ const config = { serverURL: '', - identityPath: '/rest/v1/me', - identityTokenSentVia: 'header', - authorizePath: '/oauth2/authorize', - tokenPath: '/oauth2/token', - scope: 'auth', + identityPath: '/oauth/me', + addAutopublishFields: { forLoggedInUser: ['services.wordpress'], forOtherUsers: ['services.wordpress.user_login'] @@ -15,20 +12,63 @@ const config = { const WordPress = new CustomOAuth('wordpress', config); +const fillSettings = function() { + if (RocketChat.settings.get('API_Wordpress_URL')) { + config.serverURL = RocketChat.settings.get('API_Wordpress_URL'); + + delete config.identityPath; + delete config.identityTokenSentVia; + delete config.authorizePath; + delete config.tokenPath; + delete config.scope; + + const serverType = RocketChat.settings.get('Accounts_OAuth_Wordpress_server_type'); + switch (serverType) { + case 'custom': + if (RocketChat.settings.get('Accounts_OAuth_Wordpress_identity_path')) { + config.identityPath = RocketChat.settings.get('Accounts_OAuth_Wordpress_identity_path'); + } + + if (RocketChat.settings.get('Accounts_OAuth_Wordpress_identity_token_sent_via')) { + config.identityTokenSentVia = RocketChat.settings.get('Accounts_OAuth_Wordpress_identity_token_sent_via'); + } + + if (RocketChat.settings.get('Accounts_OAuth_Wordpress_token_path')) { + config.tokenPath = RocketChat.settings.get('Accounts_OAuth_Wordpress_token_path'); + } + + if (RocketChat.settings.get('Accounts_OAuth_Wordpress_authorize_path')) { + config.authorizePath = RocketChat.settings.get('Accounts_OAuth_Wordpress_authorize_path'); + } + + if (RocketChat.settings.get('Accounts_OAuth_Wordpress_scope')) { + config.scope = RocketChat.settings.get('Accounts_OAuth_Wordpress_scope'); + } + break; + case 'wordpress-com': + config.identityPath = '/rest/v1/me'; + config.identityTokenSentVia = 'header'; + config.authorizePath = '/oauth2/authorize'; + config.tokenPath = '/oauth2/token'; + config.scope = 'auth'; + break; + default: + config.identityPath = '/oauth/me'; + break; + } + + return WordPress.configure(config); + } +}; + if (Meteor.isServer) { Meteor.startup(function() { - return RocketChat.settings.get('API_Wordpress_URL', function(key, value) { - config.serverURL = value; - return WordPress.configure(config); - }); + return fillSettings(); }); } else { Meteor.startup(function() { return Tracker.autorun(function() { - if (RocketChat.settings.get('API_Wordpress_URL')) { - config.serverURL = RocketChat.settings.get('API_Wordpress_URL'); - return WordPress.configure(config); - } + return fillSettings(); }); }); } diff --git a/packages/rocketchat-wordpress/startup.js b/packages/rocketchat-wordpress/startup.js index cf1bbb3ba12d9..39f520505ec70 100644 --- a/packages/rocketchat-wordpress/startup.js +++ b/packages/rocketchat-wordpress/startup.js @@ -22,6 +22,59 @@ RocketChat.settings.addGroup('OAuth', function() { type: 'string', enableQuery }); + this.add('Accounts_OAuth_Wordpress_server_type', '', { + type: 'select', + enableQuery, + 'public': true, + values: [ + { + key: 'wordpress-com', + i18nLabel: 'Accounts_OAuth_Wordpress_server_type_wordpress_com' + }, + { + key: 'wp-oauth-server', + i18nLabel: 'Accounts_OAuth_Wordpress_server_type_wp_oauth_server' + }, + { + key: 'custom', + i18nLabel: 'Accounts_OAuth_Wordpress_server_type_custom' + } + ] + }); + + const customOAuthQuery = [{ + _id: 'Accounts_OAuth_Wordpress', + value: true + }, { + _id: 'Accounts_OAuth_Wordpress_server_type', + value: 'custom' + }]; + + this.add('Accounts_OAuth_Wordpress_identity_path', '', { + type: 'string', + enableQuery: customOAuthQuery, + 'public': true + }); + this.add('Accounts_OAuth_Wordpress_identity_token_sent_via', '', { + type: 'string', + enableQuery: customOAuthQuery, + 'public': true + }); + this.add('Accounts_OAuth_Wordpress_token_path', '', { + type: 'string', + enableQuery: customOAuthQuery, + 'public': true + }); + this.add('Accounts_OAuth_Wordpress_authorize_path', '', { + type: 'string', + enableQuery: customOAuthQuery, + 'public': true + }); + this.add('Accounts_OAuth_Wordpress_scope', '', { + type: 'string', + enableQuery: customOAuthQuery, + 'public': true + }); return this.add('Accounts_OAuth_Wordpress_callback_url', '_oauth/wordpress', { type: 'relativeUrl', readonly: true, From 5be58e38ca89f931ccd3cb4b4e1af270445c7e8f Mon Sep 17 00:00:00 2001 From: Hudell Date: Thu, 10 May 2018 14:17:48 -0300 Subject: [PATCH 2/2] Fixed code so that changing settings will take effect immediatelly --- packages/rocketchat-wordpress/common.js | 82 ++++++++++++------------- 1 file changed, 40 insertions(+), 42 deletions(-) diff --git a/packages/rocketchat-wordpress/common.js b/packages/rocketchat-wordpress/common.js index f173830a1d0b9..c1e2bd5fa3bea 100644 --- a/packages/rocketchat-wordpress/common.js +++ b/packages/rocketchat-wordpress/common.js @@ -1,4 +1,5 @@ /* globals CustomOAuth */ +import _ from 'underscore'; const config = { serverURL: '', @@ -12,58 +13,55 @@ const config = { const WordPress = new CustomOAuth('wordpress', config); -const fillSettings = function() { - if (RocketChat.settings.get('API_Wordpress_URL')) { - config.serverURL = RocketChat.settings.get('API_Wordpress_URL'); +const fillSettings = _.debounce(() => { + config.serverURL = RocketChat.settings.get('API_Wordpress_URL'); - delete config.identityPath; - delete config.identityTokenSentVia; - delete config.authorizePath; - delete config.tokenPath; - delete config.scope; + delete config.identityPath; + delete config.identityTokenSentVia; + delete config.authorizePath; + delete config.tokenPath; + delete config.scope; - const serverType = RocketChat.settings.get('Accounts_OAuth_Wordpress_server_type'); - switch (serverType) { - case 'custom': - if (RocketChat.settings.get('Accounts_OAuth_Wordpress_identity_path')) { - config.identityPath = RocketChat.settings.get('Accounts_OAuth_Wordpress_identity_path'); - } + const serverType = RocketChat.settings.get('Accounts_OAuth_Wordpress_server_type'); + switch (serverType) { + case 'custom': + if (RocketChat.settings.get('Accounts_OAuth_Wordpress_identity_path')) { + config.identityPath = RocketChat.settings.get('Accounts_OAuth_Wordpress_identity_path'); + } - if (RocketChat.settings.get('Accounts_OAuth_Wordpress_identity_token_sent_via')) { - config.identityTokenSentVia = RocketChat.settings.get('Accounts_OAuth_Wordpress_identity_token_sent_via'); - } + if (RocketChat.settings.get('Accounts_OAuth_Wordpress_identity_token_sent_via')) { + config.identityTokenSentVia = RocketChat.settings.get('Accounts_OAuth_Wordpress_identity_token_sent_via'); + } - if (RocketChat.settings.get('Accounts_OAuth_Wordpress_token_path')) { - config.tokenPath = RocketChat.settings.get('Accounts_OAuth_Wordpress_token_path'); - } + if (RocketChat.settings.get('Accounts_OAuth_Wordpress_token_path')) { + config.tokenPath = RocketChat.settings.get('Accounts_OAuth_Wordpress_token_path'); + } - if (RocketChat.settings.get('Accounts_OAuth_Wordpress_authorize_path')) { - config.authorizePath = RocketChat.settings.get('Accounts_OAuth_Wordpress_authorize_path'); - } + if (RocketChat.settings.get('Accounts_OAuth_Wordpress_authorize_path')) { + config.authorizePath = RocketChat.settings.get('Accounts_OAuth_Wordpress_authorize_path'); + } - if (RocketChat.settings.get('Accounts_OAuth_Wordpress_scope')) { - config.scope = RocketChat.settings.get('Accounts_OAuth_Wordpress_scope'); - } - break; - case 'wordpress-com': - config.identityPath = '/rest/v1/me'; - config.identityTokenSentVia = 'header'; - config.authorizePath = '/oauth2/authorize'; - config.tokenPath = '/oauth2/token'; - config.scope = 'auth'; - break; - default: - config.identityPath = '/oauth/me'; - break; - } - - return WordPress.configure(config); + if (RocketChat.settings.get('Accounts_OAuth_Wordpress_scope')) { + config.scope = RocketChat.settings.get('Accounts_OAuth_Wordpress_scope'); + } + break; + case 'wordpress-com': + config.identityPath = '/rest/v1/me'; + config.identityTokenSentVia = 'header'; + config.authorizePath = '/oauth2/authorize'; + config.tokenPath = '/oauth2/token'; + config.scope = 'auth'; + break; + default: + config.identityPath = '/oauth/me'; + break; } -}; + return WordPress.configure(config); +}, 1000); if (Meteor.isServer) { Meteor.startup(function() { - return fillSettings(); + return RocketChat.settings.get(/(API\_Wordpress\_URL)?(Accounts\_OAuth\_Wordpress\_)?/, () => fillSettings()); }); } else { Meteor.startup(function() {