14 changed files with 199 additions and 21 deletions
@ -0,0 +1,73 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* 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 org.thingsboard.server.service.security.auth.mfa.provider.impl; |
|||
|
|||
import org.apache.commons.lang3.RandomStringUtils; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.context.annotation.Lazy; |
|||
import org.springframework.stereotype.Service; |
|||
import org.thingsboard.common.util.CollectionsUtil; |
|||
import org.thingsboard.server.common.data.User; |
|||
import org.thingsboard.server.common.data.security.model.mfa.account.BackupCodeTwoFaAccountConfig; |
|||
import org.thingsboard.server.common.data.security.model.mfa.provider.BackupCodeTwoFaProviderConfig; |
|||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFaProviderType; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.security.auth.mfa.config.TwoFaConfigManager; |
|||
import org.thingsboard.server.service.security.auth.mfa.provider.TwoFaProvider; |
|||
import org.thingsboard.server.service.security.model.SecurityUser; |
|||
|
|||
import java.util.Set; |
|||
import java.util.stream.Collectors; |
|||
import java.util.stream.Stream; |
|||
|
|||
@Service |
|||
@TbCoreComponent |
|||
public class BackupCodeTwoFaProvider implements TwoFaProvider<BackupCodeTwoFaProviderConfig, BackupCodeTwoFaAccountConfig> { |
|||
|
|||
@Autowired @Lazy |
|||
private TwoFaConfigManager twoFaConfigManager; |
|||
|
|||
@Override |
|||
public BackupCodeTwoFaAccountConfig generateNewAccountConfig(User user, BackupCodeTwoFaProviderConfig providerConfig) { |
|||
BackupCodeTwoFaAccountConfig config = new BackupCodeTwoFaAccountConfig(); |
|||
config.setCodes(generateCodes(providerConfig.getCodesQuantity(), 8)); |
|||
config.setSerializeHiddenFields(true); |
|||
return config; |
|||
} |
|||
|
|||
private static Set<String> generateCodes(int count, int length) { |
|||
return Stream.generate(() -> RandomStringUtils.random(length, "0123456789abcdef")) |
|||
.distinct().limit(count) |
|||
.collect(Collectors.toSet()); |
|||
} |
|||
|
|||
@Override |
|||
public boolean checkVerificationCode(SecurityUser user, String code, BackupCodeTwoFaProviderConfig providerConfig, BackupCodeTwoFaAccountConfig accountConfig) { |
|||
if (CollectionsUtil.contains(accountConfig.getCodes(), code)) { |
|||
accountConfig.getCodes().remove(code); |
|||
twoFaConfigManager.saveTwoFaAccountConfig(user.getTenantId(), user.getId(), accountConfig); |
|||
return true; |
|||
} else { |
|||
return false; |
|||
} |
|||
} |
|||
|
|||
@Override |
|||
public TwoFaProviderType getType() { |
|||
return TwoFaProviderType.BACKUP_CODE; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,57 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* 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 org.thingsboard.server.common.data.security.model.mfa.account; |
|||
|
|||
import com.fasterxml.jackson.annotation.JsonGetter; |
|||
import lombok.Data; |
|||
import lombok.EqualsAndHashCode; |
|||
import org.thingsboard.server.common.data.security.model.mfa.provider.TwoFaProviderType; |
|||
|
|||
import javax.validation.constraints.NotEmpty; |
|||
import java.util.Set; |
|||
|
|||
@Data |
|||
@EqualsAndHashCode(callSuper = true) |
|||
public class BackupCodeTwoFaAccountConfig extends TwoFaAccountConfig { |
|||
|
|||
@NotEmpty |
|||
private Set<String> codes; |
|||
|
|||
@Override |
|||
public TwoFaProviderType getProviderType() { |
|||
return TwoFaProviderType.BACKUP_CODE; |
|||
} |
|||
|
|||
|
|||
@JsonGetter("codes") |
|||
private Set<String> getCodesForJson() { |
|||
if (serializeHiddenFields) { |
|||
return codes; |
|||
} else { |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
@JsonGetter |
|||
private Integer getCodesLeft() { |
|||
if (codes != null) { |
|||
return codes.size(); |
|||
} else { |
|||
return null; |
|||
} |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
/** |
|||
* Copyright © 2016-2022 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* 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 org.thingsboard.server.common.data.security.model.mfa.provider; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import javax.validation.constraints.Min; |
|||
|
|||
@Data |
|||
public class BackupCodeTwoFaProviderConfig implements TwoFaProviderConfig { |
|||
|
|||
@Min(0) |
|||
private int codesQuantity; |
|||
|
|||
@Override |
|||
public TwoFaProviderType getProviderType() { |
|||
return TwoFaProviderType.BACKUP_CODE; |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue