Permalink
Browse files

first implementation

  • Loading branch information...
1 parent 3e6d5e8 commit bfcd25a35b0b48b772f4bcbbfd3409a7dc4699ae @cqpx committed Jul 11, 2012
Showing with 173 additions and 3 deletions.
  1. +4 −0 Gemfile
  2. +22 −0 LICENSE
  3. +28 −3 README.md
  4. +2 −0 Rakefile
  5. +2 −0 lib/omniauth-tqq-oauth2.rb
  6. +7 −0 lib/omniauth-tqq-oauth2/version.rb
  7. +88 −0 lib/omniauth/strategies/tqq.rb
  8. +20 −0 omniauth-tqq-oauth2.gemspec
View
@@ -0,0 +1,4 @@
+source 'https://rubygems.org'
+
+# Specify your gem's dependencies in omniauth-tqq-oauth2.gemspec
+gemspec
View
@@ -0,0 +1,22 @@
+Copyright (c) 2012 cqpx
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -1,4 +1,29 @@
-omniauth-tqq-oauth2
-===================
+# Omniauth::Tqq::Oauth2
-omniauth-tqq-oauth2
+TODO: Write a gem description
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'omniauth-tqq-oauth2'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install omniauth-tqq-oauth2
+
+## Usage
+
+TODO: Write usage instructions here
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Added some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
View
@@ -0,0 +1,2 @@
+#!/usr/bin/env rake
+require "bundler/gem_tasks"
@@ -0,0 +1,2 @@
+require "omniauth-tqq-oauth2/version"
+require 'omniauth/strategies/tqq'
@@ -0,0 +1,7 @@
+module Omniauth
+ module Tqq
+ module Oauth2
+ VERSION = "0.0.1"
+ end
+ end
+end
@@ -0,0 +1,88 @@
+require 'omniauth-oauth2'
+
+module OmniAuth
+ module Strategies
+ class Tqq < OmniAuth::Strategies::OAuth2
+ # Give your strategy a name.
+ option :name, "tqq"
+
+ # This is where you pass the options you would pass when
+ # initializing your consumer from the OAuth gem.
+ option :client_options, {
+ :site => "https://open.t.qq.com",
+ :authorize_url => "/cgi-bin/oauth2/authorize",
+ :token_url => "/cgi-bin/oauth2/access_token"
+ }
+ option :token_params, {
+ :parse => :query
+ }
+
+ # These are called after authentication has succeeded. If
+ # possible, you should try to set the UID without making
+ # additional calls (if the user id is returned with the token
+ # or as a URI parameter). This may not be possible with all
+ # providers.
+ uid {
+ access_token["openid"]
+ }
+
+ info do
+ {
+ :name => raw_info['name'],
+ :email => raw_info['email']
+ }
+ end
+
+ extra do
+ {
+ 'raw_info' => raw_info
+ }
+ end
+
+ credentials do
+ hash = {'token' => access_token.token}
+ hash.merge!('openid' => @openid) if @openid
+ hash.merge!('openkey' => @openkey) if @openkey
+ hash.merge!('refresh_token' => access_token.refresh_token) if access_token.expires? && access_token.refresh_token
+ hash.merge!('expires_at' => access_token.expires_at) if access_token.expires?
+ hash.merge!('expires' => access_token.expires?)
+ hash
+ end
+
+ def build_access_token
+ verifier = request.params['code']
+ @ac_token ||= client.auth_code.get_token(verifier, {:redirect_uri => callback_url}.merge(token_params.to_hash(:symbolize_keys => true)))
+ end
+
+ def callback_phase
+ if request.params['error'] || request.params['error_reason']
+ raise CallbackError.new(request.params['error'], request.params['error_description'] || request.params['error_reason'], request.params['error_uri'])
+ end
+
+ self.access_token = build_access_token
+ self.access_token = access_token.refresh! if access_token.expired?
+ @openid = request.params["openid"]
+ @openkey = request.params["openkey"]
+
+ super
+ rescue ::OAuth2::Error, CallbackError => e
+ fail!(:invalid_credentials, e)
+ rescue ::MultiJson::DecodeError => e
+ fail!(:invalid_response, e)
+ rescue ::Timeout::Error, ::Errno::ETIMEDOUT => e
+ fail!(:timeout, e)
+ rescue ::SocketError => e
+ fail!(:failed_to_connect, e)
+ end
+
+ def raw_info
+ @raw_info ||= access_token.get("/api/user/info", params: {
+ openid: @openid,
+ oauth_consumer_key: access_token.client.id,
+ access_token: access_token.token,
+ oauth_version: '2.a'
+ }, parse: :json).parsed
+ end
+ end
+ end
+end
@@ -0,0 +1,20 @@
+# -*- encoding: utf-8 -*-
+require File.expand_path('../lib/omniauth-tqq-oauth2/version', __FILE__)
+
+Gem::Specification.new do |gem|
+ gem.authors = ["cqpx"]
+ gem.email = ["cqpanxu@gmail.com"]
+ gem.description = %q{OmniAuth Oauth2 strategy for t.qq.com.}
+ gem.summary = %q{OmniAuth Oauth2 strategy for t.qq.com.}
+ gem.homepage = ""
+
+ gem.files = `git ls-files`.split($\)
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
+ gem.name = "omniauth-tqq-oauth2"
+ gem.require_paths = ["lib"]
+ gem.version = Omniauth::Tqq::Oauth2::VERSION
+
+ gem.add_dependency 'omniauth', '~> 1.0'
+ gem.add_dependency 'omniauth-oauth2', '~> 1.0'
+end

0 comments on commit bfcd25a

Please sign in to comment.