Search in sources :

Example 1 with QrData

use of dev.samstevens.totp.qr.QrData in project CzechIdMng by bcvsolutions.

the class DefaultTwoFactorAuthenticationManager method init.

@Override
@Transactional
public TwoFactorRegistrationResponseDto init(UUID identityId, TwoFactorAuthenticationType twoFactorAuthenticationType) {
    Assert.notNull(identityId, "Identity identifier is required.");
    IdmIdentityDto identity = identityService.get(identityId);
    Assert.notNull(identity, "Identity is required.");
    Assert.notNull(twoFactorAuthenticationType, "Two factor authentication method is required.");
    // 
    String secret = secretGenerator.generate();
    // 
    TwoFactorRegistrationResponseDto registration = new TwoFactorRegistrationResponseDto();
    registration.setVerificationSecret(secret);
    registration.setUsername(SpinalCase.format(identity.getUsername()));
    // generate qr code
    if (twoFactorAuthenticationType == TwoFactorAuthenticationType.APPLICATION) {
        QrData qrcode = qrDataFactory.newBuilder().label(registration.getUsername()).secret(secret).issuer(// TODO: ApplicationConfiguration
        "CzechIdM").build();
        try {
            byte[] imageData = qrGenerator.generate(qrcode);
            String mimeType = qrGenerator.getImageMimeType();
            registration.setQrcode(Utils.getDataUriForImage(imageData, mimeType));
        } catch (Exception ex) {
            throw new ResultCodeException(CoreResultCode.TWO_FACTOR_INIT_FAILED, ex);
        }
    } else {
        // NOTIFICATION
        sendVerificationCode(identity, generateCode(new GuardedString(secret)));
    }
    // 
    return registration;
}
Also used : QrData(dev.samstevens.totp.qr.QrData) TwoFactorRegistrationResponseDto(eu.bcvsolutions.idm.core.security.api.dto.TwoFactorRegistrationResponseDto) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) GuardedString(eu.bcvsolutions.idm.core.security.api.domain.GuardedString) IdmIdentityDto(eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto) CodeGenerationException(dev.samstevens.totp.exceptions.CodeGenerationException) ResultCodeException(eu.bcvsolutions.idm.core.api.exception.ResultCodeException) EntityNotFoundException(eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException) IOException(java.io.IOException) MustChangePasswordException(eu.bcvsolutions.idm.core.security.api.exception.MustChangePasswordException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

CodeGenerationException (dev.samstevens.totp.exceptions.CodeGenerationException)1 QrData (dev.samstevens.totp.qr.QrData)1 IdmIdentityDto (eu.bcvsolutions.idm.core.api.dto.IdmIdentityDto)1 EntityNotFoundException (eu.bcvsolutions.idm.core.api.exception.EntityNotFoundException)1 ResultCodeException (eu.bcvsolutions.idm.core.api.exception.ResultCodeException)1 GuardedString (eu.bcvsolutions.idm.core.security.api.domain.GuardedString)1 TwoFactorRegistrationResponseDto (eu.bcvsolutions.idm.core.security.api.dto.TwoFactorRegistrationResponseDto)1 MustChangePasswordException (eu.bcvsolutions.idm.core.security.api.exception.MustChangePasswordException)1 IOException (java.io.IOException)1 Transactional (org.springframework.transaction.annotation.Transactional)1