Sha256: ea4dd1629efedfe5f5b30d4001e08a05c6942249d42f2eaf91346940cf44c116

Contents?: true

Size: 1.89 KB

Versions: 14

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 = {
          :site               => 'http://www.douban.com',
          :request_token_path => '/service/auth/request_token',
          :access_token_path  => '/service/auth/access_token',
          :authorize_path     => '/service/auth/authorize',
          :realm              => 'OmniAuth'
        }

        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

14 entries across 14 versions & 3 rubygems

Version Path
omniauth-0.2.6 oa-oauth/lib/omniauth/strategies/douban.rb
oa-oauth-0.2.6 lib/omniauth/strategies/douban.rb
omniauth-0.2.5 oa-oauth/lib/omniauth/strategies/douban.rb
oa-oauth-0.2.5 lib/omniauth/strategies/douban.rb
omniauth_china-0.1.1 lib/omniauth_china/strategies/douban.rb
omniauth_china-0.1.0 lib/omniauth_china/strategies/douban.rb
omniauth_china-0.0.7 lib/omniauth_china/strategies/douban.rb
omniauth_china-0.0.6 lib/omniauth_china/strategies/douban.rb
omniauth_china-0.0.5 lib/omniauth_china/strategies/douban.rb
omniauth_china-0.0.4.beta1 lib/omniauth_china/strategies/douban.rb
omniauth_china-0.0.4 lib/omniauth_china/strategies/douban.rb
omniauth_china-0.0.3 lib/omniauth_china/strategies/douban.rb
omniauth_china-0.0.2 lib/omniauth_china/strategies/douban.rb
omniauth_china-0.0.1a lib/omniauth_china/strategies/douban.rb