JdbcMfaProviderProvisioning implements MfaProviderProvisioning, SystemDeletable { @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); } JdbcMfaProviderProvisioning(JdbcTemplate jdbcTemplate, MfaProviderValidator mfaProviderValidator); @Override MfaProvider create(MfaProvider provider, String zoneId); @Override MfaProvider update(MfaProvider provider, String zoneId); @Override MfaProvider retrieve(String id, String zoneId); @Override MfaProvider retrieveByName(String name, String zoneId); @Override List<MfaProvider> retrieveAll(String zoneId); @Override int deleteByMfaProvider(String providerId, String zoneId); @Override int deleteByIdentityZone(String zoneId); @Override Logger getLogger(); static final String TABLE_NAME; static final String MFA_PROVIDER_FIELDS; static final String CREATE_PROVIDER_SQL; static final String MFA_PROVIDER_UPDATE_FIELDS; static final String UPDATE_PROVIDER_SQL; static final String MFA_PROVIDER_BY_ID_QUERY; static final String MFA_PROVIDER_BY_NAME_QUERY; static final String MFA_PROVIDERS_QUERY; static final String MFA_PROVIDER_DELETE_BY_ID; static final String MFA_PROVIDER_DELETE_BY_ZONE_ID; }
@Test void createDuplicate() { MfaProvider mfaProvider = constructGoogleProvider(); String zoneId = IdentityZoneHolder.get().getId(); assertEquals(0, (int) jdbcTemplate.queryForObject("select count(*) from mfa_providers where identity_zone_id=? and name=?", new Object[]{zoneId, mfaProvider.getName()}, Integer.class)); doNothing().when(mfaProviderValidator); mfaProviderProvisioning.create(mfaProvider, zoneId); assertThrowsWithMessageThat(MfaAlreadyExistsException.class, () -> mfaProviderProvisioning.create(mfaProvider, zoneId), is("An MFA Provider with that name already exists.")); }
@Test void createDuplicateWorksAcrossZones() { MfaProvider mfaProvider = constructGoogleProvider(); String zoneId = IdentityZoneHolder.get().getId(); assertEquals(0, (int) jdbcTemplate.queryForObject("select count(*) from mfa_providers where identity_zone_id=? and name=?", new Object[]{zoneId, mfaProvider.getName()}, Integer.class)); doNothing().when(mfaProviderValidator); mfaProviderProvisioning.create(mfaProvider, zoneId); mfaProviderProvisioning.create(mfaProvider, zoneId + "-other-zone"); }