66//! Keycloak is only the provider's token issuer here; the CLI still reaches
77//! the gateway the usual way.
88
9- use std:: io:: Write as _;
10-
119use keycloak_support:: KeycloakContext ;
12- use testkit:: { CliOutput , Context , ResultExt as _} ;
10+ use testkit:: policy:: { Access , Endpoint as _} ;
11+ use testkit:: sdk:: raw:: proto;
12+ use testkit:: { CliOutput , Context , Credential , Profile } ;
1313
1414const CREDENTIAL_KEY : & str = "KEYCLOAK_ACCESS_TOKEN" ;
1515
@@ -20,34 +20,14 @@ const CREDENTIAL_KEY: &str = "KEYCLOAK_ACCESS_TOKEN";
2020async fn revoked_refresh_grant_requires_user_reauthorization ( cx : & mut Context ) -> testkit:: Result {
2121 let kc = KeycloakContext :: new ( cx) . await ?;
2222 let provider = kc. name ( "kc-refresh" ) ;
23- let profile_id = kc. name ( "kc-refresh-profile" ) ;
2423 let grant = kc. keycloak ( ) . password_grant ( ) . await ?;
25- let profile = write_profile ( & profile_id, kc. keycloak ( ) ) ?;
26- let profile_path = profile. path ( ) . to_string_lossy ( ) . into_owned ( ) ;
27-
28- // Register each delete before its create, so cleanup also covers a create
29- // the gateway applied but the CLI reported as failed. Both deletes succeed
30- // when there is nothing to delete.
31- delete_on_cleanup ( & kc, "profile" , & profile_id) ;
32- kc. cli ( [ "profile" , "import" , "--file" , & profile_path] )
33- . await ?
34- . success ( ) ?;
35- delete_on_cleanup ( & kc, "provider" , & provider) ;
36- kc. cli_with_env (
24+ let profile = refresh_profile ( & kc. name ( "kc-refresh-profile" ) , kc. keycloak ( ) ) . to_yaml ( ) ?;
25+ kc. install_provider (
26+ & provider,
27+ & profile,
3728 & [ ( CREDENTIAL_KEY , & grant. access_token ) ] ,
38- [
39- "provider" ,
40- "create" ,
41- "--name" ,
42- & provider,
43- "--type" ,
44- & profile_id,
45- "--credential" ,
46- CREDENTIAL_KEY ,
47- ] ,
4829 )
49- . await ?
50- . success ( ) ?;
30+ . await ?;
5131 let client_id = format ! ( "client_id={}" , kc. keycloak( ) . client_id( ) ) ;
5232 kc. cli_with_env (
5333 & [ ( "KEYCLOAK_REFRESH_TOKEN" , & grant. refresh_token ) ] ,
@@ -117,64 +97,43 @@ async fn revoked_refresh_grant_requires_user_reauthorization(cx: &mut Context) -
11797 Ok ( ( ) )
11898}
11999
120- /// Delete the `kind` named `name` when the test ends, since testkit only
121- /// cleans up sandboxes by itself.
122- fn delete_on_cleanup ( cx : & Context , kind : & ' static str , name : & str ) {
123- let cli = cx. cli_handle ( ) ;
124- let name = name. to_string ( ) ;
125- cx. on_cleanup ( format ! ( "delete {kind} {name}" ) , async move || {
126- cli. run ( [ kind, "delete" , & name] ) . await ?. success ( ) ?;
127- Ok ( ( ) )
128- } ) ;
129- }
130-
131100fn combined ( output : & CliOutput ) -> String {
132101 format ! ( "{}{}" , output. stdout, output. stderr)
133102}
134103
135- fn write_profile (
136- profile_id : & str ,
137- keycloak : & keycloak_support:: Keycloak ,
138- ) -> testkit:: Result < tempfile:: NamedTempFile > {
139- let issuer_token_url = keycloak. token_endpoint ( ) ;
140- let profile = format ! (
141- r#"id: {profile_id}
142- display_name: Keycloak provider refresh test
143- category: other
144- credentials:
145- - name: access_token
146- env_vars: [{CREDENTIAL_KEY}]
147- required: true
148- auth_style: bearer
149- header_name: authorization
150- refresh:
151- strategy: oauth2_refresh_token
152- token_url: {issuer_token_url}
153- scopes: [openid]
154- refresh_before_seconds: 60
155- max_lifetime_seconds: 3600
156- material:
157- - name: client_id
158- required: true
159- - name: refresh_token
160- required: true
161- secret: true
162- endpoints:
163- - host: keycloak.test.invalid
164- port: 443
165- protocol: rest
166- access: read-only
167- enforcement: enforce
168- binaries:
169- - /usr/bin/curl
170- "#
171- ) ;
172- let mut file = tempfile:: Builder :: new ( )
173- . suffix ( ".yaml" )
174- . tempfile ( )
175- . context ( "create provider profile" ) ?;
176- file. write_all ( profile. as_bytes ( ) )
177- . and_then ( |( ) | file. flush ( ) )
178- . context ( "write provider profile" ) ?;
179- Ok ( file)
104+ /// A profile whose `access_token` the gateway refreshes from `keycloak`
105+ /// with the OAuth refresh-token grant.
106+ fn refresh_profile ( id : & str , keycloak : & keycloak_support:: Keycloak ) -> Profile {
107+ let material = |name : & str , secret : bool | proto:: ProviderCredentialRefreshMaterial {
108+ name : name. to_string ( ) ,
109+ required : true ,
110+ secret,
111+ ..proto:: ProviderCredentialRefreshMaterial :: default ( )
112+ } ;
113+ let seconds = |seconds| Some ( prost_types:: Duration { seconds, nanos : 0 } ) ;
114+ Profile :: new ( id)
115+ . display_name ( "Keycloak provider refresh test" )
116+ . credential (
117+ Credential :: new ( "access_token" )
118+ . env_var ( CREDENTIAL_KEY )
119+ . required ( )
120+ . auth_style ( "bearer" )
121+ . header_name ( "authorization" )
122+ . refresh ( proto:: ProviderCredentialRefresh {
123+ strategy : proto:: ProviderCredentialRefreshStrategy :: Oauth2RefreshToken as i32 ,
124+ token_url : keycloak. token_endpoint ( ) ,
125+ scopes : vec ! [ "openid" . to_string( ) ] ,
126+ refresh_before : seconds ( 60 ) ,
127+ max_lifetime : seconds ( 3600 ) ,
128+ material : vec ! [
129+ material( "client_id" , false ) ,
130+ material( "refresh_token" , true ) ,
131+ ] ,
132+ ..proto:: ProviderCredentialRefresh :: default ( )
133+ } ) ,
134+ )
135+ . endpoint ( "keycloak.test.invalid" , 443 , |ep| {
136+ ep. rest ( Access :: ReadOnly ) . enforce ( ) ;
137+ } )
138+ . binaries ( [ "/usr/bin/curl" ] )
180139}
0 commit comments