Skip to content
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

Merge latest #127

Merged
merged 5 commits into from
Oct 19, 2023
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

- Replace `TotpNotEnabledException` with `UnknownUserTotpIdException`.

## [4.0.2] - 2023-10-19

- Fixes serialization of thirdParty config

## [4.0.1] - 2023-10-19

- Fixes cloning of `TenantConfig` object to include `null` values

## [4.0.0] - 2023-09-19

- Adds support for account linking
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
id 'java-library'
}

version = "4.0.0"
version = "4.0.2"

repositories {
mavenCentral()
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public TenantConfig(TenantConfig other) {
this.coreConfig = gson.fromJson(other.coreConfig.toString(), JsonObject.class);
this.emailPasswordConfig = new EmailPasswordConfig(other.emailPasswordConfig.enabled);
this.passwordlessConfig = new PasswordlessConfig(other.passwordlessConfig.enabled);
this.thirdPartyConfig = gson.fromJson(gson.toJsonTree(other.thirdPartyConfig).getAsJsonObject(), ThirdPartyConfig.class);
this.thirdPartyConfig = new ThirdPartyConfig(other.thirdPartyConfig.enabled, other.thirdPartyConfig.providers.clone());
}

public boolean deepEquals(TenantConfig other) {
Expand Down Expand Up @@ -92,6 +92,9 @@ public int hashCode() {
public JsonObject toJson(boolean shouldProtectDbConfig, Storage storage, String[] protectedCoreConfigs) {
Gson gson = new Gson();
JsonObject tenantConfigObject = gson.toJsonTree(this).getAsJsonObject();

tenantConfigObject.add("thirdParty", this.thirdPartyConfig.toJson());

tenantConfigObject.addProperty("tenantId", this.tenantIdentifier.getTenantId());

if (shouldProtectDbConfig) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@

package io.supertokens.pluginInterface.multitenancy;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import io.supertokens.pluginInterface.exceptions.InvalidConfigException;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Arrays;
import java.util.Objects;
import java.util.*;

public class ThirdPartyConfig {
public final boolean enabled;
Expand All @@ -35,6 +36,50 @@ public ThirdPartyConfig(boolean enabled, @Nullable Provider[] providers) {
this.providers = providers == null ? new Provider[0] : providers;
}

public JsonObject toJson() {
JsonObject result = new JsonObject();
result.addProperty("enabled", this.enabled);
result.add("providers", new JsonArray());

for (Provider provider : this.providers) {
result.getAsJsonArray("providers").add(provider.toJson());
}

return result;
}

public static boolean unorderedArrayEquals(Object[] array1, Object[] array2) {
if (array1 == null && array2 == null) {
return true;
} else if (array1 == null || array2 == null) {
return false;
}

List<Object> items1 = List.of(array1);
List<Object> items2 = new ArrayList<>();
items2.addAll(Arrays.asList(array2));

if (items1.size() != items2.size()) return false;

for (Object p1 : items1) {
boolean found = false;
for (Object p2 : items2) {
if (p1.equals(p2)) {
found = true;
break;
}
}

if (!found) {
return false;
} else {
items2.remove(p1);
}
}

return true;
}

public static class Provider {

@Nonnull
Expand Down Expand Up @@ -103,13 +148,48 @@ public Provider(@Nonnull String thirdPartyId, @Nonnull String name, @Nullable Pr
this.userInfoMap = userInfoMap == null ? new UserInfoMap(null, null) : userInfoMap;
}

public JsonObject toJson() {
JsonObject result = new Gson().toJsonTree(this).getAsJsonObject();

// These properties need to retain null values when serialized
if (this.authorizationEndpoint != null) {
result.add("authorizationEndpointQueryParams",
new GsonBuilder().serializeNulls().create().toJsonTree(this.authorizationEndpointQueryParams));
} else {
result.remove("authorizationEndpointQueryParams");
}

if (this.tokenEndpointBodyParams != null) {
result.add("tokenEndpointBodyParams", new GsonBuilder().serializeNulls().create().toJsonTree(this.tokenEndpointBodyParams));
} else {
result.remove("tokenEndpointBodyParams");
}

if (this.userInfoEndpointQueryParams != null) {
result.add("userInfoEndpointQueryParams", new GsonBuilder().serializeNulls().create().toJsonTree(this.userInfoEndpointQueryParams));
} else {
result.remove("userInfoEndpointQueryParams");
}

if (this.userInfoEndpointHeaders != null) {
result.add("userInfoEndpointHeaders", new GsonBuilder().serializeNulls().create().toJsonTree(this.userInfoEndpointHeaders));
} else {
result.remove("userInfoEndpointHeaders");
}
return result;
}

@Override
public boolean equals(Object other) {
if (other == null) {
return false;
}

if (other instanceof Provider) {
Provider otherProvider = (Provider) other;
return otherProvider.thirdPartyId.equals(this.thirdPartyId) &&
otherProvider.name.equals(this.name) &&
Arrays.equals(otherProvider.clients, this.clients) &&
return Objects.equals(otherProvider.thirdPartyId, this.thirdPartyId) &&
Objects.equals(otherProvider.name, this.name) &&
unorderedArrayEquals(otherProvider.clients, this.clients) &&
Objects.equals(otherProvider.authorizationEndpoint, this.authorizationEndpoint) &&
Objects.equals(otherProvider.authorizationEndpointQueryParams,
this.authorizationEndpointQueryParams) &&
Expand Down Expand Up @@ -165,7 +245,7 @@ public boolean equals(Object other) {
return Objects.equals(otherProviderClient.clientType, this.clientType) &&
otherProviderClient.clientId.equals(this.clientId) &&
Objects.equals(otherProviderClient.clientSecret, this.clientSecret) &&
Arrays.equals(otherProviderClient.scope, this.scope) &&
unorderedArrayEquals(otherProviderClient.scope, this.scope) &&
otherProviderClient.forcePKCE == this.forcePKCE &&
Objects.equals(otherProviderClient.additionalConfig, this.additionalConfig);
}
Expand Down Expand Up @@ -230,7 +310,7 @@ public boolean equals(Object other) {
if (other instanceof ThirdPartyConfig) {
ThirdPartyConfig otherThirdPartyConfig = (ThirdPartyConfig) other;
return otherThirdPartyConfig.enabled == this.enabled &&
Arrays.equals(otherThirdPartyConfig.providers, this.providers);
unorderedArrayEquals(otherThirdPartyConfig.providers, this.providers);
}
return false;
}
Expand Down
Loading