-
Notifications
You must be signed in to change notification settings - Fork 50
Add OAuth authentication support to HTTP call task #718
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
+2,142
−8
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
543b838
HttpCallTask implementation lack OAuth authentication support
treblereel 240ac32
tests
treblereel 26c32d8
post review
treblereel 6f56995
post review
treblereel 0f358b6
tests and fixies
treblereel f367116
one more fix
treblereel 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
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
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
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
62 changes: 62 additions & 0 deletions
62
...tp/src/main/java/io/serverlessworkflow/impl/executors/http/oauth/AccessTokenProvider.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,62 @@ | ||
/* | ||
* 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.executors.http.oauth; | ||
|
||
import io.serverlessworkflow.http.jwt.JWT; | ||
import io.serverlessworkflow.http.jwt.JWTConverter; | ||
import io.serverlessworkflow.impl.TaskContext; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.ServiceLoader; | ||
|
||
public class AccessTokenProvider { | ||
|
||
private final TokenResponseHandler tokenResponseHandler = new TokenResponseHandler(); | ||
|
||
private final TaskContext context; | ||
private final List<String> issuers; | ||
private final InvocationHolder invocation; | ||
|
||
private final JWTConverter jwtConverter; | ||
|
||
AccessTokenProvider(InvocationHolder invocation, TaskContext context, List<String> issuers) { | ||
this.invocation = invocation; | ||
this.issuers = issuers; | ||
this.context = context; | ||
|
||
this.jwtConverter = | ||
ServiceLoader.load(JWTConverter.class) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalStateException("No JWTConverter implementation found")); | ||
} | ||
|
||
public JWT validateAndGet() { | ||
Map<String, Object> token = tokenResponseHandler.apply(invocation, context); | ||
JWT jwt; | ||
try { | ||
jwt = jwtConverter.fromToken((String) token.get("access_token")); | ||
} catch (IllegalArgumentException e) { | ||
throw new IllegalStateException("Failed to parse JWT token: " + e.getMessage(), e); | ||
} | ||
if (!(issuers == null || issuers.isEmpty())) { | ||
String tokenIssuer = (String) jwt.getClaim("iss"); | ||
if (tokenIssuer == null || tokenIssuer.isEmpty() || !issuers.contains(tokenIssuer)) { | ||
throw new IllegalStateException("Token issuer is not valid: " + tokenIssuer); | ||
} | ||
} | ||
return jwt; | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...http/src/main/java/io/serverlessworkflow/impl/executors/http/oauth/ClientSecretBasic.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,89 @@ | ||
/* | ||
* 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.executors.http.oauth; | ||
|
||
import static io.serverlessworkflow.api.types.OAuth2AutenthicationData.OAuth2AutenthicationDataGrant.CLIENT_CREDENTIALS; | ||
import static io.serverlessworkflow.api.types.OAuth2AutenthicationData.OAuth2AutenthicationDataGrant.PASSWORD; | ||
|
||
import io.serverlessworkflow.api.types.OAuth2AutenthicationData; | ||
import io.serverlessworkflow.api.types.Oauth2; | ||
import java.util.Base64; | ||
|
||
class ClientSecretBasic { | ||
|
||
private final Oauth2 oauth2; | ||
|
||
public ClientSecretBasic(Oauth2 oauth2) { | ||
this.oauth2 = oauth2; | ||
} | ||
|
||
public void execute(HttpRequestBuilder requestBuilder) { | ||
OAuth2AutenthicationData authenticationData = | ||
oauth2.getOAuth2ConnectAuthenticationProperties().getOAuth2AutenthicationData(); | ||
if (authenticationData.getGrant().equals(PASSWORD)) { | ||
password(requestBuilder, authenticationData); | ||
} else if (authenticationData.getGrant().equals(CLIENT_CREDENTIALS)) { | ||
clientCredentials(requestBuilder, authenticationData); | ||
} else { | ||
throw new UnsupportedOperationException( | ||
"Unsupported grant type: " + authenticationData.getGrant()); | ||
} | ||
} | ||
|
||
private void clientCredentials( | ||
HttpRequestBuilder requestBuilder, OAuth2AutenthicationData authenticationData) { | ||
if (authenticationData.getClient() == null | ||
|| authenticationData.getClient().getId() == null | ||
|| authenticationData.getClient().getSecret() == null) { | ||
throw new IllegalArgumentException( | ||
"Client ID and secret must be provided for client authentication"); | ||
} | ||
|
||
String idAndSecret = | ||
authenticationData.getClient().getId() + ":" + authenticationData.getClient().getSecret(); | ||
String encodedAuth = Base64.getEncoder().encodeToString(idAndSecret.getBytes()); | ||
|
||
requestBuilder | ||
.addHeader("Authorization", "Basic " + encodedAuth) | ||
.withRequestContentType(authenticationData.getRequest()) | ||
.withGrantType(authenticationData.getGrant()); | ||
} | ||
|
||
private void password( | ||
HttpRequestBuilder requestBuilder, OAuth2AutenthicationData authenticationData) { | ||
if (authenticationData.getUsername() == null || authenticationData.getPassword() == null) { | ||
throw new IllegalArgumentException( | ||
"Username and password must be provided for password grant type"); | ||
} | ||
if (authenticationData.getClient() == null | ||
|| authenticationData.getClient().getId() == null | ||
|| authenticationData.getClient().getSecret() == null) { | ||
throw new IllegalArgumentException( | ||
"Client ID and secret must be provided for client authentication"); | ||
} | ||
|
||
String idAndSecret = | ||
authenticationData.getClient().getId() + ":" + authenticationData.getClient().getSecret(); | ||
String encodedAuth = Base64.getEncoder().encodeToString(idAndSecret.getBytes()); | ||
|
||
requestBuilder | ||
.withGrantType(authenticationData.getGrant()) | ||
.withRequestContentType(authenticationData.getRequest()) | ||
.addHeader("Authorization", "Basic " + encodedAuth) | ||
.addQueryParam("username", authenticationData.getUsername()) | ||
.addQueryParam("password", authenticationData.getPassword()); | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
...p/src/main/java/io/serverlessworkflow/impl/executors/http/oauth/ClientSecretPostStep.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,82 @@ | ||
/* | ||
* 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.executors.http.oauth; | ||
|
||
import static io.serverlessworkflow.api.types.OAuth2AutenthicationData.OAuth2AutenthicationDataGrant.CLIENT_CREDENTIALS; | ||
import static io.serverlessworkflow.api.types.OAuth2AutenthicationData.OAuth2AutenthicationDataGrant.PASSWORD; | ||
|
||
import io.serverlessworkflow.api.types.OAuth2AutenthicationData; | ||
import io.serverlessworkflow.api.types.Oauth2; | ||
|
||
class ClientSecretPostStep { | ||
private final Oauth2 oauth2; | ||
|
||
public ClientSecretPostStep(Oauth2 oauth2) { | ||
this.oauth2 = oauth2; | ||
} | ||
|
||
public void execute(HttpRequestBuilder requestBuilder) { | ||
OAuth2AutenthicationData authenticationData = | ||
oauth2.getOAuth2ConnectAuthenticationProperties().getOAuth2AutenthicationData(); | ||
|
||
if (authenticationData.getGrant().equals(PASSWORD)) { | ||
password(requestBuilder, authenticationData); | ||
} else if (authenticationData.getGrant().equals(CLIENT_CREDENTIALS)) { | ||
clientCredentials(requestBuilder, authenticationData); | ||
} else { | ||
throw new UnsupportedOperationException( | ||
"Unsupported grant type: " + authenticationData.getGrant()); | ||
} | ||
} | ||
|
||
private void clientCredentials( | ||
HttpRequestBuilder requestBuilder, OAuth2AutenthicationData authenticationData) { | ||
if (authenticationData.getClient() == null | ||
|| authenticationData.getClient().getId() == null | ||
|| authenticationData.getClient().getSecret() == null) { | ||
throw new IllegalArgumentException( | ||
"Client ID and secret must be provided for client authentication"); | ||
} | ||
|
||
requestBuilder | ||
.withGrantType(authenticationData.getGrant()) | ||
.withRequestContentType(authenticationData.getRequest()) | ||
.addQueryParam("client_id", authenticationData.getClient().getId()) | ||
.addQueryParam("client_secret", authenticationData.getClient().getSecret()); | ||
} | ||
|
||
private void password( | ||
HttpRequestBuilder requestBuilder, OAuth2AutenthicationData authenticationData) { | ||
if (authenticationData.getUsername() == null || authenticationData.getPassword() == null) { | ||
throw new IllegalArgumentException( | ||
"Username and password must be provided for password grant type"); | ||
} | ||
if (authenticationData.getClient() == null | ||
|| authenticationData.getClient().getId() == null | ||
|| authenticationData.getClient().getSecret() == null) { | ||
throw new IllegalArgumentException( | ||
"Client ID and secret must be provided for client authentication"); | ||
} | ||
|
||
requestBuilder | ||
.withGrantType(authenticationData.getGrant()) | ||
.withRequestContentType(authenticationData.getRequest()) | ||
.addQueryParam("client_id", authenticationData.getClient().getId()) | ||
.addQueryParam("client_secret", authenticationData.getClient().getSecret()) | ||
.addQueryParam("username", authenticationData.getUsername()) | ||
.addQueryParam("password", authenticationData.getPassword()); | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not letting the Illegal argument exception to escalate?. Unless Im missing something, adding an additional exception here, in my opinion, only pollutes the exception stack trace witn an additional one