|
| 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.impl.executors.http.oauth; |
| 17 | + |
| 18 | +import static io.serverlessworkflow.api.types.OAuth2AutenthicationData.OAuth2AutenthicationDataGrant.CLIENT_CREDENTIALS; |
| 19 | +import static io.serverlessworkflow.api.types.OAuth2AutenthicationData.OAuth2AutenthicationDataGrant.PASSWORD; |
| 20 | + |
| 21 | +import io.serverlessworkflow.api.types.OAuth2AutenthicationData; |
| 22 | +import io.serverlessworkflow.api.types.Oauth2; |
| 23 | +import java.util.Base64; |
| 24 | + |
| 25 | +class ClientSecretBasic { |
| 26 | + |
| 27 | + private final Oauth2 oauth2; |
| 28 | + |
| 29 | + public ClientSecretBasic(Oauth2 oauth2) { |
| 30 | + this.oauth2 = oauth2; |
| 31 | + } |
| 32 | + |
| 33 | + public void execute(HttpRequestBuilder requestBuilder) { |
| 34 | + OAuth2AutenthicationData authenticationData = |
| 35 | + oauth2.getOAuth2ConnectAuthenticationProperties().getOAuth2AutenthicationData(); |
| 36 | + if (authenticationData.getGrant().equals(PASSWORD)) { |
| 37 | + password(requestBuilder, authenticationData); |
| 38 | + } else if (authenticationData.getGrant().equals(CLIENT_CREDENTIALS)) { |
| 39 | + clientCredentials(requestBuilder, authenticationData); |
| 40 | + } else { |
| 41 | + throw new UnsupportedOperationException( |
| 42 | + "Unsupported grant type: " + authenticationData.getGrant()); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private void clientCredentials( |
| 47 | + HttpRequestBuilder requestBuilder, OAuth2AutenthicationData authenticationData) { |
| 48 | + if (authenticationData.getClient() == null |
| 49 | + || authenticationData.getClient().getId() == null |
| 50 | + || authenticationData.getClient().getSecret() == null) { |
| 51 | + throw new IllegalArgumentException( |
| 52 | + "Client ID and secret must be provided for client authentication"); |
| 53 | + } |
| 54 | + |
| 55 | + String idAndSecret = |
| 56 | + authenticationData.getClient().getId() + ":" + authenticationData.getClient().getSecret(); |
| 57 | + String encodedAuth = Base64.getEncoder().encodeToString(idAndSecret.getBytes()); |
| 58 | + |
| 59 | + requestBuilder |
| 60 | + .addHeader("Authorization", "Basic " + encodedAuth) |
| 61 | + .withRequestContentType(authenticationData.getRequest()) |
| 62 | + .withGrantType(authenticationData.getGrant()); |
| 63 | + } |
| 64 | + |
| 65 | + private void password( |
| 66 | + HttpRequestBuilder requestBuilder, OAuth2AutenthicationData authenticationData) { |
| 67 | + if (authenticationData.getUsername() == null || authenticationData.getPassword() == null) { |
| 68 | + throw new IllegalArgumentException( |
| 69 | + "Username and password must be provided for password grant type"); |
| 70 | + } |
| 71 | + if (authenticationData.getClient() == null |
| 72 | + || authenticationData.getClient().getId() == null |
| 73 | + || authenticationData.getClient().getSecret() == null) { |
| 74 | + throw new IllegalArgumentException( |
| 75 | + "Client ID and secret must be provided for client authentication"); |
| 76 | + } |
| 77 | + |
| 78 | + String idAndSecret = |
| 79 | + authenticationData.getClient().getId() + ":" + authenticationData.getClient().getSecret(); |
| 80 | + String encodedAuth = Base64.getEncoder().encodeToString(idAndSecret.getBytes()); |
| 81 | + |
| 82 | + requestBuilder |
| 83 | + .withGrantType(authenticationData.getGrant()) |
| 84 | + .withRequestContentType(authenticationData.getRequest()) |
| 85 | + .addHeader("Authorization", "Basic " + encodedAuth) |
| 86 | + .addQueryParam("username", authenticationData.getUsername()) |
| 87 | + .addQueryParam("password", authenticationData.getPassword()); |
| 88 | + } |
| 89 | +} |
0 commit comments