|
| 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 | +} |
0 commit comments