Sha256: 217fe4c9f3bf8c00930a260125f5d52257a633b5b6ac0d59e29c3020f1a2df3c

Contents?: true

Size: 1.89 KB

Versions: 3

Compression:

Stored size: 1.89 KB

Contents

require 'omniauth/oauth'
require 'multi_json'

module OmniAuth
  module Strategies
    #
    # Authenticate to Douban via OAuth and retrieve basic
    # user information.
    # Usage:
    #    use OmniAuth::Strategies::Douban, 'APIKey', 'APIKeySecret'
    class Douban < OmniAuth::Strategies::OAuth
      def initialize(app, consumer_key=nil, consumer_secret=nil, options={}, &block)
        # Although in OAuth spec the :realm parameter is optional,
        # it is required for Douban.
        client_options = {
          :access_token_path => '/service/auth/access_token',
          :authorize_path => '/service/auth/authorize',
          :realm => 'OmniAuth',
          :request_token_path => '/service/auth/request_token',
          :site => 'http://www.douban.com',
        }

        super(app, :douban, consumer_key, consumer_secret, client_options, options, &block)
      end

      def auth_hash
        OmniAuth::Utils.deep_merge(
          super, {
            'uid' => @access_token.params[:douban_user_id],
            'user_info' => user_info,
            'extra' => {
              'user_hash' => user_hash,
            },
          }
        )
      end

      def user_info
        user_hash = self.user_hash
        location = user_hash['location'] ? user_hash['location']['$t'] : nil
        image = user_hash['link'].find{|l| l['@rel'] == 'icon'}['@href']
        douban_url = user_hash['link'].find{|l| l['@rel'] == 'alternate'}['@href']
        {
          'username' => user_hash['db:uid']['$t'],
          'name' => user_hash['title']['$t'],
          'location' => location,
          'image' => image,
          'description' => user_hash['content']['$t'],
          'urls' => {
            'Douban' => douban_url,
          },
        }
      end

      def user_hash
        @user_hash ||= MultiJson.decode(@access_token.get('http://api.douban.com/people/%40me?alt=json').body)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
oa-oauth-0.3.2 lib/omniauth/strategies/oauth/douban.rb
oa-oauth-0.3.0 lib/omniauth/strategies/oauth/douban.rb
oa-oauth-0.3.0.rc3 lib/omniauth/strategies/oauth/douban.rb