bitbucket.rb 1023 字节
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
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
V
Valery Sizov 已提交
15
        raw_info['username']
16 17 18 19 20 21 22 23 24 25 26
      end

      info do
        {
          name: raw_info['display_name'],
          avatar: raw_info['links']['avatar']['href'],
          email: primary_email
        }
      end

      def raw_info
V
Valery Sizov 已提交
27
        @raw_info ||= access_token.get('api/2.0/user').parsed
28 29 30
      end

      def primary_email
S
Stan Hu 已提交
31
        primary = emails.find { |i| i['is_primary'] && i['is_confirmed'] }
32 33 34 35
        primary && primary['email'] || nil
      end

      def emails
V
Valery Sizov 已提交
36
        email_response = access_token.get('api/2.0/user/emails').parsed
37 38 39 40 41
        @emails ||= email_response && email_response['values'] || nil
      end
    end
  end
end