1
+ require 'oauth/provider/authorizer'
1
2
module OAuth
2
3
module Controllers
3
4
@@ -9,7 +10,7 @@ def self.included(controller)
9
10
oauthenticate :strategies => :token , :interactive => false , :only => [ :invalidate , :capabilities ]
10
11
oauthenticate :strategies => :two_legged , :interactive => false , :only => [ :request_token ]
11
12
oauthenticate :strategies => :oauth10_request_token , :interactive => false , :only => [ :access_token ]
12
- skip_before_filter :verify_authenticity_token , :only => [ :request_token , :access_token , :invalidate , :test_request ]
13
+ skip_before_filter :verify_authenticity_token , :only => [ :request_token , :access_token , :invalidate , :test_request , :token ]
13
14
end
14
15
end
15
16
@@ -37,7 +38,10 @@ def token
37
38
oauth2_error "invalid_client"
38
39
return
39
40
end
40
- if [ "authorization_code" , "password" , "none" ] . include? ( params [ :grant_type ] )
41
+ # older drafts used none for client_credentials
42
+ params [ :grant_type ] = 'client_credentials' if params [ :grant_type ] == 'none'
43
+ logger . info "grant_type=#{ params [ :grant_type ] } "
44
+ if [ "authorization_code" , "password" , "client_credentials" ] . include? ( params [ :grant_type ] )
41
45
send "oauth2_token_#{ params [ :grant_type ] . underscore } "
42
46
else
43
47
oauth2_error "unsupported_grant_type"
@@ -52,10 +56,14 @@ def authorize
52
56
if params [ :oauth_token ]
53
57
@token = ::RequestToken . find_by_token! params [ :oauth_token ]
54
58
oauth1_authorize
55
- elsif [ "code" , "token" ] . include? ( params [ :response_type ] ) # pick flow
56
- send "oauth2_authorize_#{ params [ :response_type ] } "
57
59
else
58
- render :status => 404 , :text => "No token provided"
60
+ if request . post?
61
+ @authorizer = OAuth ::Provider ::Authorizer . new current_user , user_authorizes_token? , params
62
+ redirect_to @authorizer . redirect_uri
63
+ else
64
+ @client_application = ClientApplication . find_by_key! params [ :client_id ]
65
+ render :action => "oauth2_authorize"
66
+ end
59
67
end
60
68
end
61
69
@@ -121,59 +129,6 @@ def oauth1_authorize
121
129
end
122
130
end
123
131
124
- def oauth2_authorize_code
125
- @client_application = ClientApplication . find_by_key params [ :client_id ]
126
- # Using ||= allows us to override this and customize the verification_code and call super to handle the rest
127
- @token ||= Oauth2Verifier . new :client_application => @client_application , :user => current_user , :callback_url => @redirect_url . to_s , :scope => params [ :scope ] , :state => params [ :state ]
128
- if request . post?
129
- @redirect_url = URI . parse ( params [ :redirect_uri ] || @client_application . callback_url ) if params [ :redirect_uri ] || @client_application . callback_url
130
- if user_authorizes_token? && @token . save
131
- unless @redirect_url . to_s . blank?
132
- @redirect_url . query = @redirect_url . query . blank? ? @token . to_query : @redirect_url . query + @token . to_query
133
- redirect_to @redirect_url . to_s
134
- else
135
- render :action => "authorize_success"
136
- end
137
- else
138
- unless @redirect_url . to_s . blank?
139
- @redirect_url . query = @redirect_url . query . blank? ?
140
- "error=user_denied" :
141
- @redirect_url . query + "&error=user_denied"
142
- redirect_to @redirect_url . to_s
143
- else
144
- render :action => "authorize_failure"
145
- end
146
- end
147
- else
148
- render :action => "oauth2_authorize"
149
- end
150
- end
151
-
152
- def oauth2_authorize_token
153
- @client_application = ClientApplication . find_by_key params [ :client_id ]
154
- @token = Oauth2Token . new :client_application => @client_application , :user => current_user , :scope => params [ :scope ]
155
- if request . post?
156
- @redirect_url = URI . parse ( params [ :redirect_uri ] || @client_application . callback_url )
157
- if user_authorizes_token? && @token . save
158
- unless @redirect_url . to_s . blank?
159
- redirect_to "#{ @redirect_url . to_s } ##{ @token . to_query } "
160
- else
161
- render :action => "authorize_success"
162
- end
163
- else
164
- unless @redirect_url . to_s . blank?
165
- @redirect_url . query = @redirect_url . query . blank? ?
166
- "error=user_denied" :
167
- @redirect_url . query + "&error=user_denied"
168
- redirect_to @redirect_url . to_s
169
- else
170
- render :action => "authorize_failure"
171
- end
172
- end
173
- else
174
- render :action => "oauth2_authorize"
175
- end
176
- end
177
132
178
133
# http://tools.ietf.org/html/draft-ietf-oauth-v2-22#section-4.1.1
179
134
def oauth2_token_authorization_code
@@ -207,7 +162,7 @@ def authenticate_user(username,password)
207
162
end
208
163
209
164
# autonomous authorization which creates a token for client_applications user
210
- def oauth2_token_none
165
+ def oauth2_token_client_credentials
211
166
@token = Oauth2Token . create :client_application => @client_application , :user => @client_application . user , :scope => params [ :scope ]
212
167
render :json => @token
213
168
end
0 commit comments