Add custom OmniAuth strategy for Bitbucket OAuth2

This commit is contained in:
Douglas Barbosa Alexandre 2016-08-22 15:36:41 -03:00 committed by Stan Hu
parent 69aaa97203
commit a2ef52b32b
2 changed files with 51 additions and 0 deletions

View File

@ -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

View File

@ -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