oauth-plugin 0.3.14 → 0.4.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +10 -0
- data/README.rdoc +44 -9
- data/Rakefile +2 -2
- data/VERSION +1 -1
- data/generators/oauth_consumer/templates/migration.rb +1 -1
- data/generators/oauth_provider/oauth_provider_generator.rb +6 -0
- data/generators/oauth_provider/templates/access_token.rb +2 -2
- data/generators/oauth_provider/templates/client_application.rb +8 -6
- data/generators/oauth_provider/templates/client_applications.yml +6 -6
- data/generators/oauth_provider/templates/clients_controller_spec.rb +130 -193
- data/generators/oauth_provider/templates/controller.rb +12 -0
- data/generators/oauth_provider/templates/controller_spec.rb +762 -291
- data/generators/oauth_provider/templates/controller_spec_helper.rb +49 -63
- data/generators/oauth_provider/templates/migration.rb +6 -5
- data/generators/oauth_provider/templates/oauth2_authorize.html.erb +16 -0
- data/generators/oauth_provider/templates/oauth2_authorize.html.haml +17 -0
- data/generators/oauth_provider/templates/oauth2_token.rb +6 -0
- data/generators/oauth_provider/templates/oauth2_token_spec.rb +29 -0
- data/generators/oauth_provider/templates/oauth2_verifier.rb +28 -0
- data/generators/oauth_provider/templates/oauth2_verifier_spec.rb +54 -0
- data/generators/oauth_provider/templates/oauth_nonce_test.rb +1 -1
- data/generators/oauth_provider/templates/oauth_token.rb +3 -4
- data/generators/oauth_provider/templates/request_token.rb +1 -1
- data/lib/generators/active_record/oauth_consumer_generator.rb +33 -0
- data/lib/generators/active_record/oauth_consumer_templates/consumer_token.rb +5 -0
- data/lib/generators/active_record/oauth_consumer_templates/migration.rb +20 -0
- data/lib/generators/active_record/oauth_provider_generator.rb +39 -0
- data/lib/generators/active_record/oauth_provider_templates/access_token.rb +16 -0
- data/lib/generators/active_record/oauth_provider_templates/client_application.rb +57 -0
- data/lib/generators/active_record/oauth_provider_templates/migration.rb +47 -0
- data/lib/generators/active_record/oauth_provider_templates/oauth2_token.rb +5 -0
- data/lib/generators/active_record/oauth_provider_templates/oauth2_verifier.rb +28 -0
- data/lib/generators/active_record/oauth_provider_templates/oauth_nonce.rb +13 -0
- data/lib/generators/active_record/oauth_provider_templates/oauth_token.rb +30 -0
- data/lib/generators/active_record/oauth_provider_templates/request_token.rb +40 -0
- data/lib/generators/erb/oauth_consumer_generator.rb +14 -0
- data/lib/generators/erb/oauth_consumer_templates/index.html.erb +29 -0
- data/lib/generators/erb/oauth_consumer_templates/show.html.erb +7 -0
- data/lib/generators/erb/oauth_provider_generator.rb +21 -0
- data/lib/generators/erb/oauth_provider_templates/_form.html.erb +17 -0
- data/lib/generators/erb/oauth_provider_templates/authorize.html.erb +14 -0
- data/lib/generators/erb/oauth_provider_templates/authorize_failure.html.erb +1 -0
- data/lib/generators/erb/oauth_provider_templates/authorize_success.html.erb +1 -0
- data/lib/generators/erb/oauth_provider_templates/edit.html.erb +7 -0
- data/lib/generators/erb/oauth_provider_templates/index.html.erb +43 -0
- data/lib/generators/erb/oauth_provider_templates/new.html.erb +5 -0
- data/lib/generators/erb/oauth_provider_templates/oauth2_authorize.html.erb +16 -0
- data/lib/generators/erb/oauth_provider_templates/show.html.erb +27 -0
- data/lib/generators/haml/oauth_consumer_generator.rb +21 -0
- data/lib/generators/haml/oauth_consumer_templates/index.html.haml +18 -0
- data/lib/generators/haml/oauth_consumer_templates/show.html.haml +8 -0
- data/lib/generators/haml/oauth_provider_generator.rb +28 -0
- data/lib/generators/haml/oauth_provider_templates/_form.html.haml +21 -0
- data/lib/generators/haml/oauth_provider_templates/authorize.html.haml +16 -0
- data/lib/generators/haml/oauth_provider_templates/authorize_failure.html.haml +1 -0
- data/lib/generators/haml/oauth_provider_templates/authorize_success.html.haml +1 -0
- data/lib/generators/haml/oauth_provider_templates/edit.html.haml +4 -0
- data/lib/generators/haml/oauth_provider_templates/index.html.haml +39 -0
- data/lib/generators/haml/oauth_provider_templates/new.html.haml +5 -0
- data/lib/generators/haml/oauth_provider_templates/oauth2_authorize.html.haml +17 -0
- data/lib/generators/haml/oauth_provider_templates/show.html.haml +30 -0
- data/lib/generators/mongoid/oauth_consumer_generator.rb +15 -0
- data/lib/generators/mongoid/oauth_consumer_templates/consumer_token.rb +14 -0
- data/lib/generators/mongoid/oauth_provider_generator.rb +21 -0
- data/lib/generators/mongoid/oauth_provider_templates/access_token.rb +16 -0
- data/lib/generators/mongoid/oauth_provider_templates/client_application.rb +71 -0
- data/lib/generators/mongoid/oauth_provider_templates/oauth2_token.rb +5 -0
- data/lib/generators/mongoid/oauth_provider_templates/oauth2_verifier.rb +25 -0
- data/lib/generators/mongoid/oauth_provider_templates/oauth_nonce.rb +24 -0
- data/lib/generators/mongoid/oauth_provider_templates/oauth_token.rb +44 -0
- data/lib/generators/mongoid/oauth_provider_templates/request_token.rb +36 -0
- data/lib/generators/oauth_consumer/USAGE +8 -0
- data/lib/generators/oauth_consumer/oauth_consumer_generator.rb +27 -0
- data/lib/generators/oauth_consumer/templates/controller.rb +19 -0
- data/lib/generators/oauth_consumer/templates/oauth_config.rb +46 -0
- data/lib/generators/oauth_inflections.rb +6 -0
- data/lib/generators/oauth_plugin.rb +0 -0
- data/lib/generators/oauth_provider/USAGE +18 -0
- data/lib/generators/oauth_provider/oauth_provider_generator.rb +47 -0
- data/lib/generators/oauth_provider/templates/clients_controller.rb +52 -0
- data/lib/generators/oauth_provider/templates/controller.rb +23 -0
- data/lib/generators/rspec/oauth_provider_generator.rb +39 -0
- data/lib/generators/rspec/templates/client_application_spec.rb +29 -0
- data/lib/generators/rspec/templates/client_applications.yml +23 -0
- data/lib/generators/rspec/templates/clients_controller_spec.rb +176 -0
- data/lib/generators/rspec/templates/controller_spec.rb +838 -0
- data/lib/generators/rspec/templates/controller_spec_helper.rb +66 -0
- data/lib/generators/rspec/templates/oauth2_token_spec.rb +29 -0
- data/lib/generators/rspec/templates/oauth2_verifier_spec.rb +54 -0
- data/lib/generators/rspec/templates/oauth_nonce_spec.rb +24 -0
- data/lib/generators/rspec/templates/oauth_nonces.yml +13 -0
- data/lib/generators/rspec/templates/oauth_token_spec.rb +309 -0
- data/lib/generators/rspec/templates/oauth_tokens.yml +17 -0
- data/lib/generators/test_unit/oauth_provider_generator.rb +37 -0
- data/lib/generators/test_unit/templates/client_application_test.rb +42 -0
- data/lib/generators/test_unit/templates/client_applications.yml +23 -0
- data/lib/generators/test_unit/templates/clients_controller_test.rb +280 -0
- data/lib/generators/test_unit/templates/controller_test.rb +310 -0
- data/lib/generators/test_unit/templates/controller_test_helper.rb +115 -0
- data/lib/generators/test_unit/templates/oauth_nonce_test.rb +26 -0
- data/lib/generators/test_unit/templates/oauth_nonces.yml +13 -0
- data/lib/generators/test_unit/templates/oauth_token_test.rb +57 -0
- data/lib/generators/test_unit/templates/oauth_tokens.yml +17 -0
- data/lib/oauth/controllers/application_controller_methods.rb +169 -66
- data/lib/oauth/controllers/provider_controller.rb +154 -38
- data/lib/oauth/models/consumers/service_loader.rb +2 -0
- data/lib/oauth/models/consumers/token.rb +0 -1
- data/oauth-plugin.gemspec +97 -10
- data/rails/init.rb +6 -2
- metadata +119 -12
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'oauth/helper'
|
2
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
3
|
+
|
4
|
+
class ClientNoneTest < ActiveSupport::TestCase
|
5
|
+
include OAuth::Helper
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@oauth_nonce = OauthNonce.remember(generate_key,Time.now.to_i)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_should_be_valid
|
12
|
+
assert @oauth_nonce.valid?
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_should_not_have_errors
|
16
|
+
assert_equal [], @oauth_nonce.errors.full_messages
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_should_not_be_a_new_record
|
20
|
+
assert !@oauth_nonce.new_record?
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_shuold_not_allow_a_second_one_with_the_same_values
|
24
|
+
assert_equal false, OauthNonce.remember(@oauth_nonce.nonce, @oauth_nonce.timestamp)
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
|
2
|
+
one:
|
3
|
+
id: 1
|
4
|
+
nonce: a_nonce
|
5
|
+
timestamp: 1
|
6
|
+
created_at: 2007-11-25 17:27:04
|
7
|
+
updated_at: 2007-11-25 17:27:04
|
8
|
+
two:
|
9
|
+
id: 2
|
10
|
+
nonce: b_nonce
|
11
|
+
timestamp: 2
|
12
|
+
created_at: 2007-11-25 17:27:04
|
13
|
+
updated_at: 2007-11-25 17:27:04
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
class RequestTokenTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
fixtures :client_applications, :users, :oauth_tokens
|
6
|
+
|
7
|
+
def setup
|
8
|
+
@token = RequestToken.create :client_application=>client_applications(:one)
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_should_be_valid
|
12
|
+
assert @token.valid?
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_should_not_have_errors
|
16
|
+
assert @token.errors.empty?
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_should_have_a_token
|
20
|
+
assert_not_nil @token.token
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_should_have_a_secret
|
24
|
+
assert_not_nil @token.secret
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_should_not_be_authorized
|
28
|
+
assert !@token.authorized?
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_should_not_be_invalidated
|
32
|
+
assert !@token.invalidated?
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_should_authorize_request
|
36
|
+
@token.authorize!(users(:quentin))
|
37
|
+
assert @token.authorized?
|
38
|
+
assert_not_nil @token.authorized_at
|
39
|
+
assert_equal users(:quentin), @token.user
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_should_not_exchange_without_approval
|
43
|
+
assert_equal false, @token.exchange!
|
44
|
+
assert_equal false, @token.invalidated?
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_should_not_exchange_without_approval
|
48
|
+
@token.authorize!(users(:quentin))
|
49
|
+
@access = @token.exchange!
|
50
|
+
assert_not_equal false, @access
|
51
|
+
assert @token.invalidated?
|
52
|
+
|
53
|
+
assert_equal users(:quentin), @access.user
|
54
|
+
assert @access.authorized?
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
|
2
|
+
one:
|
3
|
+
id: 1
|
4
|
+
user_id: 1
|
5
|
+
client_application_id: 1
|
6
|
+
token: one
|
7
|
+
secret: MyString
|
8
|
+
created_at: 2007-11-19 07:31:46
|
9
|
+
updated_at: 2007-11-19 07:31:46
|
10
|
+
two:
|
11
|
+
id: 2
|
12
|
+
user_id: 1
|
13
|
+
client_application_id: 1
|
14
|
+
token: two
|
15
|
+
secret: MyString
|
16
|
+
created_at: 2007-11-19 07:31:46
|
17
|
+
updated_at: 2007-11-19 07:31:46
|
@@ -3,6 +3,154 @@ module OAuth
|
|
3
3
|
module Controllers
|
4
4
|
|
5
5
|
module ApplicationControllerMethods
|
6
|
+
|
7
|
+
def self.included(controller)
|
8
|
+
controller.class_eval do
|
9
|
+
extend ClassMethods
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
def oauthenticate(options={})
|
15
|
+
filter_options = {}
|
16
|
+
filter_options[:only] = options.delete(:only) if options[:only]
|
17
|
+
filter_options[:except] = options.delete(:except) if options[:except]
|
18
|
+
before_filter Filter.new(options), filter_options
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class Filter
|
23
|
+
def initialize(options={})
|
24
|
+
@options={
|
25
|
+
:interactive=>true,
|
26
|
+
:strategies => [:token,:two_legged]
|
27
|
+
}.merge(options)
|
28
|
+
@strategies = Array(@options[:strategies])
|
29
|
+
@strategies << :interactive if @options[:interactive]
|
30
|
+
end
|
31
|
+
|
32
|
+
def filter(controller)
|
33
|
+
Authenticator.new(controller,@strategies).allow?
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class Authenticator
|
38
|
+
attr_accessor :controller, :strategies, :strategy
|
39
|
+
def initialize(controller,strategies)
|
40
|
+
@controller = controller
|
41
|
+
@strategies = strategies
|
42
|
+
end
|
43
|
+
|
44
|
+
def params
|
45
|
+
controller.send :params
|
46
|
+
end
|
47
|
+
def request
|
48
|
+
controller.send :request
|
49
|
+
end
|
50
|
+
|
51
|
+
def allow?
|
52
|
+
if @strategies.any? do |strategy|
|
53
|
+
@strategy = strategy.to_sym
|
54
|
+
send @strategy
|
55
|
+
end
|
56
|
+
true
|
57
|
+
else
|
58
|
+
if @strategies.include?(:interactive)
|
59
|
+
controller.send :access_denied
|
60
|
+
else
|
61
|
+
controller.send :invalid_oauth_response
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def oauth20_token
|
67
|
+
return false unless defined?(Oauth2Token)
|
68
|
+
token, options = token_and_options
|
69
|
+
token ||= params[:oauth_token] || params[:access_token]
|
70
|
+
if !token.blank?
|
71
|
+
@oauth2_token = Oauth2Token.find_by_token(token)
|
72
|
+
if @oauth2_token && @oauth2_token.authorized?
|
73
|
+
controller.send :current_token=, @oauth2_token
|
74
|
+
end
|
75
|
+
end
|
76
|
+
@oauth2_token!=nil
|
77
|
+
end
|
78
|
+
|
79
|
+
def oauth10_token
|
80
|
+
begin
|
81
|
+
if ClientApplication.verify_request(request) do |request_proxy|
|
82
|
+
@oauth_token = ClientApplication.find_token(request_proxy.token)
|
83
|
+
if @oauth_token.respond_to?(:provided_oauth_verifier=)
|
84
|
+
@oauth_token.provided_oauth_verifier=request_proxy.oauth_verifier
|
85
|
+
end
|
86
|
+
# return the token secret and the consumer secret
|
87
|
+
[(@oauth_token.nil? ? nil : @oauth_token.secret), (@oauth_token.client_application.nil? ? nil : @oauth_token.client_application.secret)]
|
88
|
+
end
|
89
|
+
controller.send :current_token=, @oauth_token
|
90
|
+
true
|
91
|
+
else
|
92
|
+
false
|
93
|
+
end
|
94
|
+
rescue
|
95
|
+
false
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def oauth10_request_token
|
100
|
+
oauth10_token && @oauth_token.is_a?(::RequestToken)
|
101
|
+
end
|
102
|
+
|
103
|
+
def oauth10_access_token
|
104
|
+
oauth10_token && @oauth_token.is_a?(::AccessToken)
|
105
|
+
end
|
106
|
+
|
107
|
+
def token
|
108
|
+
oauth20_token || oauth10_access_token
|
109
|
+
end
|
110
|
+
|
111
|
+
def two_legged
|
112
|
+
begin
|
113
|
+
if ClientApplication.verify_request(request) do |request_proxy|
|
114
|
+
@client_application = ClientApplication.find_by_key(request_proxy.consumer_key)
|
115
|
+
|
116
|
+
# Store this temporarily in client_application object for use in request token generation
|
117
|
+
@client_application.token_callback_url=request_proxy.oauth_callback if request_proxy.oauth_callback
|
118
|
+
|
119
|
+
# return the token secret and the consumer secret
|
120
|
+
[nil, @client_application.secret]
|
121
|
+
end
|
122
|
+
controller.send :current_client_application=, @client_application
|
123
|
+
true
|
124
|
+
else
|
125
|
+
false
|
126
|
+
end
|
127
|
+
rescue
|
128
|
+
false
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def interactive
|
133
|
+
@controller.send :logged_in?
|
134
|
+
end
|
135
|
+
|
136
|
+
# Blatantly stolen from http://github.com/technoweenie/http_token_authentication
|
137
|
+
# Parses the token and options out of the OAuth authorization header. If
|
138
|
+
# the header looks like this:
|
139
|
+
# Authorization: OAuth abc
|
140
|
+
# Then the returned token is "abc", and the options is {:nonce => "def"}
|
141
|
+
#
|
142
|
+
# request - ActionController::Request instance with the current headers.
|
143
|
+
#
|
144
|
+
# Returns an Array of [String, Hash] if a token is present.
|
145
|
+
# Returns nil if no token is found.
|
146
|
+
def token_and_options
|
147
|
+
if header = (request.respond_to?(:authorization) ? request.authorization : ActionController::HttpAuthentication::Basic.authorization(request)).to_s[/^OAuth (.*)/]
|
148
|
+
[$1.strip, {}]
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
153
|
+
|
6
154
|
protected
|
7
155
|
|
8
156
|
def current_token
|
@@ -13,67 +161,28 @@ module OAuth
|
|
13
161
|
@current_client_application
|
14
162
|
end
|
15
163
|
|
16
|
-
def oauthenticate
|
17
|
-
verified=verify_oauth_signature
|
18
|
-
return verified && current_token.is_a?(::AccessToken)
|
19
|
-
end
|
20
|
-
|
21
164
|
def oauth?
|
22
165
|
current_token!=nil
|
23
166
|
end
|
24
167
|
|
25
|
-
# use in a before_filter
|
168
|
+
# use in a before_filter. Note this is for compatibility purposes. Better to use oauthenticate now
|
26
169
|
def oauth_required
|
27
|
-
|
28
|
-
if authorized?
|
29
|
-
return true
|
30
|
-
else
|
31
|
-
invalid_oauth_response
|
32
|
-
end
|
33
|
-
else
|
34
|
-
invalid_oauth_response
|
35
|
-
end
|
170
|
+
Authenticator.new(self,[:oauth10_access_token]).allow?
|
36
171
|
end
|
37
172
|
|
38
|
-
#
|
173
|
+
# use in before_filter. Note this is for compatibility purposes. Better to use oauthenticate now
|
39
174
|
def login_or_oauth_required
|
40
|
-
|
41
|
-
if authorized?
|
42
|
-
return true
|
43
|
-
else
|
44
|
-
invalid_oauth_response
|
45
|
-
end
|
46
|
-
else
|
47
|
-
login_required
|
48
|
-
end
|
175
|
+
Authenticator.new(self,[:oauth10_access_token,:interactive]).allow?
|
49
176
|
end
|
50
177
|
|
51
|
-
|
52
|
-
# verifies a request token request
|
53
|
-
def verify_oauth_consumer_signature
|
54
|
-
begin
|
55
|
-
valid = ClientApplication.verify_request(request) do |request_proxy|
|
56
|
-
@current_client_application = ClientApplication.find_by_key(request_proxy.consumer_key)
|
57
|
-
|
58
|
-
# Store this temporarily in client_application object for use in request token generation
|
59
|
-
@current_client_application.token_callback_url=request_proxy.oauth_callback if request_proxy.oauth_callback
|
60
|
-
|
61
|
-
# return the token secret and the consumer secret
|
62
|
-
[nil, @current_client_application.secret]
|
63
|
-
end
|
64
|
-
rescue
|
65
|
-
valid=false
|
66
|
-
end
|
67
|
-
|
68
|
-
invalid_oauth_response unless valid
|
69
|
-
end
|
70
|
-
|
71
|
-
def verify_oauth_request_token
|
72
|
-
verify_oauth_signature && current_token.is_a?(::RequestToken)
|
73
|
-
end
|
74
|
-
|
75
178
|
def invalid_oauth_response(code=401,message="Invalid OAuth Request")
|
76
179
|
render :text => message, :status => code
|
180
|
+
false
|
181
|
+
end
|
182
|
+
|
183
|
+
# override this in your controller
|
184
|
+
def access_denied
|
185
|
+
head 401
|
77
186
|
end
|
78
187
|
|
79
188
|
private
|
@@ -82,27 +191,21 @@ module OAuth
|
|
82
191
|
@current_token=token
|
83
192
|
if @current_token
|
84
193
|
@current_user=@current_token.user
|
85
|
-
@current_client_application=@current_token.client_application
|
194
|
+
@current_client_application=@current_token.client_application
|
195
|
+
else
|
196
|
+
@current_user = nil
|
197
|
+
@current_client_application = nil
|
86
198
|
end
|
87
199
|
@current_token
|
88
200
|
end
|
89
201
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
end
|
98
|
-
# return the token secret and the consumer secret
|
99
|
-
[(current_token.nil? ? nil : current_token.secret), (current_client_application.nil? ? nil : current_client_application.secret)]
|
100
|
-
end
|
101
|
-
# reset @current_user to clear state for restful_...._authentication
|
102
|
-
@current_user = nil if (!valid)
|
103
|
-
valid
|
104
|
-
rescue
|
105
|
-
false
|
202
|
+
def current_client_application=(app)
|
203
|
+
if app
|
204
|
+
@current_client_application = app
|
205
|
+
@current_user = app.user
|
206
|
+
else
|
207
|
+
@current_client_application = nil
|
208
|
+
@current_user = nil
|
106
209
|
end
|
107
210
|
end
|
108
211
|
end
|
@@ -5,16 +5,16 @@ module OAuth
|
|
5
5
|
def self.included(controller)
|
6
6
|
controller.class_eval do
|
7
7
|
before_filter :login_required, :only => [:authorize,:revoke]
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
8
|
+
oauthenticate :only => [:test_request]
|
9
|
+
oauthenticate :strategies => :token, :interactive => false, :only => [:invalidate,:capabilities]
|
10
|
+
oauthenticate :strategies => :two_legged, :interactive => false, :only => [:request_token]
|
11
|
+
oauthenticate :strategies => :oauth10_request_token, :interactive => false, :only => [:access_token]
|
12
12
|
skip_before_filter :verify_authenticity_token, :only=>[:request_token, :access_token, :invalidate, :test_request]
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
16
16
|
def request_token
|
17
|
-
@token = current_client_application.create_request_token
|
17
|
+
@token = current_client_application.create_request_token params
|
18
18
|
if @token
|
19
19
|
render :text => @token.to_query
|
20
20
|
else
|
@@ -31,43 +31,29 @@ module OAuth
|
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
34
|
+
def token
|
35
|
+
@client_application = ClientApplication.find_by_key params[:client_id]
|
36
|
+
if @client_application.secret != params[:client_secret]
|
37
|
+
oauth2_error "invalid_client"
|
38
|
+
return
|
39
|
+
end
|
40
|
+
if ["authorization_code","password","none"].include?(params[:grant_type])
|
41
|
+
send "oauth2_token_#{params[:grant_type].underscore}"
|
42
|
+
else
|
43
|
+
oauth2_error "unsupported_grant_type"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
34
47
|
def test_request
|
35
48
|
render :text => params.collect{|k,v|"#{k}=#{v}"}.join("&")
|
36
49
|
end
|
37
50
|
|
38
51
|
def authorize
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
unless @token.invalidated?
|
46
|
-
if request.post?
|
47
|
-
if user_authorizes_token?
|
48
|
-
@token.authorize!(current_user)
|
49
|
-
if @token.oauth10?
|
50
|
-
@redirect_url = params[:oauth_callback] || @token.client_application.callback_url
|
51
|
-
else
|
52
|
-
@redirect_url = @token.oob? ? @token.client_application.callback_url : @token.callback_url
|
53
|
-
end
|
54
|
-
|
55
|
-
if @redirect_url
|
56
|
-
if @token.oauth10?
|
57
|
-
redirect_to "#{@redirect_url}?oauth_token=#{@token.token}"
|
58
|
-
else
|
59
|
-
redirect_to "#{@redirect_url}?oauth_token=#{@token.token}&oauth_verifier=#{@token.verifier}"
|
60
|
-
end
|
61
|
-
else
|
62
|
-
render :action => "authorize_success"
|
63
|
-
end
|
64
|
-
else
|
65
|
-
@token.invalidate!
|
66
|
-
render :action => "authorize_failure"
|
67
|
-
end
|
68
|
-
end
|
69
|
-
else
|
70
|
-
render :action => "authorize_failure"
|
52
|
+
if params[:oauth_token]
|
53
|
+
@token = ::RequestToken.find_by_token params[:oauth_token]
|
54
|
+
oauth1_authorize
|
55
|
+
elsif ["code","token"].include?(params[:response_type]) # pick flow
|
56
|
+
send "oauth2_authorize_#{params[:response_type]}"
|
71
57
|
end
|
72
58
|
end
|
73
59
|
|
@@ -102,10 +88,140 @@ module OAuth
|
|
102
88
|
|
103
89
|
protected
|
104
90
|
|
91
|
+
def oauth1_authorize
|
92
|
+
unless @token
|
93
|
+
render :action=>"authorize_failure"
|
94
|
+
return
|
95
|
+
end
|
96
|
+
|
97
|
+
unless @token.invalidated?
|
98
|
+
if request.post?
|
99
|
+
if user_authorizes_token?
|
100
|
+
@token.authorize!(current_user)
|
101
|
+
@redirect_url = URI.parse(@token.oob? ? @token.client_application.callback_url : @token.callback_url)
|
102
|
+
|
103
|
+
unless @redirect_url.to_s.blank?
|
104
|
+
@redirect_url.query = @redirect_url.query.blank? ?
|
105
|
+
"oauth_token=#{@token.token}&oauth_verifier=#{@token.verifier}" :
|
106
|
+
@redirect_url.query + "&oauth_token=#{@token.token}&oauth_verifier=#{@token.verifier}"
|
107
|
+
redirect_to @redirect_url.to_s
|
108
|
+
else
|
109
|
+
render :action => "authorize_success"
|
110
|
+
end
|
111
|
+
else
|
112
|
+
@token.invalidate!
|
113
|
+
render :action => "authorize_failure"
|
114
|
+
end
|
115
|
+
end
|
116
|
+
else
|
117
|
+
render :action => "authorize_failure"
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
def oauth2_authorize_code
|
122
|
+
@client_application = ClientApplication.find_by_key params[:client_id]
|
123
|
+
if request.post?
|
124
|
+
@redirect_url = URI.parse(params[:redirect_url] || @client_application.callback_url)
|
125
|
+
if user_authorizes_token?
|
126
|
+
@verification_code = Oauth2Verifier.create :client_application=>@client_application, :user=>current_user, :callback_url=>@redirect_url.to_s
|
127
|
+
|
128
|
+
unless @redirect_url.to_s.blank?
|
129
|
+
@redirect_url.query = @redirect_url.query.blank? ?
|
130
|
+
"code=#{@verification_code.code}" :
|
131
|
+
@redirect_url.query + "&code=#{@verification_code.code}"
|
132
|
+
redirect_to @redirect_url.to_s
|
133
|
+
else
|
134
|
+
render :action => "authorize_success"
|
135
|
+
end
|
136
|
+
else
|
137
|
+
unless @redirect_url.to_s.blank?
|
138
|
+
@redirect_url.query = @redirect_url.query.blank? ?
|
139
|
+
"error=user_denied" :
|
140
|
+
@redirect_url.query + "&error=user_denied"
|
141
|
+
redirect_to @redirect_url.to_s
|
142
|
+
else
|
143
|
+
render :action => "authorize_failure"
|
144
|
+
end
|
145
|
+
end
|
146
|
+
else
|
147
|
+
render :action => "oauth2_authorize"
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def oauth2_authorize_token
|
152
|
+
@client_application = ClientApplication.find_by_key params[:client_id]
|
153
|
+
if request.post?
|
154
|
+
@redirect_url = URI.parse(params[:redirect_url] || @client_application.callback_url)
|
155
|
+
if user_authorizes_token?
|
156
|
+
@token = Oauth2Token.create :client_application=>@client_application, :user=>current_user, :scope=>params[:scope]
|
157
|
+
unless @redirect_url.to_s.blank?
|
158
|
+
@redirect_url.query = @redirect_url.query.blank? ?
|
159
|
+
"access_token=#{@token.token}" :
|
160
|
+
@redirect_url.query + "&access_token=#{@token.token}"
|
161
|
+
redirect_to @redirect_url.to_s
|
162
|
+
else
|
163
|
+
render :action => "authorize_success"
|
164
|
+
end
|
165
|
+
else
|
166
|
+
unless @redirect_url.to_s.blank?
|
167
|
+
@redirect_url.query = @redirect_url.query.blank? ?
|
168
|
+
"error=user_denied" :
|
169
|
+
@redirect_url.query + "&error=user_denied"
|
170
|
+
redirect_to @redirect_url.to_s
|
171
|
+
else
|
172
|
+
render :action => "authorize_failure"
|
173
|
+
end
|
174
|
+
end
|
175
|
+
else
|
176
|
+
render :action => "oauth2_authorize"
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
# http://tools.ietf.org/html/draft-ietf-oauth-v2-08#section-4.1.1
|
181
|
+
def oauth2_token_authorization_code
|
182
|
+
@verification_code = @client_application.oauth2_verifiers.find_by_token params[:code]
|
183
|
+
unless @verification_code
|
184
|
+
oauth2_error
|
185
|
+
return
|
186
|
+
end
|
187
|
+
if @verification_code.redirect_url != params[:redirect_url]
|
188
|
+
oauth2_error
|
189
|
+
return
|
190
|
+
end
|
191
|
+
@token = @verification_code.exchange!
|
192
|
+
render :json=>@token
|
193
|
+
end
|
194
|
+
|
195
|
+
# http://tools.ietf.org/html/draft-ietf-oauth-v2-08#section-4.1.2
|
196
|
+
def oauth2_token_password
|
197
|
+
@user = authenticate_user( params[:username], params[:password])
|
198
|
+
unless @user
|
199
|
+
oauth2_error
|
200
|
+
return
|
201
|
+
end
|
202
|
+
@token = Oauth2Token.create :client_application=>@client_application, :user=>@user, :scope=>params[:scope]
|
203
|
+
render :json=>@token
|
204
|
+
end
|
205
|
+
|
206
|
+
# should authenticate and return a user if valid password. Override in your own controller
|
207
|
+
def authenticate_user(username,password)
|
208
|
+
User.authenticate(username,password)
|
209
|
+
end
|
210
|
+
|
211
|
+
# autonomous authorization which creates a token for client_applications user
|
212
|
+
def oauth2_token_none
|
213
|
+
@token = Oauth2Token.create :client_application=>@client_application, :user=>@client_application.user, :scope=>params[:scope]
|
214
|
+
render :json=>@token
|
215
|
+
end
|
216
|
+
|
105
217
|
# Override this to match your authorization page form
|
106
218
|
def user_authorizes_token?
|
107
219
|
params[:authorize] == '1'
|
220
|
+
end
|
221
|
+
|
222
|
+
def oauth2_error(error="invalid_grant")
|
223
|
+
render :json=>{:error=>error}.to_json
|
108
224
|
end
|
109
225
|
end
|
110
226
|
end
|
111
|
-
end
|
227
|
+
end
|