xoauth 0.2.5 → 0.2.6
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.
- checksums.yaml +4 -4
- data/README.md +30 -5
- data/lib/mongoid/xoauth.rb +15 -3
- data/lib/oauth/info.rb +1 -1
- data/lib/oauth/provider/qq.rb +12 -3
- data/lib/oauth/provider/weibo.rb +15 -9
- data/lib/oauth/provider.rb +5 -11
- data/lib/oauth/version.rb +1 -1
- data/lib/oauth.rb +1 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a716f73ea3176bb4780244190b31e8d1df2be885
|
4
|
+
data.tar.gz: 964292a77efdb330133bf38fb36ce20ec90aeba5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 92f7ea7e579ef9cea6bfcebfcaad37b2b63388d4a2ca56ccdf6ec05670a40327d32ab02b504da0d889d39ceb5437cd25c74c5116d7a5dc3b696ac14fc08efcf5
|
7
|
+
data.tar.gz: c8e367a6ac675f5cd4e88a9163ec843f77819c449db189f90e2250388a5517bd7eb16ee87cd6b11c948872ca45be84e01cdc85f367306a48a8ac09b12a7912e6
|
data/README.md
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
# Oauth
|
2
2
|
|
3
|
-
|
3
|
+
Chinese Social network (weibo/qq/weixin currently) authentication with mongoid.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
7
7
|
Add this line to your application's Gemfile:
|
8
8
|
|
9
9
|
```ruby
|
10
|
-
gem 'oauth'
|
10
|
+
gem 'xoauth', :require => 'oauth'
|
11
11
|
```
|
12
12
|
|
13
13
|
And then execute:
|
@@ -16,15 +16,40 @@ And then execute:
|
|
16
16
|
|
17
17
|
Or install it yourself as:
|
18
18
|
|
19
|
-
$ gem install
|
19
|
+
$ gem install xoauth
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
-
|
23
|
+
```ruby
|
24
|
+
class User
|
25
|
+
include Mongoid::Document
|
26
|
+
include Mongoid::Xoauth
|
27
|
+
|
28
|
+
xoauth weibo: {'appid'=> '1112', 'secret'=> '123dffd'}
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
Then we can use the following methods:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
# get the found user or nil
|
36
|
+
User.find_by_oauth_uid(uid, Oauth::Weibo)
|
37
|
+
|
38
|
+
# authorize url
|
39
|
+
Oauth::Weibo.authorize_url
|
40
|
+
|
41
|
+
# detail of code: {access_token, uid, created_at, expires_in}
|
42
|
+
# can be used to initialize Oauth::Weibo
|
43
|
+
Oauth::Weibo.detail_of_code(code)
|
44
|
+
|
45
|
+
# fetch the detail infomation of the oauth client
|
46
|
+
Oauth::Weibo#fetch_info!
|
47
|
+
|
48
|
+
```
|
24
49
|
|
25
50
|
## Contributing
|
26
51
|
|
27
|
-
1. Fork it ( https://github.com/
|
52
|
+
1. Fork it ( https://github.com/Slacken/xoauth/fork )
|
28
53
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
54
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
55
|
4. Push to the branch (`git push origin my-new-feature`)
|
data/lib/mongoid/xoauth.rb
CHANGED
@@ -15,14 +15,26 @@ module Mongoid
|
|
15
15
|
# config oauths
|
16
16
|
params.each_pair{|key, param| Oauth::Configure[key.to_s] = param}
|
17
17
|
end
|
18
|
-
end
|
19
18
|
|
20
|
-
|
21
|
-
|
19
|
+
def find_by_oauth(oauth)
|
20
|
+
where('oauths.uid' => oauth.uid, 'oauths._type' => oauth.class.to_s).first
|
21
|
+
end
|
22
22
|
end
|
23
23
|
|
24
24
|
def oauth(klass)
|
25
25
|
self.oauths.find_by(_type: klass.to_s) # "Oauth::#{name.to_s.capitalize}"
|
26
26
|
end
|
27
|
+
|
28
|
+
def refresh_oauth(oauth)
|
29
|
+
auth = self.oauth(oauth.class)
|
30
|
+
if auth.access_token == oauth.access_token
|
31
|
+
# check expires_in
|
32
|
+
else
|
33
|
+
%w{access_token created_at expires_in refresh_token}.each do |key|
|
34
|
+
auth[key] = oauth[key]
|
35
|
+
end
|
36
|
+
end
|
37
|
+
auth
|
38
|
+
end
|
27
39
|
end
|
28
40
|
end
|
data/lib/oauth/info.rb
CHANGED
data/lib/oauth/provider/qq.rb
CHANGED
@@ -5,6 +5,16 @@ module Oauth
|
|
5
5
|
api_access('get_user_info')
|
6
6
|
end
|
7
7
|
|
8
|
+
def basic_info
|
9
|
+
info && {
|
10
|
+
"name" => info.data["name"],
|
11
|
+
"avatar" => info.data["avatar_hd"] || info.data["avatar_large"],
|
12
|
+
"gender" => info.data["gender"],
|
13
|
+
"location" => info.data["location"],
|
14
|
+
"description" => info.data["description"]
|
15
|
+
}
|
16
|
+
end
|
17
|
+
|
8
18
|
def api_access(api, http_params = {}, http_method = 'get')
|
9
19
|
url = 'https://graph.qq.com/user/' + api
|
10
20
|
http_params.merge!({"access_token" => access_token, "openid"=> uid, "oauth_consumer_key" => Configure['qq']['appid']})
|
@@ -24,14 +34,14 @@ module Oauth
|
|
24
34
|
end
|
25
35
|
end
|
26
36
|
|
27
|
-
def authorize_url
|
37
|
+
def authorize_url(params = {})
|
28
38
|
get_params = {
|
29
39
|
response_type: 'code',
|
30
40
|
client_id: Configure['qq']['appid'],
|
31
41
|
redirect_uri: Configure['qq']['callback'],
|
32
42
|
state: 1,
|
33
43
|
scope: 'get_user_info,get_info,do_like'
|
34
|
-
}
|
44
|
+
}.merge(params)
|
35
45
|
"https://graph.qq.com/oauth2.0/authorize?#{URI.encode_www_form(get_params)}"
|
36
46
|
end
|
37
47
|
|
@@ -46,7 +56,6 @@ module Oauth
|
|
46
56
|
response = getJSON('https://graph.qq.com/oauth2.0/token', get_params)
|
47
57
|
if response # access_token=xxx&expires_in=7776000&refresh_token=xxx
|
48
58
|
detail = Hash[response.split('&').map{|q| q.split('=')}]
|
49
|
-
detail["created_at"] = Time.now
|
50
59
|
detail['uid'] = openid(detail["access_token"])
|
51
60
|
if detail['uid']
|
52
61
|
detail
|
data/lib/oauth/provider/weibo.rb
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
module Oauth
|
2
2
|
class Weibo < Provider
|
3
3
|
|
4
|
-
def follow(uid
|
5
|
-
|
6
|
-
api_access('friendships/create', {'uid'=> uid}, 'post')
|
4
|
+
def follow(uid)
|
5
|
+
api_access('friendships/create', {'uid' => uid}, 'post')
|
7
6
|
end
|
8
7
|
|
9
8
|
def publish(content)
|
@@ -14,6 +13,16 @@ module Oauth
|
|
14
13
|
api_access('users/show',{'uid' => uid})
|
15
14
|
end
|
16
15
|
|
16
|
+
def basic_info
|
17
|
+
info && {
|
18
|
+
"name" => info.data["name"],
|
19
|
+
"avatar" => info.data["avatar_hd"] || info.data["avatar_large"],
|
20
|
+
"gender" => info.data["gender"],
|
21
|
+
"location" => info.data["location"],
|
22
|
+
"description" => info.data["description"]
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
17
26
|
def api_access(api, http_params, http_method = 'get')
|
18
27
|
return nil if expired? # expired
|
19
28
|
url = 'https://api.weibo.com/2/' + api + '.json'
|
@@ -39,13 +48,13 @@ module Oauth
|
|
39
48
|
end
|
40
49
|
end
|
41
50
|
|
42
|
-
def authorize_url
|
51
|
+
def authorize_url(params = {})
|
43
52
|
get_params = {
|
44
53
|
'client_id' => Configure['weibo']['appid'],
|
45
54
|
'redirect_uri' => Configure['weibo']['callback'],
|
46
55
|
'response_type' => 'code',
|
47
56
|
'display' => 'default' # for different divice, default|mobile|wap|client|apponweibo
|
48
|
-
}
|
57
|
+
}.merge(params)
|
49
58
|
"https://api.weibo.com/oauth2/authorize?#{URI.encode_www_form(get_params)}";
|
50
59
|
end
|
51
60
|
|
@@ -59,10 +68,7 @@ module Oauth
|
|
59
68
|
'redirect_uri' => Configure['weibo']['callback']
|
60
69
|
}
|
61
70
|
response = postJSON(url,post_params)
|
62
|
-
if response
|
63
|
-
response["created_at"] = Time.now
|
64
|
-
response.delete('remind_in')
|
65
|
-
end
|
71
|
+
response.delete('remind_in') if response
|
66
72
|
response
|
67
73
|
end
|
68
74
|
end
|
data/lib/oauth/provider.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'net/http'
|
2
|
-
require 'net/https'
|
3
2
|
require 'uri'
|
4
3
|
require 'json'
|
5
4
|
|
@@ -8,21 +7,17 @@ module Oauth
|
|
8
7
|
include Mongoid::Document
|
9
8
|
field :access_token
|
10
9
|
field :uid, type: String
|
11
|
-
field :created_at, type: Time
|
10
|
+
field :created_at, type: Time, default: ->{ Time.now }
|
12
11
|
field :expires_in, type: Integer
|
13
12
|
field :refresh_token
|
14
13
|
|
15
14
|
embedded_in :oauthable, polymorphic: true, inverse_of: :oauth
|
16
|
-
|
15
|
+
belongs_to :info, class_name: 'Oauth::Info', inverse_of: :oauth
|
17
16
|
|
18
17
|
def fetch_info!
|
19
18
|
return nil if expired?
|
20
19
|
info = fetch_info
|
21
|
-
|
22
|
-
self.info = Info.new(data: info)
|
23
|
-
self.save
|
24
|
-
end
|
25
|
-
self.info
|
20
|
+
info && self.info = Oauth::Info.create(data: info)
|
26
21
|
end
|
27
22
|
|
28
23
|
def expired?
|
@@ -40,13 +35,12 @@ module Oauth
|
|
40
35
|
|
41
36
|
def request(url, request_params, method = 'get', format = nil)
|
42
37
|
uri = URI(url)
|
43
|
-
klass = (uri.scheme == 'https' ? Net::HTTPS : Net::HTTP)
|
44
38
|
begin
|
45
39
|
if method == 'get'
|
46
40
|
uri.query = (uri.query.nil? ? '' : (uri.query + "&")) + URI.encode_www_form(request_params)
|
47
|
-
response =
|
41
|
+
response = Net::HTTP.get_response(uri)
|
48
42
|
else
|
49
|
-
response =
|
43
|
+
response = Net::HTTP.post_form(uri, request_params)
|
50
44
|
end
|
51
45
|
rescue Exception => e
|
52
46
|
puts e.message
|
data/lib/oauth/version.rb
CHANGED
data/lib/oauth.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xoauth
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- binz
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-03-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|