Skip to content

Commit

Permalink
Merge branch 'feat/mfa' into mfa-multitenancy
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Oct 20, 2023
2 parents 85580a2 + 5a5d16d commit 6806926
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 33 deletions.
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 @@ -109,6 +109,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,8 +16,10 @@

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;
Expand All @@ -34,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 @@ -102,11 +148,46 @@ 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.authorizationEndpointQueryParams != 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) &&
return Objects.equals(otherProvider.thirdPartyId, this.thirdPartyId) &&
Objects.equals(otherProvider.name, this.name) &&
unorderedArrayEquals(otherProvider.clients, this.clients) &&
Objects.equals(otherProvider.authorizationEndpoint, this.authorizationEndpoint) &&
Expand Down Expand Up @@ -163,7 +244,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 @@ -223,42 +304,13 @@ public boolean equals(Object other) {
}
}

public static boolean unorderedArrayEquals(Object[] array1, Object[] array2) {
Set<Object> items1 = Set.of(array1);
Set<Object> items2 = new HashSet<>();
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;
}

@Override
public boolean equals(Object other) {
if (other instanceof ThirdPartyConfig) {
ThirdPartyConfig otherThirdPartyConfig = (ThirdPartyConfig) other;


return otherThirdPartyConfig.enabled == this.enabled &&
unorderedArrayEquals(this.providers, otherThirdPartyConfig.providers);

}
return false;
}
Expand Down

0 comments on commit 6806926

Please sign in to comment.