Skip to content

Commit

Permalink
Make frontend api required for config
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbalakirev committed Oct 27, 2024
1 parent 23c74cd commit 3348023
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 49 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
CORBADO_API_SECRET=corbado1_xxx
CORBADO_PROJECT_ID=pro-xxx
CORBADO_BACKEND_API=
CORBADO_FRONTEND_API=

1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ jobs:
CORBADO_API_SECRET: ${{ secrets.CORBADO_API_SECRET }}
CORBADO_PROJECT_ID: ${{ secrets.CORBADO_PROJECT_ID }}
CORBADO_BACKEND_API: ${{ vars.CORBADO_BACKEND_API }}
CORBADO_FRONTEND_API: ${{ vars.CORBADO_FRONTEND_API }}
run: mvn --batch-mode --update-snapshots verify -DskipInstall

deploy:
Expand Down
51 changes: 8 additions & 43 deletions src/main/java/com/corbado/sdk/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.lang3.StringUtils;

import lombok.Builder;
import lombok.Getter;
import lombok.NonNull;
Expand Down Expand Up @@ -40,8 +42,8 @@ public class Config {
/** The api secret with custom setter. Must be provided. */
@NonNull @Getter private String apiSecret;

/** The backend api with custom setter. Default value: "https://backendapi.cloud.corbado.io/v2" */
@Getter @Builder.Default private String backendApi = "https://backendapi.cloud.corbado.io/v2";
/** The backend api with custom setter. */
@Getter private String backendApi;

/** The short session cookie name. Default value: "cbo_short_session" */
@Getter @Setter @Builder.Default private String shortSessionCookieName = "cbo_short_session";
Expand All @@ -61,38 +63,6 @@ public class Config {
/** The cname. Replaces issuer field if present. */
@Getter @Setter private String cname;

// Constructors
/**
* Instantiates a new config.
*
* @param projectId the project id
* @param apiSecret the api secret
*/
public Config(@NonNull final String projectId, @NonNull final String apiSecret) {

setProjectId(projectId); // set and validate
setApiSecret(apiSecret);

// default values
setFrontendApi(projectId);
setIssuer(this.frontendApi);
}

/**
* Instantiates a new config.
*
* @param projectId the project id
* @param apiSecret the api secret
* @param backendApi the backend api
*/
public Config(
@NonNull final String projectId,
@NonNull final String apiSecret,
@NonNull final String backendApi) {
this(projectId, apiSecret);
setBackendApi(backendApi);
}

// Custom Getters and Setters
/**
* Sets the api secret.
Expand Down Expand Up @@ -136,8 +106,6 @@ public void setBackendApi(String backendApi) {
*/
public void setFrontendApi(String frontendApi) {
frontendApi = StringUtils.trim(frontendApi);
frontendApi = StringUtils.prependIfMissing(frontendApi, HTTPS);
frontendApi = StringUtils.appendIfMissing(frontendApi, ".frontendapi.corbado.io");

try {
new URL(frontendApi); // Validate URL syntax
Expand Down Expand Up @@ -178,10 +146,10 @@ public void setProjectId(@NonNull String projectId) {
public Config(
@NonNull final String projectId,
@NonNull final String apiSecret,
final String backendApi,
@NonNull final String backendApi,
final String shortSessionCookieName,
final String issuer,
final String frontendApi,
@NonNull final String frontendApi,
final Integer shortSessionLength,
final boolean cacheKeys,
String cname) {
Expand All @@ -190,11 +158,8 @@ public Config(
setApiSecret(apiSecret);
setBackendApi(backendApi);
setShortSessionCookieName(shortSessionCookieName);
if (StringUtils.isEmpty(frontendApi)) {
setFrontendApi(projectId);
} else {
setFrontendApi(frontendApi);
}
setFrontendApi(frontendApi);

setShortSessionLength(shortSessionLength);
setCacheKeys(cacheKeys);
setCname(cname);
Expand Down
21 changes: 15 additions & 6 deletions src/test/java/com/corbado/util/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public class TestUtils {
/** The Constant CORBADO_BACKEND_API. */
public static final String CORBADO_BACKEND_API = "CORBADO_BACKEND_API";

/** The Constant CORBADO_FRONTEND_API. */
public static final String CORBADO_FRONTEND_API = "CORBADO_FRONTEND_API";

/** The Constant CANNOT_BE_BLANK_MESSAGE. */
public static final String CANNOT_BE_BLANK_MESSAGE = "cannot be blank";

Expand Down Expand Up @@ -120,13 +123,19 @@ public static CorbadoSdk instantiateSDK() throws StandardException {
if (StringUtils.isEmpty(backendApi)) {
backendApi = dotenv.get(CORBADO_BACKEND_API);
}
Config config = null;
if (StringUtils.isEmpty(backendApi)) {
config = Config.builder().apiSecret(apiSecret).projectId(projectId).build();
} else {
config =
Config.builder().apiSecret(apiSecret).projectId(projectId).backendApi(backendApi).build();

// Check for CORBADO_BACKEND_API
String frontendApi = System.getenv(CORBADO_FRONTEND_API);
if (StringUtils.isEmpty(frontendApi)) {
frontendApi = dotenv.get(CORBADO_FRONTEND_API);
}
final Config config =
Config.builder()
.apiSecret(apiSecret)
.projectId(projectId)
.backendApi(backendApi)
.frontendApi(frontendApi)
.build();

return new CorbadoSdk(config);
}
Expand Down

0 comments on commit 3348023

Please sign in to comment.