1717
1818import static io .serverlessworkflow .api .types .OAuth2AuthenticationDataClient .ClientAuthentication .CLIENT_SECRET_POST ;
1919import static io .serverlessworkflow .impl .WorkflowUtils .isValid ;
20- import static io .serverlessworkflow .impl .executors .http .auth . requestbuilder . SecretKeys .AUDIENCES ;
21- import static io .serverlessworkflow .impl .executors .http .auth . requestbuilder . SecretKeys .AUTHENTICATION ;
22- import static io .serverlessworkflow .impl .executors .http .auth . requestbuilder . SecretKeys .CLIENT ;
23- import static io .serverlessworkflow .impl .executors .http .auth . requestbuilder . SecretKeys .ENCODING ;
24- import static io .serverlessworkflow .impl .executors .http .auth . requestbuilder . SecretKeys .REQUEST ;
25- import static io .serverlessworkflow .impl .executors .http .auth . requestbuilder . SecretKeys .SCOPES ;
20+ import static io .serverlessworkflow .impl .executors .http .SecretKeys .AUDIENCES ;
21+ import static io .serverlessworkflow .impl .executors .http .SecretKeys .AUTHENTICATION ;
22+ import static io .serverlessworkflow .impl .executors .http .SecretKeys .CLIENT ;
23+ import static io .serverlessworkflow .impl .executors .http .SecretKeys .ENCODING ;
24+ import static io .serverlessworkflow .impl .executors .http .SecretKeys .REQUEST ;
25+ import static io .serverlessworkflow .impl .executors .http .SecretKeys .SCOPES ;
2626
2727import io .serverlessworkflow .api .types .OAuth2AuthenticationData ;
2828import io .serverlessworkflow .api .types .OAuth2AuthenticationDataClient ;
3737abstract class AbstractAuthRequestBuilder <T extends OAuth2AuthenticationData >
3838 implements AuthRequestBuilder <T > {
3939
40- private static final String DEFAULT_ENCODING = "application/x-www-form-urlencoded; charset=UTF-8" ;
41-
4240 protected final WorkflowApplication application ;
41+ protected final HttpRequestInfoBuilder requestBuilder = new HttpRequestInfoBuilder ();
4342
4443 public AbstractAuthRequestBuilder (WorkflowApplication application ) {
4544 this .application = application ;
4645 }
4746
48- public void accept (HttpRequestBuilder requestBuilder , T authenticationData ) {
49- requestEncoding (requestBuilder , authenticationData );
50- authenticationURI (requestBuilder , authenticationData );
51- audience (requestBuilder , authenticationData );
52- scope (requestBuilder , authenticationData );
53- authenticationMethod (requestBuilder , authenticationData );
47+ @ Override
48+ public HttpRequestInfo apply (T authenticationData ) {
49+ requestEncoding (authenticationData );
50+ authenticationURI (authenticationData );
51+ audience (authenticationData );
52+ scope (authenticationData );
53+ authenticationMethod (authenticationData );
54+ return requestBuilder .build ();
5455 }
5556
5657 @ Override
57- public void accept (HttpRequestBuilder requestBuilder , Map <String , Object > secret ) {
58- requestEncoding (requestBuilder , secret );
59- authenticationURI (requestBuilder , secret );
60- audience (requestBuilder , secret );
61- scope (requestBuilder , secret );
62- authenticationMethod (requestBuilder , secret );
58+ public HttpRequestInfo apply (Map <String , Object > secret ) {
59+ requestEncoding (secret );
60+ authenticationURI (secret );
61+ audience (secret );
62+ scope (secret );
63+ authenticationMethod (secret );
64+ return requestBuilder .build ();
6365 }
6466
65- protected void audience (HttpRequestBuilder requestBuilder , T authenticationData ) {
67+ protected void audience (T authenticationData ) {
6668 if (authenticationData .getAudiences () != null && !authenticationData .getAudiences ().isEmpty ()) {
6769 String audiences = String .join (" " , authenticationData .getAudiences ());
6870 requestBuilder .addQueryParam (
6971 "audience" , WorkflowUtils .buildStringFilter (application , audiences ));
7072 }
7173 }
7274
73- protected void audience (HttpRequestBuilder requestBuilder , Map <String , Object > secret ) {
75+ protected void audience (Map <String , Object > secret ) {
7476 String audiences = (String ) secret .get (AUDIENCES );
7577 if (isValid (audiences )) {
76- requestBuilder .addQueryParam ("audience" , ( w , t , m ) -> audiences );
78+ requestBuilder .addQueryParam ("audience" , audiences );
7779 }
7880 }
7981
80- protected void authenticationMethod (HttpRequestBuilder requestBuilder , T authenticationData ) {
82+ protected void authenticationMethod (T authenticationData ) {
8183 ClientSecretHandler secretHandler ;
8284 switch (getClientAuthentication (authenticationData )) {
8385 case CLIENT_SECRET_BASIC :
84- secretHandler = new ClientSecretBasic (application );
86+ secretHandler = new ClientSecretBasic (application , requestBuilder );
8587 case CLIENT_SECRET_JWT :
8688 throw new UnsupportedOperationException ("Client Secret JWT is not supported yet" );
8789 case PRIVATE_KEY_JWT :
8890 throw new UnsupportedOperationException ("Private Key JWT is not supported yet" );
8991 default :
90- secretHandler = new ClientSecretPost (application );
92+ secretHandler = new ClientSecretPost (application , requestBuilder );
9193 }
92- secretHandler .accept (requestBuilder , authenticationData );
94+ secretHandler .accept (authenticationData );
9395 }
9496
95- protected void authenticationMethod (
96- HttpRequestBuilder requestBuilder , Map <String , Object > secret ) {
97+ protected void authenticationMethod (Map <String , Object > secret ) {
9798 Map <String , Object > client = (Map <String , Object >) secret .get (CLIENT );
9899 ClientSecretHandler secretHandler ;
99100 String auth = (String ) client .get (AUTHENTICATION );
100101 if (auth == null ) {
101- secretHandler = new ClientSecretPost (application );
102+ secretHandler = new ClientSecretPost (application , requestBuilder );
102103 } else {
103104 switch (auth ) {
104105 case "client_secret_basic" :
105- secretHandler = new ClientSecretBasic (application );
106+ secretHandler = new ClientSecretBasic (application , requestBuilder );
106107 break ;
107108 default :
108109 case "client_secret_post" :
109- secretHandler = new ClientSecretPost (application );
110+ secretHandler = new ClientSecretPost (application , requestBuilder );
110111 break ;
111112 case "private_key_jwt" :
112113 throw new UnsupportedOperationException ("Private Key JWT is not supported yet" );
113114 case "client_secret_jwt" :
114115 throw new UnsupportedOperationException ("Client Secret JWT is not supported yet" );
115116 }
116117 }
117- secretHandler .accept (requestBuilder , secret );
118+ secretHandler .accept (secret );
118119 }
119120
120121 private OAuth2AuthenticationDataClient .ClientAuthentication getClientAuthentication (
@@ -125,11 +126,11 @@ private OAuth2AuthenticationDataClient.ClientAuthentication getClientAuthenticat
125126 : authenticationData .getClient ().getAuthentication ();
126127 }
127128
128- protected void scope (HttpRequestBuilder requestBuilder , T authenticationData ) {
129- scope (requestBuilder , authenticationData .getScopes ());
129+ protected void scope (T authenticationData ) {
130+ scope (authenticationData .getScopes ());
130131 }
131132
132- protected void scope (HttpRequestBuilder requestBuilder , List <String > scopesList ) {
133+ protected void scope (List <String > scopesList ) {
133134 if (scopesList == null || scopesList .isEmpty ()) {
134135 return ;
135136 }
@@ -147,28 +148,26 @@ protected void scope(HttpRequestBuilder requestBuilder, List<String> scopesList)
147148 }
148149 }
149150
150- protected void scope (HttpRequestBuilder requestBuilder , Map <String , Object > secret ) {
151+ protected void scope (Map <String , Object > secret ) {
151152 String scopes = (String ) secret .get (SCOPES );
152153 if (isValid (scopes )) {
153- requestBuilder .addQueryParam ("scope" , ( w , t , m ) -> scopes );
154+ requestBuilder .addQueryParam ("scope" , scopes );
154155 }
155156 }
156157
157- void requestEncoding (HttpRequestBuilder requestBuilder , T authenticationData ) {
158- requestBuilder .withRequestContentType (authenticationData .getRequest ());
158+ void requestEncoding (T authenticationData ) {
159+ requestBuilder .withContentType (authenticationData .getRequest ());
159160 }
160161
161- void requestEncoding (HttpRequestBuilder requestBuilder , Map <String , Object > secret ) {
162+ void requestEncoding (Map <String , Object > secret ) {
162163 Map <String , Object > request = (Map <String , Object >) secret .get (REQUEST );
163164 String encoding = (String ) request .get (ENCODING );
164165 if (isValid (encoding )) {
165- requestBuilder .addHeader ("Content-Type" , ( w , t , m ) -> encoding );
166+ requestBuilder .addHeader ("Content-Type" , encoding );
166167 }
167168 }
168169
169- protected abstract void authenticationURI (
170- HttpRequestBuilder requestBuilder , T authenticationData );
170+ protected abstract void authenticationURI (T authenticationData );
171171
172- protected abstract void authenticationURI (
173- HttpRequestBuilder requestBuilder , Map <String , Object > secret );
172+ protected abstract void authenticationURI (Map <String , Object > secret );
174173}
0 commit comments