oauth_china 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/README +45 -0
- data/Rakefile +2 -0
- data/lib/oauth_china/strategies/douban.rb +29 -0
- data/lib/oauth_china/strategies/qq.rb +34 -0
- data/lib/oauth_china/strategies/sina.rb +27 -0
- data/lib/oauth_china/version.rb +3 -0
- data/lib/oauth_china.rb +81 -0
- data/oauth_china.gemspec +22 -0
- metadata +96 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
一.简介:
|
2
|
+
OAuth gem for rails,支持豆瓣,新浪微薄,qq微薄
|
3
|
+
|
4
|
+
二.安装:
|
5
|
+
$ gem install oauth_china
|
6
|
+
|
7
|
+
三.使用:
|
8
|
+
|
9
|
+
1. 在Gemfile里添加:
|
10
|
+
|
11
|
+
gem 'oauth_china'
|
12
|
+
|
13
|
+
2. 添加配置文件
|
14
|
+
|
15
|
+
config/oauth/douban.yml
|
16
|
+
config/oauth/sina.yml
|
17
|
+
config/oauth/qq.yml
|
18
|
+
|
19
|
+
例子:
|
20
|
+
|
21
|
+
development:
|
22
|
+
key: "you api key"
|
23
|
+
secret: "your secret"
|
24
|
+
url: "http://yoursite.com"
|
25
|
+
callback: "http://localhost:3000/your_callback_url"
|
26
|
+
production:
|
27
|
+
key: "you api key"
|
28
|
+
secret: "your secret"
|
29
|
+
url: "http://yoursite.com"
|
30
|
+
callback: "http://localhost:3000/your_callback_url"
|
31
|
+
|
32
|
+
3.Example
|
33
|
+
if params[:oauth_token]
|
34
|
+
sina = OauthChina::Sina.load(Rails.cache.read(params[:oauth_token]))
|
35
|
+
sina.authorize(:oauth_verifier => params[:oauth_verifier])
|
36
|
+
#resp0 = sina.get("http://api.douban.com/people/%40me")
|
37
|
+
#resp1 = sina.get("http://open.t.qq.com/api/user/info?format=json")
|
38
|
+
resp = sina.get("/account/verify_credentials.xml")
|
39
|
+
render :text => resp.body
|
40
|
+
else
|
41
|
+
sina = OauthChina::Sina.new
|
42
|
+
url = sina.authorize_url
|
43
|
+
Rails.cache.write(sina.oauth_token, sina.dump)
|
44
|
+
redirect_to url
|
45
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module OauthChina
|
2
|
+
class Douban < OauthChina::OAuth
|
3
|
+
|
4
|
+
def initialize(*args)
|
5
|
+
self.consumer_options = {
|
6
|
+
:signature_method => "HMAC-SHA1",
|
7
|
+
:site => "http://www.douban.com",
|
8
|
+
:scheme => :header,
|
9
|
+
:request_token_path => '/service/auth/request_token',
|
10
|
+
:access_token_path => '/service/auth/access_token',
|
11
|
+
:authorize_path => '/service/auth/authorize',
|
12
|
+
:realm => url
|
13
|
+
}
|
14
|
+
super(*args)
|
15
|
+
end
|
16
|
+
|
17
|
+
def name
|
18
|
+
:douban
|
19
|
+
end
|
20
|
+
|
21
|
+
def authorized?
|
22
|
+
#TODO
|
23
|
+
end
|
24
|
+
|
25
|
+
def destroy
|
26
|
+
#TODO
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module OauthChina
|
2
|
+
class QQ < OauthChina::OAuth
|
3
|
+
|
4
|
+
def initialize(*args)
|
5
|
+
self.consumer_options = {
|
6
|
+
:site => "https://open.t.qq.com",
|
7
|
+
:request_token_path => "/cgi-bin/request_token",
|
8
|
+
:access_token_path => "/cgi-bin/access_token",
|
9
|
+
:authorize_path => "/cgi-bin/authorize",
|
10
|
+
:http_method => :get,
|
11
|
+
:scheme => :query_string,
|
12
|
+
:nonce => nonce,
|
13
|
+
:realm => url
|
14
|
+
}
|
15
|
+
super(*args)
|
16
|
+
end
|
17
|
+
|
18
|
+
def name
|
19
|
+
:qq
|
20
|
+
end
|
21
|
+
|
22
|
+
def nonce
|
23
|
+
Base64.encode64(OpenSSL::Random.random_bytes(32)).gsub(/\W/, '')[0, 32]
|
24
|
+
end
|
25
|
+
|
26
|
+
def authorized?
|
27
|
+
#TODO
|
28
|
+
end
|
29
|
+
|
30
|
+
def destroy
|
31
|
+
#TODO
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module OauthChina
|
2
|
+
class Sina < OauthChina::OAuth
|
3
|
+
|
4
|
+
def initialize(*args)
|
5
|
+
self.consumer_options = {
|
6
|
+
:site => 'http://api.t.sina.com.cn',
|
7
|
+
:request_token_path => '/oauth/request_token',
|
8
|
+
:access_token_path => '/oauth/access_token',
|
9
|
+
:authorize_path => '/oauth/authorize',
|
10
|
+
:realm => url
|
11
|
+
}
|
12
|
+
super(*args)
|
13
|
+
end
|
14
|
+
|
15
|
+
def name
|
16
|
+
:sina
|
17
|
+
end
|
18
|
+
|
19
|
+
def authorized?
|
20
|
+
#TODO
|
21
|
+
end
|
22
|
+
|
23
|
+
def destroy
|
24
|
+
#TODO
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/oauth_china.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'oauth'
|
3
|
+
|
4
|
+
module OauthChina
|
5
|
+
|
6
|
+
class OAuth
|
7
|
+
|
8
|
+
CONFIG = {}
|
9
|
+
|
10
|
+
attr_accessor :request_token, :access_token, :consumer_options
|
11
|
+
|
12
|
+
delegate :get, :post, :put, :delete, :to => :access_token
|
13
|
+
|
14
|
+
def initialize(request_token = nil, request_token_secret = nil)
|
15
|
+
if request_token && request_token_secret
|
16
|
+
self.request_token = ::OAuth::RequestToken.new(consumer, request_token, request_token_secret)
|
17
|
+
else
|
18
|
+
self.request_token = consumer.get_request_token(:oauth_callback => self.callback)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
#每次认证等唯一标志
|
23
|
+
def oauth_token
|
24
|
+
request_token.params[:oauth_token]
|
25
|
+
end
|
26
|
+
|
27
|
+
def consumer
|
28
|
+
@consumer ||= ::OAuth::Consumer.new(key, secret, consumer_options)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.load(data)
|
32
|
+
a_token = data[:access_token]
|
33
|
+
a_token_secret = data[:access_token_secret]
|
34
|
+
|
35
|
+
oauth = self.new(data[:request_token], data[:request_token_secret])
|
36
|
+
oauth.access_token = ::OAuth::AccessToken.new(consumer, a_token, a_token_secret) if a_token
|
37
|
+
oauth
|
38
|
+
end
|
39
|
+
|
40
|
+
def dump
|
41
|
+
{
|
42
|
+
:request_token => request_token.token,
|
43
|
+
:request_token_secret => request_token.secret,
|
44
|
+
:access_token => access_token.nil? ? nil : access_token.token,
|
45
|
+
:access_token_secret => access_token.nil? ? nil : access_token.secret
|
46
|
+
}
|
47
|
+
end
|
48
|
+
|
49
|
+
def key; config['key']; end
|
50
|
+
def secret; config['secret']; end
|
51
|
+
def url; config['url']; end
|
52
|
+
def callback; config["callback"]; end
|
53
|
+
|
54
|
+
def config
|
55
|
+
CONFIG[self.name] ||= lambda do
|
56
|
+
require 'yaml'
|
57
|
+
filename = "#{Rails.root}/config/oauth/#{self.name}.yml"
|
58
|
+
file = File.open(filename)
|
59
|
+
yaml = YAML.load(file)
|
60
|
+
return yaml[Rails.env]
|
61
|
+
end.call
|
62
|
+
end
|
63
|
+
|
64
|
+
def authorize_url
|
65
|
+
@authorize_url ||= request_token.authorize_url(:oauth_callback => URI.encode(callback))
|
66
|
+
end
|
67
|
+
|
68
|
+
#QQ和新浪OAuth需要verifier参数,豆瓣不需要
|
69
|
+
def authorize(options = {})
|
70
|
+
return unless self.access_token.nil?
|
71
|
+
a_token = self.request_token.get_access_token(options)
|
72
|
+
self.access_token ||= ::OAuth::AccessToken.new(consumer, a_token.token, a_token.secret)
|
73
|
+
end
|
74
|
+
|
75
|
+
end
|
76
|
+
|
77
|
+
|
78
|
+
autoload :Sina, 'oauth_china/strategies/sina'
|
79
|
+
autoload :Douban, 'oauth_china/strategies/douban'
|
80
|
+
autoload :QQ, 'oauth_china/strategies/qq'
|
81
|
+
end
|
data/oauth_china.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "oauth_china/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "oauth_china"
|
7
|
+
s.version = OauthChina::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Hooopo"]
|
10
|
+
s.email = ["hoooopo@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{新浪,豆瓣,腾讯等国内微薄OAuth认证}
|
13
|
+
s.description = %q{新浪,豆瓣,腾讯等国内微薄OAuth认证}
|
14
|
+
|
15
|
+
s.rubyforge_project = "oauth_china"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
s.add_development_dependency "oauth"
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: oauth_china
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Hooopo
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-03-20 00:00:00 +08:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: oauth
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
version: "0"
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
description: !binary |
|
36
|
+
5paw5rWq77yM6LGG55Oj77yM6IW+6K6v562J5Zu95YaF5b6u6JaET0F1dGjo
|
37
|
+
rqTor4E=
|
38
|
+
|
39
|
+
email:
|
40
|
+
- hoooopo@gmail.com
|
41
|
+
executables: []
|
42
|
+
|
43
|
+
extensions: []
|
44
|
+
|
45
|
+
extra_rdoc_files: []
|
46
|
+
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- Gemfile
|
50
|
+
- README
|
51
|
+
- Rakefile
|
52
|
+
- lib/oauth_china.rb
|
53
|
+
- lib/oauth_china/strategies/douban.rb
|
54
|
+
- lib/oauth_china/strategies/qq.rb
|
55
|
+
- lib/oauth_china/strategies/sina.rb
|
56
|
+
- lib/oauth_china/version.rb
|
57
|
+
- oauth_china.gemspec
|
58
|
+
has_rdoc: true
|
59
|
+
homepage: ""
|
60
|
+
licenses: []
|
61
|
+
|
62
|
+
post_install_message:
|
63
|
+
rdoc_options: []
|
64
|
+
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
hash: 3
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
version: "0"
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
hash: 3
|
82
|
+
segments:
|
83
|
+
- 0
|
84
|
+
version: "0"
|
85
|
+
requirements: []
|
86
|
+
|
87
|
+
rubyforge_project: oauth_china
|
88
|
+
rubygems_version: 1.6.2
|
89
|
+
signing_key:
|
90
|
+
specification_version: 3
|
91
|
+
summary: !binary |
|
92
|
+
5paw5rWq77yM6LGG55Oj77yM6IW+6K6v562J5Zu95YaF5b6u6JaET0F1dGjo
|
93
|
+
rqTor4E=
|
94
|
+
|
95
|
+
test_files: []
|
96
|
+
|