summaryrefslogtreecommitdiff
path: root/lib/omniauth/strategies/bitbucket.rb
blob: 5a7d67c23903e3efb6163f7abd859e8e414d1678 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
        raw_info['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('api/2.0/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('api/2.0/user/emails').parsed
        @emails ||= email_response && email_response['values'] || nil
      end
    end
  end
end