omniauth-qq 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in omniauth-qq.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,133 @@
1
+ # Omniauth QQ
2
+
3
+ This is QQ strategies collection for OmniAuth 1.0, which includes TQQ and QQ-Connect.
4
+
5
+ ## [TQQ](http://open.t.qq.com/) OAuth
6
+ Strategy from https://github.com/ballantyne/omniauth-tqq, credit go to Scott Ballantyne.
7
+ Please note that some modifies had written into for my own purpose, like: raw_info attributes rewrote.
8
+
9
+ ## [QQ-Connect](http://connect.qq.com/intro/login/) OAuth2
10
+ Strategy from https://github.com/kaichen/omniauth-qq-connect, credit go to Kai Chen.
11
+
12
+ ## Installation
13
+
14
+ Add to your `Gemfile`:
15
+
16
+ ```ruby
17
+ gem 'omniauth-qq'
18
+ ```
19
+
20
+ Then `bundle install`.
21
+
22
+ Or install it yourself as:
23
+
24
+ $ gem install omniauth-qq
25
+
26
+ ## Usage
27
+
28
+ `OmniAuth::Strategies::Qq` is simply a Rack middleware. Read the OmniAuth 1.0 docs for detailed instructions: https://github.com/intridea/omniauth.
29
+
30
+ Here's a quick example, adding the middleware to a Rails app in `config/initializers/omniauth.rb`:
31
+
32
+ ```ruby
33
+ Rails.application.config.middleware.use OmniAuth::Builder do
34
+ provider :tqq, ENV['TQQ_KEY'], ENV['TQQ_SECRET']
35
+ provider :qq_connect, ENV['QQ_CONNECT_KEY'], ENV['QQ_CONNECT_SECRET']
36
+ end
37
+ ```
38
+
39
+ ## Authentication Hash
40
+
41
+ Here's an example *Authentication Hash* available in `request.env['omniauth.auth']`:
42
+
43
+ ### TQQ returns:
44
+
45
+ ```ruby
46
+ {
47
+ :provider => 'tqq',
48
+ :uid => 'B11630C4...', # QQ call it openid
49
+ :info => {
50
+ :nickname => 'beenhero',
51
+ :email => '',
52
+ :name => 'beenhero',
53
+ :location => '杭州',
54
+ :image => 'http://app.qlogo.cn/mbloghead/b8bc8cee839d42d1824c/40',
55
+ :description => '碳水化合物而已',
56
+ :urls => { :Tqq => 't.qq.com/beenhero' },
57
+ },
58
+ :credentials => {
59
+ :token => '2.00JjgzmBd7F...', # OAuth access_token, which you may wish to store
60
+ :secret => 'ac737720847e...',
61
+ },
62
+ :extra => {
63
+ :raw_info => {
64
+ :data => {
65
+ ... # data from http://open.t.qq.com/api/user/info?format=json, check by yourself
66
+ },
67
+ # extracted some general named attribute from [date]
68
+ :gender => 'm', # m: male, f: female, '': none
69
+ :followers_count: 53,
70
+ :friends_count: 14,
71
+ }
72
+ }
73
+ }
74
+ ```
75
+
76
+ ### QQ-Connect returns:
77
+
78
+ ```ruby
79
+ {
80
+ :provider => 'qq_connect',
81
+ :uid => 'B11630C4...', # QQ call it openid
82
+ :info => {
83
+ :nickname => 'beenhero',
84
+ :image => 'http://qzapp.qlogo.cn/qzapp/100250034/B11630C4AAC8C17B57ECFEA80852C813/50',
85
+ # so little info !? I think so, QQ-Connect only provides so, you can check from the raw_info below. Or you can try TQQ instead :)
86
+ },
87
+ :credentials => {
88
+ :token => '2.00JjgzmBd7F...', # OAuth 2.0 access_token, which you may wish to store
89
+ :expires_at => 1331780640, # when the access token expires (if it expires)
90
+ :expires => true # if you request `offline_access` this will be false
91
+ },
92
+ :extra => {
93
+ :raw_info => {
94
+ ... # little info from https://graph.qq.com/user/get_user_info
95
+ }
96
+ }
97
+ }
98
+ ```
99
+
100
+ *PS.* Built and tested on MRI Ruby 1.9.3
101
+
102
+ ## Contributing
103
+
104
+ 1. Fork it
105
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
106
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
107
+ 4. Push to the branch (`git push origin my-new-feature`)
108
+ 5. Create new Pull Request
109
+
110
+ ## License
111
+
112
+ Copyright (c) 2012 by Bin He
113
+
114
+ MIT License
115
+
116
+ Permission is hereby granted, free of charge, to any person obtaining
117
+ a copy of this software and associated documentation files (the
118
+ "Software"), to deal in the Software without restriction, including
119
+ without limitation the rights to use, copy, modify, merge, publish,
120
+ distribute, sublicense, and/or sell copies of the Software, and to
121
+ permit persons to whom the Software is furnished to do so, subject to
122
+ the following conditions:
123
+
124
+ The above copyright notice and this permission notice shall be
125
+ included in all copies or substantial portions of the Software.
126
+
127
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
128
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
129
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
130
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
131
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
132
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
133
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ require "omniauth-qq/version"
2
+ require 'omniauth/strategies/qq_connect'
3
+ require 'omniauth/strategies/tqq'
@@ -0,0 +1,5 @@
1
+ module Omniauth
2
+ module Qq
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,62 @@
1
+ # encoding: utf-8
2
+
3
+ require 'omniauth/strategies/oauth2'
4
+
5
+ module OmniAuth
6
+ module Strategies
7
+ class QQConnect < OmniAuth::Strategies::OAuth2
8
+ option :name, "qq_connect"
9
+
10
+ option :client_options, {
11
+ :site => 'https://graph.qq.com/oauth2.0/',
12
+ :authorize_url => '/oauth2.0/authorize',
13
+ :token_url => "/oauth2.0/token"
14
+ }
15
+
16
+ option :token_params, {
17
+ :state => 'foobar',
18
+ :parse => :query
19
+ }
20
+
21
+ uid do
22
+ @uid ||= begin
23
+ access_token.options[:mode] = :query
24
+ access_token.options[:param_name] = :access_token
25
+ # Response Example: "callback( {\"client_id\":\"11111\",\"openid\":\"000000FFFF\"} );\n"
26
+ response = access_token.get('/oauth2.0/me')
27
+ #TODO handle error case
28
+ matched = response.body.match(/"openid":"(?<openid>\w+)"/)
29
+ matched[:openid]
30
+ end
31
+ end
32
+
33
+ info do
34
+ {
35
+ :nickname => raw_info['nickname'],
36
+ :image => raw_info['figureurl_1'],
37
+ }
38
+ end
39
+
40
+ extra do
41
+ {
42
+ :raw_info => raw_info
43
+ }
44
+ end
45
+
46
+ def raw_info
47
+ @raw_info ||= begin
48
+ #TODO handle error case
49
+ #TODO make info request url configurable
50
+ client.request(:get, "https://graph.qq.com/user/get_user_info", :params => {
51
+ :format => :json,
52
+ :openid => uid,
53
+ :oauth_consumer_key => options[:client_id],
54
+ :access_token => access_token.token
55
+ }, :parse => :json).parsed
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ OmniAuth.config.add_camelization('qq_connect', 'QQConnect')
@@ -0,0 +1,83 @@
1
+ require 'omniauth-oauth'
2
+ require 'multi_json'
3
+
4
+ module OmniAuth
5
+ module Strategies
6
+ class Tqq < OmniAuth::Strategies::OAuth
7
+ option :name, 'tqq'
8
+ option :sign_in, true
9
+ def initialize(*args)
10
+ super
11
+ # taken from https://github.com/intridea/omniauth/blob/0-3-stable/oa-oauth/lib/omniauth/strategies/oauth/tqq.rb#L15-24
12
+ options.client_options = {
13
+ :access_token_path => '/cgi-bin/access_token',
14
+ :authorize_path => '/cgi-bin/authorize',
15
+ :http_method => :get,
16
+ :nonce => nonce,
17
+ :realm => 'OmniAuth',
18
+ :request_token_path => '/cgi-bin/request_token',
19
+ :scheme => :query_string,
20
+ :site => 'https://open.t.qq.com',
21
+ }
22
+ end
23
+
24
+ # https://github.com/intridea/omniauth/blob/0-3-stable/oa-oauth/lib/omniauth/strategies/oauth/tqq.rb#L28-30
25
+ def nonce
26
+ Base64.encode64(OpenSSL::Random.random_bytes(32)).gsub(/\W/, '')[0, 32]
27
+ end
28
+
29
+ def consumer
30
+ consumer = ::OAuth::Consumer.new(options.consumer_key, options.consumer_secret, options.client_options)
31
+ consumer
32
+ end
33
+
34
+ uid { raw_info['data']['openid'] }
35
+
36
+ info do
37
+ {
38
+ :nickname => raw_info['data']['nick'],
39
+ :email => raw_info['data']['email'],
40
+ :name => raw_info['data']['name'],
41
+ :location => raw_info['data']['location'],
42
+ :image => raw_info['data']['head'] + '/40',
43
+ :description => raw_info['data']['introduction'],
44
+ :urls => {
45
+ 'Tqq' => 't.qq.com/' + raw_info['data']['name']
46
+ }
47
+ }
48
+ end
49
+
50
+ extra do
51
+ # rename some attribute to my own needs.
52
+ raw_info['gender'] ||= raw_info['data']['sex'] == 1 ? 'm' : (raw_info['data']['sex'] == 2 ? 'f' : '')
53
+ raw_info['followers_count'] ||= raw_info['data']['fansnum']
54
+ raw_info['friends_count'] ||= raw_info['data']['idolnum']
55
+ { :raw_info => raw_info }
56
+ end
57
+
58
+ #taken from https://github.com/intridea/omniauth/blob/0-3-stable/oa-oauth/lib/omniauth/strategies/oauth/tsina.rb#L52-67
59
+ def request_phase
60
+ request_token = consumer.get_request_token(:oauth_callback => callback_url)
61
+ session['oauth'] ||= {}
62
+ session['oauth'][name.to_s] = {'callback_confirmed' => true, 'request_token' => request_token.token, 'request_secret' => request_token.secret}
63
+
64
+ if request_token.callback_confirmed?
65
+ redirect request_token.authorize_url(options[:authorize_params])
66
+ else
67
+ redirect request_token.authorize_url(options[:authorize_params].merge(:oauth_callback => callback_url))
68
+ end
69
+
70
+ rescue ::Timeout::Error => e
71
+ fail!(:timeout, e)
72
+ rescue ::Net::HTTPFatalError, ::OpenSSL::SSL::SSLError => e
73
+ fail!(:service_unavailable, e)
74
+ end
75
+
76
+ def raw_info
77
+ @raw_info ||= MultiJson.decode(access_token.get('http://open.t.qq.com/api/user/info?format=json').body)
78
+ rescue ::Errno::ETIMEDOUT
79
+ raise ::Timeout::Error
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/omniauth-qq/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Bin He"]
6
+ gem.email = ["beenhero@gmail.com"]
7
+ gem.description = %q{OmniAuth strategies for TQQ and QQ Connect).}
8
+ gem.summary = %q{OmniAuth strategies for TQQ and QQ Connect).}
9
+ gem.homepage = "https://github.com/beenhero/omniauth-qq"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "omniauth-qq"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Omniauth::Qq::VERSION
17
+
18
+ gem.add_dependency 'omniauth', '~> 1.0'
19
+ gem.add_dependency 'omniauth-oauth2', '~> 1.0'
20
+ gem.add_dependency 'multi_json'
21
+ end
metadata ADDED
@@ -0,0 +1,87 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: omniauth-qq
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bin He
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: omniauth
16
+ requirement: &70123960967340 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70123960967340
25
+ - !ruby/object:Gem::Dependency
26
+ name: omniauth-oauth2
27
+ requirement: &70123960966780 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: '1.0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70123960966780
36
+ - !ruby/object:Gem::Dependency
37
+ name: multi_json
38
+ requirement: &70123960966380 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70123960966380
47
+ description: OmniAuth strategies for TQQ and QQ Connect).
48
+ email:
49
+ - beenhero@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - README.md
57
+ - Rakefile
58
+ - lib/omniauth-qq.rb
59
+ - lib/omniauth-qq/version.rb
60
+ - lib/omniauth/strategies/qq_connect.rb
61
+ - lib/omniauth/strategies/tqq.rb
62
+ - omniauth-qq.gemspec
63
+ homepage: https://github.com/beenhero/omniauth-qq
64
+ licenses: []
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ! '>='
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.17
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: OmniAuth strategies for TQQ and QQ Connect).
87
+ test_files: []