Search in sources :

Example 1 with MfaAlreadyExistsException

use of org.cloudfoundry.identity.uaa.mfa.exception.MfaAlreadyExistsException in project uaa by cloudfoundry.

the class MockMvcUtils method createMfaProvider.

public static MfaProvider createMfaProvider(ApplicationContext context, IdentityZone zone) {
    String zoneId = zone.getId();
    MfaProvider provider = new MfaProvider();
    provider.setName(new RandomValueStringGenerator(5).generate().toLowerCase());
    provider.setType(MfaProvider.MfaProviderType.GOOGLE_AUTHENTICATOR);
    provider.setIdentityZoneId(zoneId);
    provider.setConfig(new GoogleMfaProviderConfig());
    provider.getConfig().setIssuer(zone.getName());
    MfaProviderProvisioning provisioning = context.getBean(MfaProviderProvisioning.class);
    try {
        return provisioning.create(provider, zoneId);
    } catch (MfaAlreadyExistsException x) {
        return provisioning.update(provider, zoneId);
    }
}
Also used : MfaProvider(org.cloudfoundry.identity.uaa.mfa.MfaProvider) MfaAlreadyExistsException(org.cloudfoundry.identity.uaa.mfa.exception.MfaAlreadyExistsException) MfaProviderProvisioning(org.cloudfoundry.identity.uaa.mfa.MfaProviderProvisioning) GoogleMfaProviderConfig(org.cloudfoundry.identity.uaa.mfa.GoogleMfaProviderConfig) RandomValueStringGenerator(org.springframework.security.oauth2.common.util.RandomValueStringGenerator)

Example 2 with MfaAlreadyExistsException

use of org.cloudfoundry.identity.uaa.mfa.exception.MfaAlreadyExistsException in project uaa by cloudfoundry.

the class MfaProviderEndpoints method deleteMfaProviderById.

@RequestMapping(value = "{id}", method = DELETE)
public ResponseEntity<MfaProvider> deleteMfaProviderById(@PathVariable String id) {
    MfaProvider existing = mfaProviderProvisioning.retrieve(id, IdentityZoneHolder.get().getId());
    MfaConfig currentMfaConfig = IdentityZoneHolder.get().getConfig().getMfaConfig();
    if (currentMfaConfig.isEnabled() && currentMfaConfig.getProviderName().equals(existing.getName())) {
        throw new MfaAlreadyExistsException("MFA provider is currently active on zone: " + IdentityZoneHolder.get().getId() + ". Please deactivate it from the zone or set another MFA provider");
    }
    publisher.publishEvent(new EntityDeletedEvent<>(existing, SecurityContextHolder.getContext().getAuthentication(), IdentityZoneHolder.getCurrentZoneId()));
    return new ResponseEntity<>(existing, HttpStatus.OK);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) MfaAlreadyExistsException(org.cloudfoundry.identity.uaa.mfa.exception.MfaAlreadyExistsException) MfaConfig(org.cloudfoundry.identity.uaa.zone.MfaConfig) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with MfaAlreadyExistsException

use of org.cloudfoundry.identity.uaa.mfa.exception.MfaAlreadyExistsException in project uaa by cloudfoundry.

the class JdbcMfaProviderProvisioning method create.

@Override
public MfaProvider create(MfaProvider provider, String zoneId) {
    mfaProviderValidator.validate(provider);
    final String id = UUID.randomUUID().toString();
    try {
        jdbcTemplate.update(CREATE_PROVIDER_SQL, ps -> {
            int pos = 1;
            ps.setString(pos++, id);
            ps.setString(pos++, provider.getName());
            ps.setString(pos++, provider.getType().toValue());
            ps.setString(pos++, JsonUtils.writeValueAsString(provider.getConfig()));
            ps.setString(pos++, zoneId);
            ps.setTimestamp(pos++, new Timestamp(System.currentTimeMillis()));
            ps.setTimestamp(pos++, new Timestamp(System.currentTimeMillis()));
        });
    } catch (DuplicateKeyException e) {
        String message = e.getMostSpecificCause().getMessage();
        if (message.toUpperCase().contains("IDX_MFA_UNIQUE_NAME")) {
            message = "An MFA Provider with that name already exists.";
        }
        throw new MfaAlreadyExistsException(message);
    }
    return retrieve(id, zoneId);
}
Also used : MfaAlreadyExistsException(org.cloudfoundry.identity.uaa.mfa.exception.MfaAlreadyExistsException) Timestamp(java.sql.Timestamp) DuplicateKeyException(org.springframework.dao.DuplicateKeyException)

Example 4 with MfaAlreadyExistsException

use of org.cloudfoundry.identity.uaa.mfa.exception.MfaAlreadyExistsException in project uaa by cloudfoundry.

the class JdbcMfaProviderProvisioning method update.

@Override
public MfaProvider update(MfaProvider provider, String zoneId) {
    try {
        jdbcTemplate.update(UPDATE_PROVIDER_SQL, ps -> {
            int pos = 1;
            ps.setString(pos++, provider.getName());
            ps.setString(pos++, provider.getType().toValue());
            ps.setString(pos++, JsonUtils.writeValueAsString(provider.getConfig()));
            ps.setString(pos++, zoneId);
            ps.setTimestamp(pos++, new Timestamp(System.currentTimeMillis()));
            ps.setString(pos++, provider.getId().trim());
            ps.setString(pos++, zoneId);
        });
    } catch (DuplicateKeyException e) {
        String message = e.getMostSpecificCause().getMessage();
        if (message.toUpperCase().contains("IDX_MFA_UNIQUE_NAME")) {
            message = "An MFA Provider with that name already exists.";
        }
        throw new MfaAlreadyExistsException(message);
    }
    return retrieve(provider.getId(), zoneId);
}
Also used : MfaAlreadyExistsException(org.cloudfoundry.identity.uaa.mfa.exception.MfaAlreadyExistsException) Timestamp(java.sql.Timestamp) DuplicateKeyException(org.springframework.dao.DuplicateKeyException)

Aggregations

MfaAlreadyExistsException (org.cloudfoundry.identity.uaa.mfa.exception.MfaAlreadyExistsException)4 Timestamp (java.sql.Timestamp)2 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)2 GoogleMfaProviderConfig (org.cloudfoundry.identity.uaa.mfa.GoogleMfaProviderConfig)1 MfaProvider (org.cloudfoundry.identity.uaa.mfa.MfaProvider)1 MfaProviderProvisioning (org.cloudfoundry.identity.uaa.mfa.MfaProviderProvisioning)1 MfaConfig (org.cloudfoundry.identity.uaa.zone.MfaConfig)1 ResponseEntity (org.springframework.http.ResponseEntity)1 RandomValueStringGenerator (org.springframework.security.oauth2.common.util.RandomValueStringGenerator)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1