Skip to content

Commit 2befe39

Browse files
authored
Improve OAuth2 DSL (FuncDSL and DSL) (#1452)
* Improve OAuth2 DSL (FuncDSL and DSL) Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com> * Add integration tests to oauth2 and use.authentications Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com> * Apply pull request suggestions Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com> --------- Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com>
1 parent eef3bed commit 2befe39

7 files changed

Lines changed: 519 additions & 28 deletions

File tree

experimental/fluent/func/src/main/java/io/serverlessworkflow/fluent/func/dsl/FuncDSL.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import io.serverlessworkflow.fluent.func.configurers.SwitchCaseConfigurer;
4040
import io.serverlessworkflow.fluent.spec.AbstractEventConsumptionStrategyBuilder;
4141
import io.serverlessworkflow.fluent.spec.EventFilterBuilder;
42+
import io.serverlessworkflow.fluent.spec.OAuth2AuthenticationPolicyBuilder;
43+
import io.serverlessworkflow.fluent.spec.OAuth2AuthenticationPolicyBuilder.OAuth2AuthenticationPropertiesEndpointsBuilder;
4244
import io.serverlessworkflow.fluent.spec.ScheduleBuilder;
4345
import io.serverlessworkflow.fluent.spec.TimeoutBuilder;
4446
import io.serverlessworkflow.fluent.spec.WorkflowTaskBuilder;
@@ -2587,4 +2589,24 @@ public static AuthenticationConfigurer oauth2(
25872589
public static AuthenticationConfigurer oauth2(String secret) {
25882590
return DSL.oauth2(secret);
25892591
}
2592+
2593+
/**
2594+
* @see DSL#oauth2(String, OAuth2AuthenticationData.OAuth2AuthenticationDataGrant, String, String,
2595+
* Consumer)
2596+
*/
2597+
public static AuthenticationConfigurer oauth2(
2598+
String authority,
2599+
OAuth2AuthenticationData.OAuth2AuthenticationDataGrant grant,
2600+
String clientId,
2601+
String clientSecret,
2602+
Consumer<OAuth2AuthenticationPropertiesEndpointsBuilder> endpoints) {
2603+
return DSL.oauth2(authority, grant, clientId, clientSecret, endpoints);
2604+
}
2605+
2606+
/**
2607+
* @see DSL#oauth2(Consumer)
2608+
*/
2609+
public static AuthenticationConfigurer oauth2(Consumer<OAuth2AuthenticationPolicyBuilder> cfg) {
2610+
return DSL.oauth2(cfg);
2611+
}
25902612
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverlessworkflow.fluent.func;
17+
18+
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.call;
19+
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.http;
20+
import static io.serverlessworkflow.fluent.func.dsl.FuncDSL.oauth2;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertNotNull;
23+
24+
import io.serverlessworkflow.api.types.OAuth2AuthenticationData;
25+
import io.serverlessworkflow.api.types.OAuth2AuthenticationDataClient;
26+
import io.serverlessworkflow.api.types.OAuth2ConnectAuthenticationProperties;
27+
import io.serverlessworkflow.api.types.Workflow;
28+
import java.net.URI;
29+
import java.util.List;
30+
import org.junit.jupiter.api.Test;
31+
32+
class FuncDSLOAuth2Test {
33+
34+
private static final String EXPR_ENDPOINT = "${ .endpoint }";
35+
36+
private static OAuth2ConnectAuthenticationProperties oauth2PropertiesOf(Workflow wf) {
37+
var auth =
38+
wf.getDo()
39+
.get(0)
40+
.getTask()
41+
.getCallTask()
42+
.getCallHTTP()
43+
.getWith()
44+
.getEndpoint()
45+
.getEndpointConfiguration()
46+
.getAuthentication()
47+
.getAuthenticationPolicy();
48+
assertNotNull(auth.getOAuth2AuthenticationPolicy());
49+
return auth.getOAuth2AuthenticationPolicy()
50+
.getOauth2()
51+
.getOAuth2ConnectAuthenticationProperties();
52+
}
53+
54+
@Test
55+
void convenience_overload_sets_token_endpoint() {
56+
Workflow wf =
57+
FuncWorkflowBuilder.workflow("oauth2-token")
58+
.tasks(
59+
call(
60+
http()
61+
.POST()
62+
.endpoint(
63+
EXPR_ENDPOINT,
64+
oauth2(
65+
"https://auth.example.com/",
66+
OAuth2AuthenticationData.OAuth2AuthenticationDataGrant
67+
.CLIENT_CREDENTIALS,
68+
"client-id",
69+
"client-secret",
70+
e -> e.token("/custom/token")))))
71+
.build();
72+
73+
var props = oauth2PropertiesOf(wf);
74+
assertEquals(URI.create("https://auth.example.com/"), props.getAuthority().getLiteralUri());
75+
assertEquals(
76+
OAuth2AuthenticationData.OAuth2AuthenticationDataGrant.CLIENT_CREDENTIALS,
77+
props.getGrant());
78+
assertEquals("client-id", props.getClient().getId());
79+
assertEquals("client-secret", props.getClient().getSecret());
80+
assertEquals("/custom/token", props.getEndpoints().getToken());
81+
}
82+
83+
@Test
84+
void builder_overload_supports_full_oauth2_section() {
85+
Workflow wf =
86+
FuncWorkflowBuilder.workflow("oauth2-full")
87+
.tasks(
88+
call(
89+
http()
90+
.GET()
91+
.endpoint(
92+
EXPR_ENDPOINT,
93+
oauth2(
94+
o ->
95+
o.endpoints(
96+
e ->
97+
e.token("/oauth2/token")
98+
.revocation("/oauth2/revoke")
99+
.introspection("/oauth2/introspect"))
100+
.authority("https://auth.example.com/")
101+
.grant(
102+
OAuth2AuthenticationData.OAuth2AuthenticationDataGrant
103+
.CLIENT_CREDENTIALS)
104+
.scopes("read", "write")
105+
.audiences("api://default")
106+
.client(
107+
c ->
108+
c.id("client-id")
109+
.secret("client-secret")
110+
.authentication(
111+
OAuth2AuthenticationDataClient
112+
.ClientAuthentication
113+
.CLIENT_SECRET_BASIC))))))
114+
.build();
115+
116+
var props = oauth2PropertiesOf(wf);
117+
assertEquals(URI.create("https://auth.example.com/"), props.getAuthority().getLiteralUri());
118+
assertEquals(
119+
OAuth2AuthenticationData.OAuth2AuthenticationDataGrant.CLIENT_CREDENTIALS,
120+
props.getGrant());
121+
assertEquals(List.of("read", "write"), props.getScopes());
122+
assertEquals(List.of("api://default"), props.getAudiences());
123+
assertEquals("client-id", props.getClient().getId());
124+
assertEquals(
125+
OAuth2AuthenticationDataClient.ClientAuthentication.CLIENT_SECRET_BASIC,
126+
props.getClient().getAuthentication());
127+
assertEquals("/oauth2/token", props.getEndpoints().getToken());
128+
assertEquals("/oauth2/revoke", props.getEndpoints().getRevocation());
129+
assertEquals("/oauth2/introspect", props.getEndpoints().getIntrospection());
130+
}
131+
}

experimental/test/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@
7979
<version>${project.version}</version>
8080
<scope>test</scope>
8181
</dependency>
82+
<dependency>
83+
<groupId>io.serverlessworkflow</groupId>
84+
<artifactId>serverlessworkflow-impl-jackson-jwt</artifactId>
85+
<version>${project.version}</version>
86+
<scope>test</scope>
87+
</dependency>
8288
<dependency>
8389
<groupId>org.glassfish.jersey.media</groupId>
8490
<artifactId>jersey-media-json-jackson</artifactId>

0 commit comments

Comments
 (0)