omniauth-weibo-oauth2 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +4 -0
- data/Gemfile +2 -0
- data/README.md +107 -0
- data/lib/omniauth-weibo-oauth2.rb +2 -0
- data/lib/omniauth-weibo-oauth2/version.rb +5 -0
- data/lib/omniauth/strategies/weibo.rb +104 -0
- data/omniauth-weibo-oauth2.gemspec +20 -0
- metadata +77 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4040d4ee269c2068d318a030a337992300946252
|
4
|
+
data.tar.gz: 1f9cb798786b901af6a4a6f56f63feaa1524b47c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 825f565429f22d40d722130ec49114069702fe8196b3cf7a1a41cfec0acf1783ca9c5e93b20b53ba78e7fe59a760a4388665779e21ef25f9c9ce3cb0438c75d5
|
7
|
+
data.tar.gz: f9d353d2decb60c77f84ba401c74adef39a94c758362340e8d3182e1833316d07ab50d0f1da56fa0c55c787a73be97875c50ea8b9d4be888ce96e69524388dfd
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
# OmniAuth Weibo OAuth2
|
2
|
+
|
3
|
+
Weibo OAuth2 Strategy for OmniAuth 1.0.
|
4
|
+
|
5
|
+
Read Weibo OAuth2 docs for more details: http://open.weibo.com/wiki/授权机制
|
6
|
+
|
7
|
+
## Installing
|
8
|
+
|
9
|
+
Add to your `Gemfile`:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'omniauth-weibo-oauth2'
|
13
|
+
```
|
14
|
+
|
15
|
+
Then `bundle install`.
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install omniauth-weibo-oauth2
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
`OmniAuth::Strategies::Weibo` is simply a Rack middleware. Read the OmniAuth 1.0 docs for detailed instructions: https://github.com/intridea/omniauth.
|
24
|
+
|
25
|
+
Here's a quick example, adding the middleware to a Rails app in `config/initializers/omniauth.rb`:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
Rails.application.config.middleware.use OmniAuth::Builder do
|
29
|
+
provider :weibo, ENV['WEIBO_KEY'], ENV['WEIBO_SECRET']
|
30
|
+
end
|
31
|
+
```
|
32
|
+
## Configuration
|
33
|
+
|
34
|
+
you can set up redirect_uri in `omniauth.rb` as following:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
provider :weibo, ENV['WEIBO_KEY'], ENV['WEIBO_SECRET'],
|
38
|
+
token_params: {redirect_uri: "http://127.0.0.1:3000/auth/weibo/callback" }
|
39
|
+
```
|
40
|
+
|
41
|
+
## Authentication Option
|
42
|
+
* **image_size**: This option defines the size of the user's image in *Authentication Hash* (info['image']). Valid options include `small` (30x30), `middle` (50x50), `large` (180x180) and `original` (the size of the image originally uploaded). Default is `middle`.
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
Rails.application.config.middleware.use OmniAuth::Builder do
|
46
|
+
provider :weibo, ENV['WEIBO_KEY'], ENV['WEIBO_SECRET'], :image_size => 'original'
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
## Authentication Hash
|
51
|
+
|
52
|
+
Here's an example *Authentication Hash* available in `request.env['omniauth.auth']`:
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
{
|
56
|
+
:provider => 'weibo',
|
57
|
+
:uid => '1234567890',
|
58
|
+
:info => {
|
59
|
+
:nickname => 'beenhero',
|
60
|
+
:name => 'beenhero',
|
61
|
+
:location => '浙江 杭州',
|
62
|
+
:image => 'http://tp4.sinaimg.cn/1640099215/50/1287016234/1',
|
63
|
+
:description => '移步twitter@beenhero',
|
64
|
+
:urls => { :Blog => 'http://beenhero.com'
|
65
|
+
:Weibo => 'http://weibo.com/beenhero'
|
66
|
+
},
|
67
|
+
},
|
68
|
+
:credentials => {
|
69
|
+
:token => '2.00JjgzmBd7F...', # OAuth 2.0 access_token, which you may wish to store
|
70
|
+
:expires_at => 1331780640, # when the access token expires (if it expires)
|
71
|
+
:expires => true # if you request `offline_access` this will be false
|
72
|
+
},
|
73
|
+
:extra => {
|
74
|
+
:raw_info => {
|
75
|
+
... # data from /2/users/show.json, check by yourself
|
76
|
+
}
|
77
|
+
}
|
78
|
+
}
|
79
|
+
```
|
80
|
+
*PS.* Built and tested on MRI Ruby 1.9.3
|
81
|
+
|
82
|
+
## Build&pulish gem
|
83
|
+
```
|
84
|
+
gem build omniauth-weibo-oauth2.gemspec
|
85
|
+
```
|
86
|
+
|
87
|
+
```
|
88
|
+
gem push omniauth-weibo-oauth2-VERSION.gem
|
89
|
+
```
|
90
|
+
|
91
|
+
## Contributing
|
92
|
+
|
93
|
+
1. Fork it
|
94
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
95
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
96
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
97
|
+
5. Create new Pull Request
|
98
|
+
|
99
|
+
## License
|
100
|
+
|
101
|
+
Copyright (c) 2012 by Bin He
|
102
|
+
|
103
|
+
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:
|
104
|
+
|
105
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
106
|
+
|
107
|
+
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.
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require "omniauth-oauth2"
|
2
|
+
|
3
|
+
module OmniAuth
|
4
|
+
module Strategies
|
5
|
+
class Weibo < OmniAuth::Strategies::OAuth2
|
6
|
+
option :client_options, {
|
7
|
+
:site => "https://api.weibo.com",
|
8
|
+
:authorize_url => "/oauth2/authorize",
|
9
|
+
:token_url => "/oauth2/access_token",
|
10
|
+
:token_method => :post
|
11
|
+
}
|
12
|
+
option :token_params, {
|
13
|
+
:parse => :json
|
14
|
+
}
|
15
|
+
|
16
|
+
uid do
|
17
|
+
raw_info['id']
|
18
|
+
end
|
19
|
+
|
20
|
+
info do
|
21
|
+
{
|
22
|
+
:nickname => raw_info['screen_name'],
|
23
|
+
:name => raw_info['name'],
|
24
|
+
:location => raw_info['location'],
|
25
|
+
:image => image_url,
|
26
|
+
:description => raw_info['description'],
|
27
|
+
:urls => {
|
28
|
+
'Blog' => raw_info['url'],
|
29
|
+
'Weibo' => raw_info['domain'].empty? ? "http://weibo.com/u/#{raw_info['id']}" : "http://weibo.com/#{raw_info['domain']}",
|
30
|
+
}
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
extra do
|
35
|
+
{
|
36
|
+
:raw_info => raw_info
|
37
|
+
}
|
38
|
+
end
|
39
|
+
|
40
|
+
def raw_info
|
41
|
+
access_token.options[:mode] = :query
|
42
|
+
access_token.options[:param_name] = 'access_token'
|
43
|
+
@uid ||= access_token.get('/2/account/get_uid.json').parsed["uid"]
|
44
|
+
@raw_info ||= access_token.get("/2/users/show.json", :params => {:uid => @uid}).parsed
|
45
|
+
end
|
46
|
+
|
47
|
+
def find_image
|
48
|
+
raw_info[%w(avatar_hd avatar_large profile_image_url).find { |e| raw_info[e].present? }]
|
49
|
+
end
|
50
|
+
|
51
|
+
#url: option: size:
|
52
|
+
#avatar_hd original original_size
|
53
|
+
#avatar_large large 180x180
|
54
|
+
#profile_image_url middle 50x50
|
55
|
+
# small 30x30
|
56
|
+
#default is middle
|
57
|
+
def image_url
|
58
|
+
image_size = options[:image_size] || :middle
|
59
|
+
case image_size.to_sym
|
60
|
+
when :original
|
61
|
+
url = raw_info['avatar_hd']
|
62
|
+
when :large
|
63
|
+
url = raw_info['avatar_large']
|
64
|
+
when :small
|
65
|
+
url = raw_info['avatar_large'].sub('/180/','/30/')
|
66
|
+
else
|
67
|
+
url = raw_info['profile_image_url']
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
##
|
72
|
+
# You can pass +display+, +with_offical_account+ or +state+ params to the auth request, if
|
73
|
+
# you need to set them dynamically. You can also set these options
|
74
|
+
# in the OmniAuth config :authorize_params option.
|
75
|
+
#
|
76
|
+
# /auth/weibo?display=mobile&with_offical_account=1
|
77
|
+
#
|
78
|
+
def authorize_params
|
79
|
+
super.tap do |params|
|
80
|
+
%w[display with_offical_account forcelogin].each do |v|
|
81
|
+
if request.params[v]
|
82
|
+
params[v.to_sym] = request.params[v]
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
protected
|
89
|
+
def build_access_token
|
90
|
+
params = {
|
91
|
+
'client_id' => client.id,
|
92
|
+
'client_secret' => client.secret,
|
93
|
+
'code' => request.params['code'],
|
94
|
+
'grant_type' => 'authorization_code',
|
95
|
+
'redirect_uri' => options['redirect_uri']
|
96
|
+
}.merge(token_params.to_hash(symbolize_keys: true))
|
97
|
+
client.get_token(params, deep_symbolize(options.auth_token_params))
|
98
|
+
end
|
99
|
+
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
OmniAuth.config.add_camelization "weibo", "Weibo"
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/omniauth-weibo-oauth2/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 Oauth2 strategy for weibo.com.}
|
8
|
+
gem.summary = %q{OmniAuth Oauth2 strategy for weibo.com.}
|
9
|
+
gem.homepage = "https://github.com/beenhero/omniauth-weibo-oauth2"
|
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-weibo-oauth2"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = OmniAuth::WeiboOauth2::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency 'omniauth', '~> 1.0'
|
19
|
+
gem.add_dependency 'omniauth-oauth2', '~> 1.0'
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: omniauth-weibo-oauth2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bin He
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-24 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: omniauth
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: omniauth-oauth2
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
41
|
+
description: OmniAuth Oauth2 strategy for weibo.com.
|
42
|
+
email: beenhero@gmail.com
|
43
|
+
executables: []
|
44
|
+
extensions: []
|
45
|
+
extra_rdoc_files: []
|
46
|
+
files:
|
47
|
+
- ".gitignore"
|
48
|
+
- Gemfile
|
49
|
+
- README.md
|
50
|
+
- lib/omniauth-weibo-oauth2.rb
|
51
|
+
- lib/omniauth-weibo-oauth2/version.rb
|
52
|
+
- lib/omniauth/strategies/weibo.rb
|
53
|
+
- omniauth-weibo-oauth2.gemspec
|
54
|
+
homepage: https://github.com/beenhero/omniauth-weibo-oauth2
|
55
|
+
licenses: []
|
56
|
+
metadata: {}
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 2.6.7
|
74
|
+
signing_key:
|
75
|
+
specification_version: 4
|
76
|
+
summary: OmniAuth Oauth2 strategy for weibo.com.
|
77
|
+
test_files: []
|