Skip to content

Commit

Permalink
Add option on custom oauth to send access token in headers or in payl…
Browse files Browse the repository at this point in the history
…oad (#2818)
  • Loading branch information
marceloschmidt authored and engelgabriel committed Apr 8, 2016
1 parent 9d61b4a commit 6b79286
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 16 deletions.
17 changes: 12 additions & 5 deletions packages/rocketchat-custom-oauth/custom_oauth_server.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class CustomOAuth
@serverURL = options.serverURL
@tokenPath = options.tokenPath
@identityPath = options.identityPath
@tokenSentVia = options.tokenSentVia

if not /^https?:\/\/.+/.test @tokenPath
@tokenPath = @serverURL + @tokenPath
Expand Down Expand Up @@ -75,13 +76,19 @@ class CustomOAuth
return response.data.access_token

getIdentity: (accessToken) ->
params = {}
headers =
'User-Agent': @userAgent # http://doc.gitlab.com/ce/api/users.html#Current-user

if @accessTokenSentVia is 'header'
headers['Authorization'] = 'Bearer ' + accessToken
else
params['access_token'] = accessToken

try
response = HTTP.get @identityPath,
headers:
'User-Agent': @userAgent # http://doc.gitlab.com/ce/api/users.html#Current-user
'Authorization': 'Bearer ' + accessToken
params:
access_token: accessToken
headers: headers
params: params

if response.data
return response.data
Expand Down
3 changes: 3 additions & 0 deletions packages/rocketchat-lib/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"Accounts_iframe_url" : "Iframe Url",
"Accounts_LoginExpiration" : "Login Expiration in Days",
"Accounts_ManuallyApproveNewUsers" : "Manually Approve New Users",
"Accounts_OAuth_Custom_Token_Sent_Via" : "Token Sent Via",
"Accounts_OAuth_Custom_Authorize_Path" : "Authorize Path",
"Accounts_OAuth_Custom_Button_Color" : "Button Color",
"Accounts_OAuth_Custom_Button_Label_Color" : "Button Text Color",
Expand Down Expand Up @@ -338,6 +339,7 @@
"GoogleTagManager_id" : "Google Tag Manager Id",
"Has_more" : "Has more",
"Have_your_own_chat" : "Have your own web chat. Developed with Meteor.com, the Rocket.Chat is a great solution for developers looking forward to build and evolve their own chat platform.",
"Header" : "Header",
"Hidden" : "Hidden",
"Hide_Group_Warning" : "Are you sure you want to hide the group \"%s\"?",
"Hide_Private_Warning" : "Are you sure you want to hide the discussion with \"%s\"?",
Expand Down Expand Up @@ -646,6 +648,7 @@
"Password" : "Password",
"Password_Change_Disabled" : "Your Rocket.Chat administrator has disabled the changing of passwords",
"Password_changed_successfully" : "Password changed successfully",
"Payload" : "Payload",
"People" : "People",
"Permissions" : "Permissions",
"Pin_Message" : "Pin Message",
Expand Down
23 changes: 12 additions & 11 deletions packages/rocketchat-lib/server/methods/addOAuthService.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ Meteor.methods

name = name.toLowerCase().replace(/[^a-z0-9]/g, '')
name = s.capitalize(name)
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}" , false , { type: 'boolean', group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_Enable', persistent: true }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_url" , '' , { type: 'string' , group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_URL', persistent: true }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_token_path" , '/oauth/token' , { type: 'string' , group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_Token_Path', persistent: true }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_identity_path" , '/me' , { type: 'string' , group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_Identity_Path', persistent: true }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_authorize_path" , '/oauth/authorize', { type: 'string' , group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_Authorize_Path', persistent: true }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_id" , '' , { type: 'string' , group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_id', persistent: true }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_secret" , '' , { type: 'string' , group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_Secret', persistent: true }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_login_style" , 'popup' , { type: 'select' , group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_Login_Style', persistent: true, values: [ { key: 'redirect', i18nLabel: 'Redirect' }, { key: 'popup', i18nLabel: 'Popup' }, { key: '', i18nLabel: 'Default' } ] }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_button_label_text" , '' , { type: 'string' , group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_Button_Label_Text', persistent: true }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_button_label_color", '#FFFFFF' , { type: 'string' , group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_Button_Label_Color', persistent: true }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_button_color" , '#13679A' , { type: 'string' , group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_Button_Color', persistent: true }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}" , false , { type: 'boolean', group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_Enable', persistent: true }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_url" , '' , { type: 'string' , group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_URL', persistent: true }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_token_path" , '/oauth/token' , { type: 'string' , group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_Token_Path', persistent: true }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_identity_path" , '/me' , { type: 'string' , group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_Identity_Path', persistent: true }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_authorize_path" , '/oauth/authorize', { type: 'string' , group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_Authorize_Path', persistent: true }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_token_sent_via" , 'payload' , { type: 'select' , group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_Token_Sent_Via', persistent: true, values: [ { key: 'header', i18nLabel: 'Header' }, { key: 'payload', i18nLabel: 'Payload' } ] }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_id" , '' , { type: 'string' , group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_id', persistent: true }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_secret" , '' , { type: 'string' , group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_Secret', persistent: true }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_login_style" , 'popup' , { type: 'select' , group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_Login_Style', persistent: true, values: [ { key: 'redirect', i18nLabel: 'Redirect' }, { key: 'popup', i18nLabel: 'Popup' }, { key: '', i18nLabel: 'Default' } ] }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_button_label_text" , '' , { type: 'string' , group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_Button_Label_Text', persistent: true }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_button_label_color" , '#FFFFFF' , { type: 'string' , group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_Button_Label_Color', persistent: true }
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_button_color" , '#13679A' , { type: 'string' , group: 'OAuth', section: "Custom OAuth: #{name}", i18nLabel: 'Accounts_OAuth_Custom_Button_Color', persistent: true }
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Meteor.methods
RocketChat.settings.removeById "Accounts_OAuth_Custom_#{name}_token_path"
RocketChat.settings.removeById "Accounts_OAuth_Custom_#{name}_identity_path"
RocketChat.settings.removeById "Accounts_OAuth_Custom_#{name}_authorize_path"
RocketChat.settings.removeById "Accounts_OAuth_Custom_#{name}_token_sent_via"
RocketChat.settings.removeById "Accounts_OAuth_Custom_#{name}_id"
RocketChat.settings.removeById "Accounts_OAuth_Custom_#{name}_secret"
RocketChat.settings.removeById "Accounts_OAuth_Custom_#{name}_button_label_text"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,14 @@ OAuthServicesUpdate = ->
data.buttonLabelColor = RocketChat.models.Settings.findOneById("#{service._id}_button_label_color")?.value
data.loginStyle = RocketChat.models.Settings.findOneById("#{service._id}_login_style")?.value
data.buttonColor = RocketChat.models.Settings.findOneById("#{service._id}_button_color")?.value
data.tokenSentVia = RocketChat.models.Settings.findOneById("#{service._id}_token_sent_via")?.value
new CustomOAuth serviceName.toLowerCase(),
serverURL: data.serverURL
tokenPath: data.tokenPath
identityPath: data.identityPath
authorizePath: data.authorizePath
loginStyle: data.loginStyle
tokenSentVia: data.tokenSentVia

if serviceName is 'Facebook'
data.appId = data.clientId
Expand Down
39 changes: 39 additions & 0 deletions server/startup/migrations/v40.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
RocketChat.Migrations.add({
version: 40,
up: function() {
RocketChat.models.Settings.find({ _id: /Accounts_OAuth_Custom_/, i18nLabel: 'Accounts_OAuth_Custom_Enable' }).forEach(function(customOauth) {
var parts = customOauth._id.split('_');
var name = parts[3];
var id = 'Accounts_OAuth_Custom_' + name + '_token_sent_via';
if (!RocketChat.models.Settings.findOne( { _id: id } )) {
RocketChat.models.Settings.insert({
'_id': id,
'type': 'select',
'group': 'OAuth',
'section': 'Custom OAuth: ' + name,
'i18nLabel': 'Accounts_OAuth_Custom_Token_Sent_Via',
'persistent': true,
'values': [
{
'key': 'header',
'i18nLabel': 'Header'
},
{
'key': 'payload',
'i18nLabel': 'Payload'
}
],
'packageValue': 'payload',
'valueSource': 'packageValue',
'ts': new Date(),
'hidden': false,
'blocked': false,
'sorter': 255,
'i18nDescription': 'Accounts_OAuth_Custom_' + name + '_token_sent_via_Description',
'createdAt': new Date(),
'value': 'payload'
});
}
});
}
});

0 comments on commit 6b79286

Please sign in to comment.