Sha256: 850f3bc21102adbde37a941ec4aa6993fa4092280e68f49059974dff4bd306b9

Contents?: true

Size: 1.79 KB

Versions: 3

Compression:

Stored size: 1.79 KB

Contents

require 'omniauth/oauth'
require 'multi_json'

module OmniAuth
  module Strategies
    # Authenticate to Tqq via OAuth and retrieve basic
    # user information.
    #
    # Usage:
    #    use OmniAuth::Strategies::Tqq, 'APIKey', 'APIKeySecret'
    class Tqq < OmniAuth::Strategies::OAuth

      def initialize(app, consumer_key=nil, consumer_secret=nil, options={}, &block)
        @api_key = consumer_key
        client_options = {
          :access_token_path => '/cgi-bin/access_token',
          :authorize_path => '/cgi-bin/authorize',
          :http_method => :get,
          :nonce => nonce,
          :realm => 'OmniAuth',
          :request_token_path => '/cgi-bin/request_token',
          :scheme => :query_string,
          :site => 'https://open.t.qq.com',
        }
        super(app, :tqq, consumer_key, consumer_secret, client_options, options, &block)
      end

      def nonce
        Base64.encode64(OpenSSL::Random.random_bytes(32)).gsub(/\W/, '')[0, 32]
      end

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

      def user_info
        user_hash = self.user_hash
        {
          'username' => user_hash['data']['name'],
          'name' => user_hash['data']['nick'],
          'location' => user_hash['data']['location'],
          'image' => user_hash['data']['head'],
          'description' => user_hash['description'],
          'urls' => {
            'Tqq' => 't.qq.com',
          },
        }
      end

      def user_hash
        @user_hash ||= MultiJson.decode(@access_token.get('http://open.t.qq.com/api/user/info?format=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/tqq.rb
oa-oauth-0.3.0 lib/omniauth/strategies/oauth/tqq.rb
oa-oauth-0.3.0.rc3 lib/omniauth/strategies/oauth/tqq.rb