Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit a80670506e8fad950a1262e5fd5500ac76402a9d @ballantyne committed Oct 29, 2011
Showing with 118 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +4 −0 Gemfile
  3. +1 −0 Rakefile
  4. +2 −0 lib/omniauth-tqq.rb
  5. +5 −0 lib/omniauth-tqq/version.rb
  6. +78 −0 lib/omniauth/strategies/tqq.rb
  7. +24 −0 omniauth-tqq.gemspec
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in omniauth-tqq.gemspec
+gemspec
@@ -0,0 +1 @@
+require "bundler/gem_tasks"
@@ -0,0 +1,2 @@
+require "omniauth-tqq/version"
+require 'omniauth/strategies/tqq'
@@ -0,0 +1,5 @@
+module Omniauth
+ module Tqq
+ VERSION = "1.0.0.rc2"
+ end
+end
@@ -0,0 +1,78 @@
+require 'omniauth-oauth'
+require 'multi_json'
+
+module OmniAuth
+ module Strategies
+ class Weibo < OmniAuth::Strategies::OAuth
+ option :name, 'tqq'
+ option :sign_in, true
+ def initialize(*args)
+ super
+ # taken from https://github.com/intridea/omniauth/blob/0-3-stable/oa-oauth/lib/omniauth/strategies/oauth/tqq.rb#L15-24
+ options.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',
+ }
+ end
+
+ # https://github.com/intridea/omniauth/blob/0-3-stable/oa-oauth/lib/omniauth/strategies/oauth/tqq.rb#L28-30
+ def nonce
+ Base64.encode64(OpenSSL::Random.random_bytes(32)).gsub(/\W/, '')[0, 32]
+ end
+
+ def consumer
+ consumer = ::OAuth::Consumer.new(options.consumer_key, options.consumer_secret, options.client_options)
+ consumer
+ end
+
+ uid { access_token.params[:id] }
+
+ info do
+ {
+ :nickname => raw_info['data']['nick'],
+ :name => raw_info['data']['name'],
+ :location => raw_info['data']['location'],
+ :image => raw_info['data']['head'],
+ :description => raw_info['description'],
+ :urls => {
+ 'Tqq' => 't.qq.com'
+ }
+ }
+ end
+
+ extra do
+ { :raw_info => raw_info }
+ end
+
+ #taken from https://github.com/intridea/omniauth/blob/0-3-stable/oa-oauth/lib/omniauth/strategies/oauth/tsina.rb#L52-67
+ def request_phase
+ request_token = consumer.get_request_token(:oauth_callback => callback_url)
+ session['oauth'] ||= {}
+ session['oauth'][name.to_s] = {'callback_confirmed' => true, 'request_token' => request_token.token, 'request_secret' => request_token.secret}
+
+ if request_token.callback_confirmed?
+ redirect request_token.authorize_url(options[:authorize_params])
+ else
+ redirect request_token.authorize_url(options[:authorize_params].merge(:oauth_callback => callback_url))
+ end
+
+ rescue ::Timeout::Error => e
+ fail!(:timeout, e)
+ rescue ::Net::HTTPFatalError, ::OpenSSL::SSL::SSLError => e
+ fail!(:service_unavailable, e)
+ end
+
+ def raw_info
+ @raw_info ||= MultiJson.decode(access_token.get('http://open.t.qq.com/api/user/info?format=json').body)
+ rescue ::Errno::ETIMEDOUT
+ raise ::Timeout::Error
+ end
+ end
+ end
+end
@@ -0,0 +1,24 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "omniauth-tqq/version"
+
+Gem::Specification.new do |s|
+ s.name = "omniauth-tqq"
+ s.version = Omniauth::Tqq::VERSION
+ s.authors = ["Scott Ballantyne"]
+ s.email = ["ussballantyne@gmail.com"]
+ s.homepage = ""
+ s.summary = %q{TODO: Write a gem summary}
+ s.description = %q{TODO: Write a gem description}
+
+ s.rubyforge_project = "omniauth-tqq"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ # specify any dependencies here; for example:
+ # s.add_development_dependency "rspec"
+ # s.add_runtime_dependency "rest-client"
+end

0 comments on commit a806705

Please sign in to comment.