Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 37 additions & 2 deletions src/main/java/com/darksci/pardot/api/ConfigurationBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,25 @@

package com.darksci.pardot.api;

import java.util.Objects;

import com.darksci.pardot.api.auth.AuthorizationServer;
import com.darksci.pardot.api.auth.PasswordSessionRefreshHandler;
import com.darksci.pardot.api.auth.SessionRefreshHandler;
import com.darksci.pardot.api.auth.SsoAccessTokenSessionRefreshHandler;
import com.darksci.pardot.api.auth.SsoClientCredentialsRefreshHandler;
import com.darksci.pardot.api.auth.SsoRefreshTokenSessionRefreshHandler;
import com.darksci.pardot.api.auth.SsoSessionRefreshHandler;
import com.darksci.pardot.api.config.Configuration;
import com.darksci.pardot.api.config.PasswordLoginCredentials;
import com.darksci.pardot.api.config.ProxyConfiguration;
import com.darksci.pardot.api.config.SsoAccessTokenCredentials;
import com.darksci.pardot.api.config.SsoClientCredentials;
import com.darksci.pardot.api.config.SsoLoginCredentials;
import com.darksci.pardot.api.config.SsoRefreshTokenCredentials;
import com.darksci.pardot.api.rest.interceptor.NoopRequestInterceptor;
import com.darksci.pardot.api.rest.interceptor.RequestInterceptor;

import java.util.Objects;

/**
* Pardot API Client Configuration Builder.
* Used to construct {@link Configuration} instances.
Expand Down Expand Up @@ -118,6 +120,39 @@ public ConfigurationBuilder withSsoLogin(final String username, final String pas
), authorizationServer));
}

/**
* For configuring authenticating to the Pardot API using the client_credentials OAuth2 authentication flow.
*
* @param clientId Connected Application client or consumer Id.
* @param clientSecret Connected Application client or consumer secret.
* @param businessUnitId Id of the Pardot business unit to connect to.
* @return Builder instance.
*/
public ConfigurationBuilder withSsoLogin(final String clientId, final String clientSecret, final String businessUnitId) {
return withCustomAuthenticationHandler(new SsoClientCredentialsRefreshHandler(new SsoClientCredentials(
Objects.requireNonNull(clientId),
Objects.requireNonNull(clientSecret),
Objects.requireNonNull(businessUnitId)
), AuthorizationServer.DEFAULT_SALESFORCE));
}

/**
* For configuring authenticating to the Pardot API using the client_credentials OAuth2 authentication flow.
*
* @param clientId Connected Application client or consumer Id.
* @param clientSecret Connected Application client or consumer secret.
* @param businessUnitId Id of the Pardot business unit to connect to.
* @param authorizationServer Override the authorization server address.
* @return Builder instance.
*/
public ConfigurationBuilder withSsoLogin(final String clientId, final String clientSecret, final String businessUnitId, final AuthorizationServer authorizationServer) {
return withCustomAuthenticationHandler(new SsoClientCredentialsRefreshHandler(new SsoClientCredentials(
Objects.requireNonNull(clientId),
Objects.requireNonNull(clientSecret),
Objects.requireNonNull(businessUnitId)
), authorizationServer));
}

/**
* For configuring authenticating to the Pardot API using a previously acquired refresh_token acquired using
* the access_code OAuth2 authentication flow.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* Copyright 2017, 2018, 2019, 2020 Stephen Powis https://github.com/Crim/pardot-java-client
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.darksci.pardot.api.auth;

import com.darksci.pardot.api.PardotClient;
import com.darksci.pardot.api.config.SsoClientCredentials;
import com.darksci.pardot.api.request.login.SsoLoginRequest;
import com.darksci.pardot.api.response.login.SsoLoginResponse;

import java.util.Objects;

/**
* Handles refreshing credentials using SSO Login method.
*/
public class SsoClientCredentialsRefreshHandler implements SessionRefreshHandler {
private final SsoClientCredentials credentials;
private final AuthorizationServer authorizationServer;

private String apiToken = null;

public SsoClientCredentialsRefreshHandler(final SsoClientCredentials credentials, final AuthorizationServer authorizationServer) {
this.credentials = Objects.requireNonNull(credentials);
this.authorizationServer = Objects.requireNonNull(authorizationServer);
}

@Override
public boolean isValid() {
return apiToken != null;
}

@Override
public void clearToken() {
this.apiToken = null;
}

@Override
public boolean refreshCredentials(final PardotClient client) {
final SsoLoginResponse response = client.login(new SsoLoginRequest(authorizationServer)
.withClientId(credentials.getClientId())
.withClientSecret(credentials.getClientSecret())
.withGrantType("client_credentials")
);

// If we have an API key.
if (response.getAccessToken() != null) {
// Set it.
setApiToken(response.getAccessToken());
return true;
}

return false;
}

@Override
public AuthParameter[] getAuthorizationHeaders() {
if (!isValid()) {
return AuthParameter.EMPTY;
}

final String value = "Bearer " + apiToken;
return new AuthParameter[] {
new AuthParameter("Authorization", value),
new AuthParameter("Pardot-Business-Unit-Id", credentials.getBusinessUnitId())
};
}

@Override
public AuthParameter[] getAuthorizationRequestParameters() {
return AuthParameter.EMPTY;
}

/**
* Used to set ApiToken value.
* @param apiToken value to set.
*/
public void setApiToken(final String apiToken) {
this.apiToken = apiToken;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright 2017, 2018, 2019, 2020 Stephen Powis https://github.com/Crim/pardot-java-client
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

package com.darksci.pardot.api.config;

import java.util.Objects;

/**
* Defines credentials for authenticating to Pardot API using Salesforce SSO Client Credentials flow.
*/
public class SsoClientCredentials {
// Immutable values.
private final String clientId;
private final String clientSecret;
private final String businessUnitId;

/**
* Constructor.
* @param clientId Connected App client or consumer Id.
* @param clientSecret Connected App client or consumer secret.
* @param businessUnitId Pardot Business Unit Id to connect to.
*/
public SsoClientCredentials(
final String clientId,
final String clientSecret,
final String businessUnitId) {

this.clientId = Objects.requireNonNull(clientId);
this.clientSecret = Objects.requireNonNull(clientSecret);
this.businessUnitId = Objects.requireNonNull(businessUnitId);
}

public String getClientId() {
return clientId;
}

public String getClientSecret() {
return clientSecret;
}

public String getBusinessUnitId() {
return businessUnitId;
}

@Override
public String toString() {
return "SsoClientCredentials{"
+ "clientId='" + clientId + '\''
+ ", clientSecret='XXXXXXXXXX'"
+ ", businessUnitId='" + businessUnitId + '\''
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.Objects;

/**
* Defines credentials for authenticating to Pardot API using Salesforce SSO.
* Defines credentials for authenticating to Pardot API using Salesforce SSO Username-Password Flow.
*/
public class SsoLoginCredentials {
// Immutable values.
Expand Down
Loading
Loading