summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouglas Barbosa Alexandre <dbalexandre@gmail.com>2016-08-22 15:36:41 -0300
committerStan Hu <stanhu@gmail.com>2016-11-21 16:47:24 -0800
commita2ef52b32bd3d1ee70ea333566e330c44e6c9170 (patch)
tree469a4e1ed516c36d31a4bf32df1f719ad8e63475
parent69aaa972038c7b285c06804e04ff5932e468e523 (diff)
downloadgitlab-ce-a2ef52b32bd3d1ee70ea333566e330c44e6c9170.tar.gz
Add custom OmniAuth strategy for Bitbucket OAuth2
-rw-r--r--config/initializers/omniauth.rb6
-rw-r--r--lib/omniauth/strategies/bitbucket.rb45
2 files changed, 51 insertions, 0 deletions
diff --git a/config/initializers/omniauth.rb b/config/initializers/omniauth.rb
index 26c30e523a7..ab5a0561b8c 100644
--- a/config/initializers/omniauth.rb
+++ b/config/initializers/omniauth.rb
@@ -26,3 +26,9 @@ if Gitlab.config.omniauth.enabled
end
end
end
+
+module OmniAuth
+ module Strategies
+ autoload :Bitbucket, Rails.root.join('lib', 'omniauth', 'strategies', 'bitbucket')
+ end
+end
diff --git a/lib/omniauth/strategies/bitbucket.rb b/lib/omniauth/strategies/bitbucket.rb
new file mode 100644
index 00000000000..2006e7582ce
--- /dev/null
+++ b/lib/omniauth/strategies/bitbucket.rb
@@ -0,0 +1,45 @@
+require 'omniauth-oauth2'
+
+module OmniAuth
+ module Strategies
+ class Bitbucket < OmniAuth::Strategies::OAuth2
+ option :name, 'bitbucket'
+
+ option :client_options, {
+ site: 'https://bitbucket.org',
+ authorize_url: 'https://bitbucket.org/site/oauth2/authorize',
+ token_url: 'https://bitbucket.org/site/oauth2/access_token'
+ }
+
+ def callback_url
+ full_host + script_name + callback_path
+ end
+
+ uid do
+ raw['username']
+ end
+
+ info do
+ {
+ name: raw_info['display_name'],
+ avatar: raw_info['links']['avatar']['href'],
+ email: primary_email
+ }
+ end
+
+ def raw_info
+ @raw_info ||= access_token.get('user').parsed
+ end
+
+ def primary_email
+ primary = emails.find{ |i| i['is_primary'] && i['is_confirmed'] }
+ primary && primary['email'] || nil
+ end
+
+ def emails
+ email_response = access_token.get('user/emails').parsed
+ @emails ||= email_response && email_response['values'] || nil
+ end
+ end
+ end
+end