@@ -406,7 +406,7 @@ def set_basic_auth_header(user, password)
406
406
"manage-clients" ,
407
407
"query-groups" ] } ,
408
408
"account" => { "roles" => [ "manage-account" , "manage-account-links" , "view-profile" ] } } ,
409
- "scope" => "email profile" ,
409
+ "scope" => token_scope ,
410
410
"sid" => "eb235240-0b47-48fa-8b3e-f3b310d352e3" ,
411
411
"email_verified" => false ,
412
412
"preferred_username" => "admin"
@@ -417,6 +417,7 @@ def set_basic_auth_header(user, password)
417
417
let ( :token_sub ) { "b70e2fbf-ea68-420c-a7a5-0a287cb689c6" }
418
418
let ( :token_aud ) { [ "https://openproject.local" , "master-realm" , "account" ] }
419
419
let ( :token_issuer ) { "https://keycloak.local/realms/master" }
420
+ let ( :token_scope ) { "email profile api_v3" }
420
421
let ( :expected_message ) { "You did not provide the correct credentials." }
421
422
let ( :keys_request_stub ) do
422
423
stub_request ( :get , "https://keycloak.local/realms/master/protocol/openid-connect/certs" )
@@ -479,6 +480,20 @@ def set_basic_auth_header(user, password)
479
480
end
480
481
end
481
482
483
+ context "when the scope does not permit access to APIv3" do
484
+ let ( :token_scope ) { "profile email" }
485
+
486
+ it "fails with HTTP 403 Forbidden" do
487
+ get resource
488
+
489
+ expect ( last_response ) . to have_http_status :forbidden
490
+ error = "Requires scope api_v3 to access this resource."
491
+ expect ( last_response . header [ "WWW-Authenticate" ] )
492
+ . to eq ( %{Bearer realm="OpenProject API", error="insufficient_scope", error_description="#{ error } "} )
493
+ expect ( JSON . parse ( last_response . body ) ) . to eq ( error_response_body )
494
+ end
495
+ end
496
+
482
497
context "when access token has expired already" do
483
498
let ( :token_exp ) { 5 . minutes . ago }
484
499
0 commit comments