JdbcUserGoogleMfaCredentialsProvisioning implements SystemDeletable, UserMfaCredentialsProvisioning<UserGoogleMfaCredentials> { @Override public void save(UserGoogleMfaCredentials credentials, String zoneId) { try { jdbcTemplate.update(CREATE_USER_MFA_CONFIG_SQL, ps -> { int pos = 1; ps.setString(pos++, credentials.getUserId()); try { ps.setString(pos++, encrypt(credentials.getSecretKey())); ps.setString(pos++, encrypt(String.valueOf(credentials.getValidationCode()))); ps.setString(pos++, encrypt(toCSScratchCode(credentials.getScratchCodes()))); } catch (EncryptionServiceException e) { logger.error("Unable to encrypt MFA credentials", e); throw new UnableToPersistMfaException(e); } ps.setString(pos++, credentials.getMfaProviderId()); ps.setString(pos++, zoneId); ps.setString(pos++, encryptionKeyService.getActiveKey().getLabel()); }); } catch (DuplicateKeyException e) { throw new UserMfaConfigAlreadyExistsException(e.getMostSpecificCause().getMessage()); } } JdbcUserGoogleMfaCredentialsProvisioning(JdbcTemplate jdbcTemplate, EncryptionKeyService encryptionKeyService); @Override void save(UserGoogleMfaCredentials credentials, String zoneId); @Override void update(UserGoogleMfaCredentials credentials, String zoneId); @Override UserGoogleMfaCredentials retrieve(String userId, String mfaProviderId); @Override int deleteByUser(String userId, String zoneId); @Override int deleteByMfaProvider(String mfaProviderId, String zoneId); @Override int deleteByIdentityZone(String zoneId); @Override int delete(String userId); @Override Logger getLogger();  }
@Test void saveUserGoogleMfaCredentials() throws EncryptionServiceException { assertEquals(0, jdbcTemplate.queryForList("SELECT * FROM user_google_mfa_credentials").size()); UserGoogleMfaCredentials userGoogleMfaCredentials = new UserGoogleMfaCredentials("jabbahut", "very_sercret_key", 74718234, Arrays.asList(1, 22)).setMfaProviderId(MFA_ID); db.save(userGoogleMfaCredentials, zoneId); List<Map<String, Object>> credentials = jdbcTemplate.queryForList("SELECT * FROM user_google_mfa_credentials"); assertEquals(1, credentials.size()); Map<String, Object> record = credentials.get(0); assertEquals("jabbahut", record.get("user_id")); assertEquals("very_sercret_key", new String( activeEncryptionKey.decrypt(Base64Utils.decodeFromString((String) record.get("secret_key")))) ); assertEquals(74718234, Integer.parseInt(new String( activeEncryptionKey.decrypt(Base64Utils.decodeFromString(String.valueOf(record.get("encrypted_validation_code")))))) ); assertEquals("1,22", new String( activeEncryptionKey.decrypt(Base64Utils.decodeFromString(String.valueOf(record.get("scratch_codes"))))) ); assertEquals(MFA_ID, record.get("mfa_provider_id")); assertEquals(zoneId, record.get("zone_id")); assertEquals(activeKeyLabel, record.get("encryption_key_label")); }
@Test void save_whenExistsForUser() { assertEquals(0, jdbcTemplate.queryForList("SELECT * FROM user_google_mfa_credentials").size()); UserGoogleMfaCredentials userGoogleMfaCredentials = new UserGoogleMfaCredentials("jabbahut", "very_sercret_key", 74718234, Arrays.asList(1, 22)).setMfaProviderId(MFA_ID); db.save(userGoogleMfaCredentials, zoneId); assertThrows(UserMfaConfigAlreadyExistsException.class, () -> db.save(userGoogleMfaCredentials, zoneId)); }