2016-08-22 14:36:41 -04:00
|
|
|
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'
|
|
|
|
}
|
|
|
|
|
|
|
|
uid do
|
2016-12-06 11:26:24 -05:00
|
|
|
raw_info['username']
|
2016-08-22 14:36:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
info do
|
|
|
|
{
|
|
|
|
name: raw_info['display_name'],
|
|
|
|
avatar: raw_info['links']['avatar']['href'],
|
|
|
|
email: primary_email
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def raw_info
|
2016-12-14 04:53:29 -05:00
|
|
|
@raw_info ||= access_token.get('api/2.0/user').parsed
|
2016-08-22 14:36:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def primary_email
|
2016-11-11 19:48:04 -05:00
|
|
|
primary = emails.find { |i| i['is_primary'] && i['is_confirmed'] }
|
2016-08-22 14:36:41 -04:00
|
|
|
primary && primary['email'] || nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def emails
|
2016-12-14 04:53:29 -05:00
|
|
|
email_response = access_token.get('api/2.0/user/emails').parsed
|
2016-08-22 14:36:41 -04:00
|
|
|
@emails ||= email_response && email_response['values'] || nil
|
|
|
|
end
|
2017-10-26 06:59:28 -04:00
|
|
|
|
|
|
|
def callback_url
|
|
|
|
options[:redirect_uri] || (full_host + script_name + callback_path)
|
|
|
|
end
|
2016-08-22 14:36:41 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|