Skip to content

Commit

Permalink
GH-186 moving the credentialId back to the Workspace where it bel…
Browse files Browse the repository at this point in the history
…ongs
  • Loading branch information
francoisledroff committed Aug 29, 2024
1 parent 4bf8896 commit 905e2d5
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ private Map<String, String> getAuthConfigMap(
putIfNotBlank(map, Workspace.IMS_URL, config.aio_ims_url());
putIfNotBlank(map, Workspace.PROJECT_ID, config.aio_project_id());
putIfNotBlank(map, Workspace.WORKSPACE_ID, config.aio_workspace_id());
putIfNotBlank(map, Workspace.CREDENTIAL_ID, config.aio_credential_id());

putIfNotBlank(map, Context.CLIENT_SECRET, config.aio_client_secret());

putIfNotBlank(map, JwtContext.CREDENTIAL_ID, config.aio_credential_id());

putIfNotBlank(map, JwtContext.TECHNICAL_ACCOUNT_ID, config.aio_technical_account_id());
putIfNotBlank(map, JwtContext.META_SCOPES, config.aio_meta_scopes());
putIfNotBlank(map, PrivateKeyBuilder.AIO_ENCODED_PKCS_8, config.aio_encoded_pkcs8());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
description = "Adobe I/O API Key (Client ID) as shown in in your Adobe Developer Console workspace")
String aio_api_key();

@AttributeDefinition(name = "Credential ID (For deprecated JWT Auth only)",
@AttributeDefinition(name = "Credential ID",
description = "Adobe I/O Credential ID as shown in your Adobe Developer Console workspace (project.workspace.details.credentials.id)")
String aio_credential_id();

Expand Down
3 changes: 2 additions & 1 deletion core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ The `Workspace` POJO holds your Adobe Developer Console Project configurations
* `aio_consumer_org_id` your Adobe Developer Console consumer orgnaization id (`project.org.id`)
* `aio_ims_org_id` your Adobe Developer Console IMS Organization ID (`project.org.ims_org_id`)
* `aio_workspace_id` your Adobe Developer Console workspace Id (`project.workspace.id`)
* `aio_credential_id` your Adobe Developer Console credential id (`project.workspace.details.credentials[i].id`)
* this is optional, but it might be handy to have it in your `Workspace` POJO, to avoid confusion when you have multiple credentials, and to eventually in some Adobe API calls

### Workspace Authentication Context
The `Workspace` POJO must also hold your Adobe Developer Auth configurations, pick one of the following authentication methods (see [aio-lib-java-ims](../ims/README.md) docs for more details):
Expand All @@ -37,7 +39,6 @@ For [OAuth2 authentication](https://developer.adobe.com/developer-console/docs/g

#### JWT authentication
For [JWT authentication](https://developer.adobe.com/developer-console/docs/guides/authentication/ServerToServerAuthentication/#service-account-jwt-credential-deprecated), you will need to provide the following properties:
* `aio_credential_id` your Adobe Developer Console jwt credential id (`project.workspace.details.credentials[i].id`)
* `aio_client_secret` your Adobe Developer Console jwt credential client secret (`project.workspace.details.credentials[i].jwt.client_secret`)
* `aio_api_key` your Adobe Developer Console jwt credential API Key (or Client ID) (`project.workspace.details.credentials[i].jwt.client_id`)
* `aio_meta_scopes` a comma separated list of metascopes associated with your API, see your Adobe Developer Console jwt credential metascopes (`project.workspace.details.credentials[i].jwt.meta_scopes`)
Expand Down
28 changes: 4 additions & 24 deletions core/src/main/java/com/adobe/aio/auth/JwtContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,22 @@
import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
* JWT Authentication context.
*/
public class JwtContext implements Context {

private static final Logger logger = LoggerFactory.getLogger(JwtContext.class);
public static final String CREDENTIAL_ID = "aio_credential_id";
public static final String TECHNICAL_ACCOUNT_ID = "aio_technical_account_id";
public static final String META_SCOPES = "aio_meta_scopes";

private final String credentialId;
private final String technicalAccountId;
private final Set<String> metascopes;
private final String clientSecret;
private final PrivateKey privateKey;

public JwtContext(final String credentialId, final String clientSecret, final String technicalAccountId,
public JwtContext(final String clientSecret, final String technicalAccountId,
final Set<String> metascopes, final PrivateKey privateKey) {
this.credentialId = credentialId;
this.clientSecret = clientSecret;
this.technicalAccountId = technicalAccountId;
this.metascopes = metascopes;
Expand All @@ -67,10 +60,6 @@ public void validate() {
}
}

public String getCredentialId() {
return credentialId;
}

public String getTechnicalAccountId() {
return technicalAccountId;
}
Expand Down Expand Up @@ -106,7 +95,6 @@ public boolean equals(Object o) {

JwtContext that = (JwtContext) o;

if (!Objects.equals(credentialId, that.credentialId)) return false;
if (!Objects.equals(technicalAccountId, that.technicalAccountId))
return false;
if (!Objects.equals(metascopes, that.metascopes)) return false;
Expand All @@ -116,8 +104,7 @@ public boolean equals(Object o) {

@Override
public int hashCode() {
int result = credentialId != null ? credentialId.hashCode() : 0;
result = 31 * result + (technicalAccountId != null ? technicalAccountId.hashCode() : 0);
int result = technicalAccountId != null ? technicalAccountId.hashCode() : 0;
result = 31 * result + (metascopes != null ? metascopes.hashCode() : 0);
result = 31 * result + (clientSecret != null ? clientSecret.hashCode() : 0);
result = 31 * result + (privateKey != null ? privateKey.hashCode() : 0);
Expand All @@ -127,25 +114,18 @@ public int hashCode() {
@Override
public String toString() {
return "JwtContext{" +
"credentialId='" + credentialId + '\'' +
", technicalAccountId='" + technicalAccountId + '\'' +
"technicalAccountId='" + technicalAccountId + '\'' +
", metascopes=" + metascopes +
'}';
}

public static class Builder {

private String credentialId;
private String clientSecret;
private String technicalAccountId;
private PrivateKey privateKey;
private final Set<String> metascopes = new HashSet<>();

public Builder credentialId(final String credentialId) {
this.credentialId = credentialId;
return this;
}

public Builder clientSecret(final String clientSecret) {
this.clientSecret = clientSecret;
return this;
Expand All @@ -167,7 +147,7 @@ public Builder privateKey(final PrivateKey privateKey) {
}

public JwtContext build() {
return new JwtContext(credentialId, clientSecret, technicalAccountId, metascopes, privateKey);
return new JwtContext(clientSecret, technicalAccountId, metascopes, privateKey);
}
}
}
21 changes: 18 additions & 3 deletions core/src/main/java/com/adobe/aio/workspace/Workspace.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,27 @@ public class Workspace {
public static final String PROJECT_ID = "aio_project_id";
public static final String WORKSPACE_ID = "aio_workspace_id";
public static final String API_KEY = "aio_api_key";
public static final String CREDENTIAL_ID = "aio_credential_id";

private final String imsUrl;
private final String imsOrgId;
private final String apiKey;
private final String consumerOrgId;
private final String projectId;
private final String workspaceId;
private final String credentialId;
private final Context authContext;

private Workspace(final String imsUrl, final String imsOrgId, final String apiKey,
final String consumerOrgId, final String projectId, final String workspaceId,
Context authContext) {
final String credentialId, Context authContext) {
this.imsUrl = StringUtils.isEmpty(imsUrl) ? Constants.PROD_IMS_URL : imsUrl;
this.imsOrgId = imsOrgId;

this.apiKey = apiKey;
this.consumerOrgId = consumerOrgId;
this.projectId = projectId;
this.workspaceId = workspaceId;
this.credentialId = credentialId;
this.authContext = authContext;
}

Expand Down Expand Up @@ -83,6 +85,11 @@ public void validateWorkspaceContext() throws IllegalStateException {
if (StringUtils.isEmpty(this.getWorkspaceId())) {
throw new IllegalStateException("Your `Workspace` is missing a workspaceId");
}
// note that the credentialId is optional
// but it might be handy to have it in your `Workspace` POJO,
// to avoid confusion when you have multiple credentials,
// and to eventually in some Adobe API calls

if (authContext == null) {
throw new IllegalStateException("Missing auth configuration ...");
}
Expand Down Expand Up @@ -122,6 +129,8 @@ public String getWorkspaceId() {
return workspaceId;
}

public String getCredentialId() { return credentialId;}

public Context getAuthContext() {
return authContext;
}
Expand Down Expand Up @@ -175,6 +184,7 @@ public static class Builder {
private String consumerOrgId;
private String projectId;
private String workspaceId;
private String credentialId;

private Map<String, String> workspaceProperties;

Expand Down Expand Up @@ -213,13 +223,18 @@ public Builder workspaceId(final String workspaceId) {
return this;
}

public Builder credentialId(final String credentialId) {
this.credentialId = credentialId;
return this;
}

public Builder authContext(final Context authContext) {
this.authContext = authContext;
return this;
}

public Workspace build() {
return new Workspace(imsUrl, imsOrgId, apiKey, consumerOrgId, projectId, workspaceId, authContext);
return new Workspace(imsUrl, imsOrgId, apiKey, consumerOrgId, projectId, workspaceId, credentialId, authContext);
}

}
Expand Down
4 changes: 2 additions & 2 deletions core/src/test/resources/workspace.jwt.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ aio_consumer_org_id=aio_consumer_org_id_changeMe
aio_ims_org_id=aio_ims_org_id_changeMe
# aio_workspace_id = your Adobe Developer Console workspace Id (project.workspace.id)
aio_workspace_id=aio_workspace_id_changeMe
# aio_credential_id = your Adobe Developer Console credential id (project.workspace.details.credentials[i].id)
aio_credential_id=aio_credential_id_changeMe

# aio_api_key = your Adobe Developer Console jwt credential API Key (or Client ID) (project.workspace.details.credentials[i].jwt.client_id
aio_api_key=aio_api_key_changeMe
# aio_credential_id = your Adobe Developer Console jwt credential id (project.workspace.details.credentials[i].id)
aio_credential_id=aio_credential_id_changeMe
# aio_client_secret = your Adobe Developer Console jwt or OAuth credential client secret (project.workspace.details.credentials[i].jwt.client_secret)
aio_client_secret=aio_client_secret_changeMe
# aio_meta_scopes : comma separated list of metascopes associated with your API, see your Adobe Developer Console jwt credential metascopes (project.workspace.details.credentials[i].jwt.meta_scopes)
Expand Down
2 changes: 2 additions & 0 deletions core/src/test/resources/workspace.oauth.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ aio_consumer_org_id=aio_consumer_org_id_changeMe
aio_ims_org_id=aio_ims_org_id_changeMe
# aio_workspace_id = your Adobe Developer Console workspace Id (project.workspace.id)
aio_workspace_id=aio_workspace_id_changeMe
# aio_credential_id = your Adobe Developer Console credential id (project.workspace.details.credentials[i].id)
aio_credential_id=aio_credential_id_changeMe

# aio_api_key = your Adobe Developer Console credential API Key (or Client ID) (project.workspace.details.credentials[i].oauth_server_to_server.client_id)
aio_api_key=aio_api_key_changeMe
Expand Down
6 changes: 3 additions & 3 deletions ims/src/main/java/com/adobe/aio/util/WorkspaceUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class WorkspaceUtil {
* We do provide a sample properties files in the
* <code>./src/test/resources</code> folder
*/
public static final String DEFAULT_TEST_PROPERTIES = "workspace.oauth.secret.properties";
public static final String DEFAULT_TEST_PROPERTIES = "workspace.secret.properties";

private static final Logger logger = LoggerFactory.getLogger(WorkspaceUtil.class);

Expand Down Expand Up @@ -70,7 +70,8 @@ public static Workspace.Builder getWorkspaceBuilder(Map<String, String> configMa
.apiKey(configMap.get(API_KEY))
.consumerOrgId(configMap.get(CONSUMER_ORG_ID))
.projectId(configMap.get(PROJECT_ID))
.workspaceId(configMap.get(WORKSPACE_ID));
.workspaceId(configMap.get(WORKSPACE_ID))
.credentialId(configMap.get(CREDENTIAL_ID));
builder.authContext(getAuthContext(configMap));
return builder;
}
Expand Down Expand Up @@ -99,7 +100,6 @@ public static OAuthContext.Builder getOAuthContextBuilder(Map<String, String> co

public static JwtContext.Builder getJwtContextBuilder(Map<String, String> configMap) {
JwtContext.Builder builder = new JwtContext.Builder()
.credentialId(configMap.get(CREDENTIAL_ID))
.clientSecret(configMap.get(CLIENT_SECRET))
.technicalAccountId(configMap.get(TECHNICAL_ACCOUNT_ID));
if (!StringUtils.isEmpty(configMap.get(META_SCOPES))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class WorkspaceUtilTest {
public void getWorkspaceBuilderFromJwtProperties() {
Workspace workspaceFromProperties = WorkspaceUtil.getWorkspaceBuilder(FileUtil.getMap(TEST_JWT_WORKSPACE_PROPERTIES)).build();
JwtContext expectedAuthContext = JwtContext.builder()
.credentialId(CREDENTIAL_ID + TEST_VALUE)
.clientSecret(CLIENT_SECRET + TEST_VALUE)
.technicalAccountId(TECHNICAL_ACCOUNT_ID + TEST_VALUE)
.addMetascope(META_SCOPES + TEST_VALUE)
Expand All @@ -44,6 +43,7 @@ public void getWorkspaceBuilderFromJwtProperties() {
.consumerOrgId(Workspace.CONSUMER_ORG_ID + TEST_VALUE)
.projectId(Workspace.PROJECT_ID + TEST_VALUE)
.workspaceId(Workspace.WORKSPACE_ID + TEST_VALUE)
.credentialId(Workspace.CREDENTIAL_ID + TEST_VALUE)
.authContext(expectedAuthContext)
.build();

Expand Down
2 changes: 1 addition & 1 deletion ims/src/test/resources/workspace.jwt.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ aio_consumer_org_id=aio_consumer_org_id_changeMe
aio_ims_org_id=aio_ims_org_id_changeMe
# aio_workspace_id = your Adobe Developer Console workspace Id (project.workspace.id)
aio_workspace_id=aio_workspace_id_changeMe
# aio_credential_id = your Adobe Developer Console jwt credential id (project.workspace.details.credentials[i].id)
# aio_credential_id = your Adobe Developer Console credential id (project.workspace.details.credentials[i].id)
aio_credential_id=aio_credential_id_changeMe
# aio_client_secret = your Adobe Developer Console jwt credential client secret (project.workspace.details.credentials[i].jwt.client_secret)
aio_client_secret=aio_client_secret_changeMe
Expand Down

0 comments on commit 905e2d5

Please sign in to comment.