Skip to content
Snippets Groups Projects
Commit d7083521 authored by Rodrigo Nascimento's avatar Rodrigo Nascimento
Browse files

Allow to configure authorize path for oAuth and allow pass complete URL in paths

parent 9a679d61
No related merge requests found
......@@ -3,6 +3,7 @@ Meteor.startup ->
added: (record) ->
new CustomOAuth record.service,
serverURL: record.serverURL
authorizePath: record.authorizePath
Template.loginServices.helpers
loginService: ->
......
......@@ -27,6 +27,7 @@
"Accounts_OAuth_Custom_URL": "URL",
"Accounts_OAuth_Custom_Token_Path": "Token Path",
"Accounts_OAuth_Custom_Identity_Path": "Identity Path",
"Accounts_OAuth_Custom_Authorize_Path": "Authorize Path",
"Accounts_OAuth_Custom_Secret": "Secret",
"Accounts_OAuth_Custom_Enable": "Enable",
"Accounts_OAuth_Custom_Button_Label_Text": "Button Text",
......
......@@ -4,18 +4,26 @@
# completion. Takes one argument, credentialToken on success, or Error on
# error.
class CustomOAuth
constructor: (@name, @options) ->
constructor: (@name, options) ->
if not Match.test @name, String
return throw new Meteor.Error 'CustomOAuth: Name is required and must be String'
if not Match.test @options, Object
if not Match.test options, Object
return throw new Meteor.Error 'CustomOAuth: Options is required and must be Object'
if not Match.test @options.serverURL, String
if not Match.test options.serverURL, String
return throw new Meteor.Error 'CustomOAuth: Options.serverURL is required and must be String'
if not Match.test options.authorizePath, String
options.authorizePath = '/oauth/authorize'
@serverURL = options.serverURL
if not /^https?:\/\/.+/.test options.authorizePath
options.authorizePath = @serverURL + options.authorizePath
@authorizePath = options.authorizePath
Accounts.oauth.registerService @name
@configureLogin()
......@@ -47,7 +55,7 @@ class CustomOAuth
credentialToken = Random.secret()
loginStyle = OAuth._loginStyle @name, config, options
loginUrl = @serverURL + '/oauth/authorize' +
loginUrl = @authorizePath +
'?client_id=' + config.clientId +
'&redirect_uri=' + OAuth._redirectUri(@name, config) +
'&response_type=code' +
......
......@@ -34,6 +34,13 @@ class CustomOAuth
options.identityPath = '/me'
@serverURL = options.serverURL
if not /^https?:\/\/.+/.test options.tokenPath
options.tokenPath = @serverURL + options.tokenPath
if not /^https?:\/\/.+/.test options.identityPath
options.identityPath = @serverURL + options.identityPath
@tokenPath = options.tokenPath
@identityPath = options.identityPath
......@@ -47,7 +54,7 @@ class CustomOAuth
response = undefined
try
response = HTTP.post @serverURL + @tokenPath,
response = HTTP.post @tokenPath,
headers:
Accept: 'application/json'
'User-Agent': @userAgent
......@@ -60,17 +67,17 @@ class CustomOAuth
state: query.state
catch err
error = new Error("Failed to complete OAuth handshake with #{@name} at #{@serverURL + @tokenPath}. " + err.message)
error = new Error("Failed to complete OAuth handshake with #{@name} at #{@tokenPath}. " + err.message)
throw _.extend error, {response: err.response}
if response.data.error #if the http response was a json object with an error attribute
throw new Error("Failed to complete OAuth handshake with #{@name} at #{@serverURL + @tokenPath}. " + response.data.error)
throw new Error("Failed to complete OAuth handshake with #{@name} at #{@tokenPath}. " + response.data.error)
else
return response.data.access_token
getIdentity: (accessToken) ->
try
response = HTTP.get @serverURL + @identityPath,
response = HTTP.get @identityPath,
headers:
'User-Agent': @userAgent # http://doc.gitlab.com/ce/api/users.html#Current-user
params:
......@@ -79,7 +86,7 @@ class CustomOAuth
return response.data
catch err
error = new Error("Failed to fetch identity from #{@name} at #{@serverURL + @identityPath}. " + err.message)
error = new Error("Failed to fetch identity from #{@name} at #{@identityPath}. " + err.message)
throw _.extend error, {response: err.response}
registerService: ->
......
......@@ -9,12 +9,13 @@ Meteor.methods
throw new Meteor.Error 'not-authorized', '[methods] addOAuthService -> Not authorized'
name = s.capitalize(name)
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}" , false , { type: 'boolean', group: 'Accounts', section: name, i18nLabel: 'Accounts_OAuth_Custom_Enable'}
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_url" , '' , { type: 'string' , group: 'Accounts', section: name, i18nLabel: 'Accounts_OAuth_Custom_URL'}
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_token_path" , '/oauth/token' , { type: 'string' , group: 'Accounts', section: name, i18nLabel: 'Accounts_OAuth_Custom_Token_Path'}
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_identity_path" , '/me' , { type: 'string' , group: 'Accounts', section: name, i18nLabel: 'Accounts_OAuth_Custom_Identity_Path'}
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_id" , '' , { type: 'string' , group: 'Accounts', section: name, i18nLabel: 'Accounts_OAuth_Custom_ID'}
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_secret" , '' , { type: 'string' , group: 'Accounts', section: name, i18nLabel: 'Accounts_OAuth_Custom_Secret'}
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_button_label_text" , '' , { type: 'string' , group: 'Accounts', section: name, i18nLabel: 'Accounts_OAuth_Custom_Button_Label_Text'}
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_button_label_color", '#FFFFFF' , { type: 'string' , group: 'Accounts', section: name, i18nLabel: 'Accounts_OAuth_Custom_Button_Label_Color'}
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_button_color" , '#13679A' , { type: 'string' , group: 'Accounts', section: name, i18nLabel: 'Accounts_OAuth_Custom_Button_Color'}
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}" , false , { type: 'boolean', group: 'Accounts', section: name, i18nLabel: 'Accounts_OAuth_Custom_Enable'}
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_url" , '' , { type: 'string' , group: 'Accounts', section: name, i18nLabel: 'Accounts_OAuth_Custom_URL'}
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_token_path" , '/oauth/token' , { type: 'string' , group: 'Accounts', section: name, i18nLabel: 'Accounts_OAuth_Custom_Token_Path'}
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_identity_path" , '/me' , { type: 'string' , group: 'Accounts', section: name, i18nLabel: 'Accounts_OAuth_Custom_Identity_Path'}
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_authorize_path" , '/oauth/authorize', { type: 'string' , group: 'Accounts', section: name, i18nLabel: 'Accounts_OAuth_Custom_Authorize_Path'}
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_id" , '' , { type: 'string' , group: 'Accounts', section: name, i18nLabel: 'Accounts_OAuth_Custom_ID'}
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_secret" , '' , { type: 'string' , group: 'Accounts', section: name, i18nLabel: 'Accounts_OAuth_Custom_Secret'}
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_button_label_text" , '' , { type: 'string' , group: 'Accounts', section: name, i18nLabel: 'Accounts_OAuth_Custom_Button_Label_Text'}
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_button_label_color", '#FFFFFF' , { type: 'string' , group: 'Accounts', section: name, i18nLabel: 'Accounts_OAuth_Custom_Button_Label_Color'}
RocketChat.settings.add "Accounts_OAuth_Custom_#{name}_button_color" , '#13679A' , { type: 'string' , group: 'Accounts', section: name, i18nLabel: 'Accounts_OAuth_Custom_Button_Color'}
......@@ -24,6 +24,7 @@ updateServices = ->
data.serverURL = Settings.findOne({_id: "#{service._id}_url"})?.value
data.tokenPath = Settings.findOne({_id: "#{service._id}_token_path"})?.value
data.identityPath = Settings.findOne({_id: "#{service._id}_identity_path"})?.value
data.authorizePath = Settings.findOne({_id: "#{service._id}_authorize_path"})?.value
data.buttonLabelText = Settings.findOne({_id: "#{service._id}_button_label_text"})?.value
data.buttonLabelColor = Settings.findOne({_id: "#{service._id}_button_label_color"})?.value
data.buttonColor = Settings.findOne({_id: "#{service._id}_button_color"})?.value
......@@ -31,6 +32,7 @@ updateServices = ->
serverURL: data.serverURL
tokenPath: data.tokenPath
identityPath: data.identityPath
authorizePath: data.authorizePath
if serviceName is 'Facebook'
data.appId = data.clientId
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment