@@ -137,20 +137,21 @@ def flow(opts = {})
137137
138138 # step 1
139139 get "/login/oauth2/auth" , :response_type => 'code' , :client_id => @client_id , :redirect_uri => 'urn:ietf:wg:oauth:2.0:oob'
140- response . should redirect_to ( login_url ( :re_login => true ) )
140+ response . should redirect_to ( login_url )
141141
142142 yield
143143
144- # step 2
144+ # step 3
145145 response . should be_redirect
146- response [ 'Location' ] . should match ( %r{/login/oauth2/auth?} )
147- code = response [ 'Location' ] . match ( /code=([^\? &]+)/ ) [ 1 ]
148- code . should be_present
146+ response [ 'Location' ] . should match ( %r{/login/oauth2/confirm$} )
147+ get response [ 'Location' ]
148+ response . should render_template ( "pseudonym_sessions/oauth2_confirm" )
149+ post "/login/oauth2/accept" , { :authenticity_token => session [ :_csrf_token ] }
149150
150- # make sure the user is now logged out, or the app also has full access to their session
151- get '/'
152151 response . should be_redirect
153- response [ 'Location' ] . should == 'http://www.example.com/login'
152+ response [ 'Location' ] . should match ( %r{/login/oauth2/auth\? } )
153+ code = response [ 'Location' ] . match ( /code=([^\? &]+)/ ) [ 1 ]
154+ code . should be_present
154155
155156 # we have the code, we can close the browser session
156157 if opts [ :basic_auth ]
@@ -241,11 +242,45 @@ def cas.validate_service_ticket(st)
241242
242243 get '/login' , :ticket => 'ST-abcd'
243244 response . should be_redirect
244- response [ 'Location' ] . should match ( %r{/login/oauth2/auth\? code=} )
245- session . should be_blank
246245 end
247246 end
248247
248+ it "should not require logging in again, or log out afterwards" do
249+ course_with_student_logged_in ( :active_all => true , :user => user_with_pseudonym )
250+ get "/login/oauth2/auth" , :response_type => 'code' , :client_id => @client_id , :redirect_uri => 'urn:ietf:wg:oauth:2.0:oob'
251+ response . should be_redirect
252+ response [ 'Location' ] . should match ( %r{/login/oauth2/confirm$} )
253+ get response [ 'Location' ]
254+ response . should render_template ( "pseudonym_sessions/oauth2_confirm" )
255+ post "/login/oauth2/accept" , { :authenticity_token => session [ :_csrf_token ] }
256+ response . should be_redirect
257+ response [ 'Location' ] . should match ( %r{/login/oauth2/auth\? } )
258+ code = response [ 'Location' ] . match ( /code=([^\? &]+)/ ) [ 1 ]
259+ code . should be_present
260+ get response [ 'Location' ]
261+ response . should be_success
262+ # verify we're still logged in
263+ get "/courses/#{ @course . id } "
264+ response . should be_success
265+ end
266+
267+ it "should redirect with access_denied if the user doesn't accept" do
268+ course_with_student_logged_in ( :active_all => true , :user => user_with_pseudonym )
269+ get "/login/oauth2/auth" , :response_type => 'code' , :client_id => @client_id , :redirect_uri => 'urn:ietf:wg:oauth:2.0:oob'
270+ response . should be_redirect
271+ response [ 'Location' ] . should match ( %r{/login/oauth2/confirm$} )
272+ get response [ 'Location' ]
273+ response . should render_template ( "pseudonym_sessions/oauth2_confirm" )
274+ get "/login/oauth2/deny"
275+ response . should be_redirect
276+ response [ 'Location' ] . should match ( %r{/login/oauth2/auth\? } )
277+ error = response [ 'Location' ] . match ( %r{error=([^\? &]+)} ) [ 1 ]
278+ error . should == "access_denied"
279+ response [ 'Location' ] . should_not match ( %r{code=} )
280+ get response [ 'Location' ]
281+ response . should be_success
282+ end
283+
249284 it "should allow http basic auth for the app auth" do
250285 flow ( :basic_auth => true ) do
251286 get response [ 'Location' ]
@@ -257,7 +292,7 @@ def cas.validate_service_ticket(st)
257292 it "should require the correct client secret" do
258293 # step 1
259294 get "/login/oauth2/auth" , :response_type => 'code' , :client_id => @client_id , :redirect_uri => 'urn:ietf:wg:oauth:2.0:oob'
260- response . should redirect_to ( login_url ( :re_login => true ) )
295+ response . should redirect_to ( login_url )
261296
262297 get response [ 'Location' ]
263298 response . should be_success
@@ -268,7 +303,9 @@ def cas.validate_service_ticket(st)
268303
269304 # step 2
270305 response . should be_redirect
271- response [ 'Location' ] . should match ( %r{/login/oauth2/auth?} )
306+ response [ 'Location' ] . should match ( %r{/login/oauth2/confirm$} )
307+ post "/login/oauth2/accept" , { :authenticity_token => session [ :_csrf_token ] }
308+
272309 code = response [ 'Location' ] . match ( /code=([^\? &]+)/ ) [ 1 ]
273310 code . should be_present
274311
@@ -304,12 +341,17 @@ def cas.validate_service_ticket(st)
304341 @key . update_attribute :redirect_uri , 'http://www.example.com/oauth2response'
305342
306343 get "/login/oauth2/auth" , :response_type => 'code' , :client_id => @client_id , :redirect_uri => "http://www.example.com/my_uri"
307- response . should redirect_to ( login_url ( :re_login => true ) )
344+ response . should redirect_to ( login_url )
308345
309346 get response [ 'Location' ]
310347 response . should be_success
311348 post "/login" , :pseudonym_session => { :unique_id => 'test1@example.com' , :password => 'test123' }
312349
350+ response . should be_redirect
351+ response [ 'Location' ] . should match ( %r{/login/oauth2/confirm$} )
352+ get response [ 'Location' ]
353+ post "/login/oauth2/accept" , { :authenticity_token => session [ :_csrf_token ] }
354+
313355 response . should be_redirect
314356 response [ 'Location' ] . should match ( %r{http://www.example.com/my_uri?} )
315357 code = response [ 'Location' ] . match ( /code=([^\? &]+)/ ) [ 1 ]
0 commit comments