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

Mfa multitenancy #122

Merged
merged 16 commits into from
Oct 26, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2023, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package io.supertokens.pluginInterface.multitenancy;

import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;

import javax.annotation.Nullable;
import java.util.HashSet;
import java.util.Set;

public class MfaConfig {
@Nullable
public final String[] firstFactors;

@Nullable
public final String[] defaultRequiredFactorIds;

public MfaConfig(@Nullable String[] firstFactors, @Nullable String[] defaultRequiredFactorIds) {
this.firstFactors = firstFactors;
this.defaultRequiredFactorIds = defaultRequiredFactorIds;
}

private boolean compareStrArray(String[] arr1, String[] arr2) {
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
if (arr1 == null && arr2 == null) {
return true;
}

if (arr1 == null || arr2 == null) {
return false;
}

Set<String> set1 = Set.of(arr1);
Set<String> set2 = Set.of(arr2);

return set1.equals(set2);
}

public JsonObject toJson() {
return new GsonBuilder().serializeNulls().create().toJsonTree(this).getAsJsonObject();
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
}

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

return compareStrArray(this.firstFactors, ((MfaConfig) other).firstFactors) &&
compareStrArray(this.defaultRequiredFactorIds, ((MfaConfig) other).defaultRequiredFactorIds);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.google.gson.JsonObject;
import com.google.gson.annotations.SerializedName;
import io.supertokens.pluginInterface.Storage;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

Expand All @@ -41,17 +40,30 @@ public class TenantConfig {
@SerializedName("passwordless")
public final PasswordlessConfig passwordlessConfig;

@Nonnull
@SerializedName("totp")
public final TotpConfig totpConfig;

@Nonnull
@SerializedName("mfa")
public final MfaConfig mfaConfig;

@Nonnull
public final JsonObject coreConfig;

public TenantConfig(@Nonnull TenantIdentifier tenantIdentifier, @Nonnull EmailPasswordConfig emailPasswordConfig,
@Nonnull ThirdPartyConfig thirdPartyConfig,
@Nonnull PasswordlessConfig passwordlessConfig, @Nullable JsonObject coreConfig) {
@Nonnull PasswordlessConfig passwordlessConfig,
@Nonnull TotpConfig totpConfig,
@Nonnull MfaConfig mfaConfig,
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
@Nullable JsonObject coreConfig) {
this.tenantIdentifier = tenantIdentifier;
this.coreConfig = coreConfig == null ? new JsonObject() : coreConfig;
this.emailPasswordConfig = emailPasswordConfig;
this.passwordlessConfig = passwordlessConfig;
this.thirdPartyConfig = thirdPartyConfig;
this.totpConfig = totpConfig;
this.mfaConfig = mfaConfig;
}

public TenantConfig(TenantConfig other) {
Expand All @@ -62,6 +74,10 @@ public TenantConfig(TenantConfig other) {
this.emailPasswordConfig = new EmailPasswordConfig(other.emailPasswordConfig.enabled);
this.passwordlessConfig = new PasswordlessConfig(other.passwordlessConfig.enabled);
this.thirdPartyConfig = new ThirdPartyConfig(other.thirdPartyConfig.enabled, other.thirdPartyConfig.providers.clone());
this.totpConfig = new TotpConfig(other.totpConfig.enabled);
this.mfaConfig = new MfaConfig(
other.mfaConfig.firstFactors == null ? null : other.mfaConfig.firstFactors.clone(),
other.mfaConfig.defaultRequiredFactorIds == null ? null : other.mfaConfig.defaultRequiredFactorIds.clone());
}

public boolean deepEquals(TenantConfig other) {
Expand All @@ -72,6 +88,8 @@ public boolean deepEquals(TenantConfig other) {
this.emailPasswordConfig.equals(other.emailPasswordConfig) &&
this.passwordlessConfig.equals(other.passwordlessConfig) &&
this.thirdPartyConfig.equals(other.thirdPartyConfig) &&
this.totpConfig.equals(other.totpConfig) &&
this.mfaConfig.equals(other.mfaConfig) &&
this.coreConfig.equals(other.coreConfig);
}

Expand All @@ -94,6 +112,7 @@ public JsonObject toJson(boolean shouldProtectDbConfig, Storage storage, String[
JsonObject tenantConfigObject = gson.toJsonTree(this).getAsJsonObject();

tenantConfigObject.add("thirdParty", this.thirdPartyConfig.toJson());
tenantConfigObject.add("mfa", this.mfaConfig.toJson());
rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved

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

rishabhpoddar marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2023, VRAI Labs and/or its affiliates. All rights reserved.
*
* This software is licensed under the Apache License, Version 2.0 (the
* "License") as published by the Apache Software Foundation.
*
* You may not use this file except in compliance with the License. You may
* obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package io.supertokens.pluginInterface.multitenancy;

public class TotpConfig {
public boolean enabled;

public TotpConfig(boolean enabled) {
this.enabled = enabled;
}

@Override
public boolean equals(Object other) {
if (other instanceof TotpConfig) {
TotpConfig otherTotpConfig = (TotpConfig) other;
return otherTotpConfig.enabled == this.enabled;
}
return false;
}
}
Loading