-
Notifications
You must be signed in to change notification settings - Fork 58
Expose auth policy resolution utilities for framework integrations #1512
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
75d4c6f
Create a resolvePolicy to help downstream frameworks
mcruzdev cedbd15
Introduce OAuthPolicyData
mcruzdev 7cc24d9
Guard against null nested configuration in OAuthPolicyData.from
mcruzdev 1bcd170
Fail fast on null workflow in resolvePolicy
mcruzdev 0bf14d4
Consolidate auth utilities into OAuthUtils
mcruzdev 5bfd5e9
Return Optional from resolvePolicy and extract OAuthScheme enum
mcruzdev 00b697b
Extract OAuthPolicyData to a top-level file with a from() factory method
mcruzdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
impl/core/src/main/java/io/serverlessworkflow/impl/auth/OAuthPolicyData.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /* | ||
| * Copyright 2020-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.serverlessworkflow.impl.auth; | ||
|
|
||
| import io.serverlessworkflow.api.types.AuthenticationPolicyUnion; | ||
| import io.serverlessworkflow.api.types.OAuth2AuthenticationData; | ||
| import io.serverlessworkflow.api.types.SecretBasedAuthenticationPolicy; | ||
| import java.util.Optional; | ||
|
|
||
| public record OAuthPolicyData( | ||
| OAuth2AuthenticationData data, SecretBasedAuthenticationPolicy secret, OAuthScheme scheme) { | ||
|
|
||
| public static Optional<OAuthPolicyData> from(AuthenticationPolicyUnion policy) { | ||
| return OAuthUtils.from(policy); | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
impl/core/src/main/java/io/serverlessworkflow/impl/auth/OAuthScheme.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| /* | ||
| * Copyright 2020-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.serverlessworkflow.impl.auth; | ||
|
|
||
| public enum OAuthScheme { | ||
| OAUTH2, | ||
| OPENID_CONNECT | ||
| } |
74 changes: 74 additions & 0 deletions
74
impl/core/src/main/java/io/serverlessworkflow/impl/auth/OAuthUtils.java
|
fjtirado marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * Copyright 2020-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.serverlessworkflow.impl.auth; | ||
|
|
||
| import io.serverlessworkflow.api.types.AuthenticationPolicyUnion; | ||
| import io.serverlessworkflow.api.types.OAuth2AuthenticationPolicy; | ||
| import io.serverlessworkflow.api.types.OAuth2AuthenticationPolicyConfiguration; | ||
| import io.serverlessworkflow.api.types.OpenIdConnectAuthenticationPolicy; | ||
| import io.serverlessworkflow.api.types.OpenIdConnectAuthenticationPolicyConfiguration; | ||
| import io.serverlessworkflow.api.types.ReferenceableAuthenticationPolicy; | ||
| import io.serverlessworkflow.api.types.Use; | ||
| import io.serverlessworkflow.api.types.Workflow; | ||
| import java.util.Optional; | ||
|
|
||
| public class OAuthUtils { | ||
|
|
||
| private OAuthUtils() {} | ||
|
|
||
| public static Optional<OAuthPolicyData> from(AuthenticationPolicyUnion policy) { | ||
| if (policy == null) { | ||
| return Optional.empty(); | ||
| } | ||
| OAuth2AuthenticationPolicy oauth2 = policy.getOAuth2AuthenticationPolicy(); | ||
| if (oauth2 != null) { | ||
| OAuth2AuthenticationPolicyConfiguration config = oauth2.getOauth2(); | ||
| if (config != null) { | ||
| return Optional.of( | ||
| new OAuthPolicyData( | ||
| config.getOAuth2ConnectAuthenticationProperties(), | ||
| config.getOAuth2AuthenticationPolicySecret(), | ||
| OAuthScheme.OAUTH2)); | ||
| } | ||
| } | ||
| OpenIdConnectAuthenticationPolicy oidc = policy.getOpenIdConnectAuthenticationPolicy(); | ||
| if (oidc != null) { | ||
| OpenIdConnectAuthenticationPolicyConfiguration config = oidc.getOidc(); | ||
| if (config != null) { | ||
| return Optional.of( | ||
| new OAuthPolicyData( | ||
| config.getOpenIdConnectAuthenticationProperties(), | ||
| config.getOpenIdConnectAuthenticationPolicySecret(), | ||
| OAuthScheme.OPENID_CONNECT)); | ||
| } | ||
| } | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| public static Optional<AuthenticationPolicyUnion> resolvePolicy( | ||
| Workflow workflow, ReferenceableAuthenticationPolicy auth) { | ||
| if (auth == null) { | ||
| return Optional.empty(); | ||
| } | ||
| if (auth.getAuthenticationPolicyReference() != null) { | ||
| String use = auth.getAuthenticationPolicyReference().getUse(); | ||
| return Optional.ofNullable(workflow.getUse()) | ||
| .map(Use::getAuthentications) | ||
| .map(a -> a.getAdditionalProperties().get(use)); | ||
| } | ||
| return Optional.ofNullable(auth.getAuthenticationPolicy()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
impl/test/src/test/java/io/serverlessworkflow/impl/test/OAuthUtilsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| /* | ||
| * Copyright 2020-Present The Serverless Workflow Specification Authors | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.serverlessworkflow.impl.test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import io.serverlessworkflow.api.types.AuthenticationPolicyUnion; | ||
| import io.serverlessworkflow.api.types.BasicAuthenticationPolicy; | ||
| import io.serverlessworkflow.api.types.OAuth2AuthenticationData; | ||
| import io.serverlessworkflow.api.types.OAuth2AuthenticationPolicy; | ||
| import io.serverlessworkflow.api.types.OAuth2AuthenticationPolicyConfiguration; | ||
| import io.serverlessworkflow.api.types.OAuth2ConnectAuthenticationProperties; | ||
| import io.serverlessworkflow.api.types.OpenIdConnectAuthenticationPolicy; | ||
| import io.serverlessworkflow.api.types.OpenIdConnectAuthenticationPolicyConfiguration; | ||
| import io.serverlessworkflow.api.types.SecretBasedAuthenticationPolicy; | ||
| import io.serverlessworkflow.impl.auth.OAuthPolicyData; | ||
| import io.serverlessworkflow.impl.auth.OAuthScheme; | ||
| import io.serverlessworkflow.impl.auth.OAuthUtils; | ||
| import java.util.Optional; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class OAuthUtilsTest { | ||
|
|
||
| @Test | ||
| void fromNullReturnsEmpty() { | ||
| assertEquals(Optional.empty(), OAuthUtils.from(null)); | ||
| } | ||
|
|
||
| @Test | ||
| void fromNonOAuthPolicyReturnsEmpty() { | ||
| AuthenticationPolicyUnion union = | ||
| new AuthenticationPolicyUnion() | ||
| .withBasicAuthenticationPolicy(new BasicAuthenticationPolicy()); | ||
| assertTrue(OAuthUtils.from(union).isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| void fromOAuth2InlineData() { | ||
| OAuth2ConnectAuthenticationProperties props = new OAuth2ConnectAuthenticationProperties(); | ||
| AuthenticationPolicyUnion union = | ||
| new AuthenticationPolicyUnion() | ||
| .withOAuth2AuthenticationPolicy( | ||
| new OAuth2AuthenticationPolicy() | ||
| .withOauth2( | ||
| new OAuth2AuthenticationPolicyConfiguration() | ||
| .withOAuth2ConnectAuthenticationProperties(props))); | ||
| Optional<OAuthPolicyData> result = OAuthUtils.from(union); | ||
| assertTrue(result.isPresent()); | ||
| OAuthPolicyData data = result.get(); | ||
| assertEquals(OAuthScheme.OAUTH2, data.scheme()); | ||
| assertEquals(props, data.data()); | ||
| assertNull(data.secret()); | ||
| } | ||
|
|
||
| @Test | ||
| void fromOAuth2Secret() { | ||
| SecretBasedAuthenticationPolicy secret = new SecretBasedAuthenticationPolicy("mySecret"); | ||
| AuthenticationPolicyUnion union = | ||
| new AuthenticationPolicyUnion() | ||
| .withOAuth2AuthenticationPolicy( | ||
| new OAuth2AuthenticationPolicy() | ||
| .withOauth2( | ||
| new OAuth2AuthenticationPolicyConfiguration() | ||
| .withOAuth2AuthenticationPolicySecret(secret))); | ||
| Optional<OAuthPolicyData> result = OAuthUtils.from(union); | ||
| assertTrue(result.isPresent()); | ||
| OAuthPolicyData data = result.get(); | ||
| assertEquals(OAuthScheme.OAUTH2, data.scheme()); | ||
| assertNull(data.data()); | ||
| assertEquals(secret, data.secret()); | ||
| } | ||
|
|
||
| @Test | ||
| void fromOidcInlineData() { | ||
| OAuth2AuthenticationData oidcData = new OAuth2AuthenticationData(); | ||
| AuthenticationPolicyUnion union = | ||
| new AuthenticationPolicyUnion() | ||
| .withOpenIdConnectAuthenticationPolicy( | ||
| new OpenIdConnectAuthenticationPolicy() | ||
| .withOidc( | ||
| new OpenIdConnectAuthenticationPolicyConfiguration() | ||
| .withOpenIdConnectAuthenticationProperties(oidcData))); | ||
| Optional<OAuthPolicyData> result = OAuthUtils.from(union); | ||
| assertTrue(result.isPresent()); | ||
| OAuthPolicyData data = result.get(); | ||
| assertEquals(OAuthScheme.OPENID_CONNECT, data.scheme()); | ||
| assertEquals(oidcData, data.data()); | ||
| assertNull(data.secret()); | ||
| } | ||
|
|
||
| @Test | ||
| void fromOidcSecret() { | ||
| SecretBasedAuthenticationPolicy secret = new SecretBasedAuthenticationPolicy("oidcSecret"); | ||
| AuthenticationPolicyUnion union = | ||
| new AuthenticationPolicyUnion() | ||
| .withOpenIdConnectAuthenticationPolicy( | ||
| new OpenIdConnectAuthenticationPolicy() | ||
| .withOidc( | ||
| new OpenIdConnectAuthenticationPolicyConfiguration() | ||
| .withOpenIdConnectAuthenticationPolicySecret(secret))); | ||
| Optional<OAuthPolicyData> result = OAuthUtils.from(union); | ||
| assertTrue(result.isPresent()); | ||
| OAuthPolicyData data = result.get(); | ||
| assertEquals(OAuthScheme.OPENID_CONNECT, data.scheme()); | ||
| assertNull(data.data()); | ||
| assertEquals(secret, data.secret()); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.