GeneralMfaProviderValidator implements MfaProviderValidator { @Override public void validate(MfaProvider mfaProvider) { if(mfaProvider.getName() == null || StringUtils.isEmpty(mfaProvider.getName().trim())) { throw new InvalidMfaProviderException("Provider name is required"); } mfaProvider.setName(mfaProvider.getName().trim()); if(mfaProvider.getName().length() > 256) { throw new InvalidMfaProviderException("Provider name cannot be longer than 256 characters"); } if(!mfaProvider.getName().matches("^[a-zA-Z0-9]+[\\sa-zA-Z0-9]*$")){ throw new InvalidMfaProviderException("Provider name must be alphanumeric"); } if(mfaProvider.getType() == null) { throw new InvalidMfaProviderException("Provider type is required. Must be one of " + MfaProvider.MfaProviderType.getStringValues()); } if(mfaProvider.getConfig() == null) { throw new InvalidMfaProviderException("Provider config is required"); } if(mfaProvider.getConfig().getIssuer() != null && mfaProvider.getConfig().getIssuer().contains(":")) { throw new InvalidMfaProviderException("Provider config contains an invalid issuer. Issuer must not contain a colon"); } if(!StringUtils.hasText(mfaProvider.getIdentityZoneId())){ throw new InvalidMfaProviderException("Provider must belong to a zone"); } }  @Override void validate(MfaProvider mfaProvider);  }
@Test public void validateProviderNullConfig() { expectedException.expect(InvalidMfaProviderException.class); expectedException.expectMessage("Provider config is required"); MfaProvider<GoogleMfaProviderConfig> provider = createValidGoogleMfaProvider() .setConfig(null); validator.validate(provider); }
@Test public void validateProviderConfigWithInvalidIssuer() { expectedException.expect(InvalidMfaProviderException.class); expectedException.expectMessage("Provider config contains an invalid issuer. Issuer must not contain a colon"); MfaProvider<GoogleMfaProviderConfig> provider = createValidGoogleMfaProvider() .setConfig(createValidGoogleMfaConfig().setIssuer("invalid:issuer")); validator.validate(provider); }
@Test public void validateProviderConfigWithMissingIssuer() { MfaProvider<GoogleMfaProviderConfig> provider = createValidGoogleMfaProvider() .setConfig(createValidGoogleMfaConfig().setIssuer(null)); validator.validate(provider); }
@Test public void validateProviderEmptyName() { expectedException.expect(InvalidMfaProviderException.class); expectedException.expectMessage("Provider name is required"); MfaProvider provider = createValidGoogleMfaProvider() .setName(""); validator.validate(provider); }
@Test public void validateProviderInvalidNameTooLong() { expectedException.expect(InvalidMfaProviderException.class); expectedException.expectMessage("Provider name cannot be longer than 256 characters"); MfaProvider provider = createValidGoogleMfaProvider() .setName(new RandomValueStringGenerator(257).generate()); validator.validate(provider); }
@Test public void validateProviderInvalidName() { expectedException.expect(InvalidMfaProviderException.class); expectedException.expectMessage("Provider name is required"); MfaProvider provider = createValidGoogleMfaProvider() .setName(" "); validator.validate(provider); }
@Test public void validateProviderWhitespaceInName() { String name = "This is a valid name"; MfaProvider provider = createValidGoogleMfaProvider() .setName(name); validator.validate(provider); }
@Test public void validateProviderTrimsName() { String name = " This is also a valid name "; MfaProvider provider = createValidGoogleMfaProvider() .setName(name); provider.setName(" " + name + " "); validator.validate(provider); assertEquals(name.trim(), provider.getName()); }
@Test public void validateProviderInvalidNameSpecialChars() { expectedException.expect(InvalidMfaProviderException.class); expectedException.expectMessage("Provider name must be alphanumeric"); MfaProvider provider = createValidGoogleMfaProvider() .setName("invalidName$"); validator.validate(provider); }
@Test public void validateProviderNullType() { expectedException.expect(InvalidMfaProviderException.class); expectedException.expectMessage("Provider type is required. Must be one of " + MfaProvider.MfaProviderType.getStringValues()); MfaProvider provider = createValidGoogleMfaProvider() .setType(null); validator.validate(provider); }
@Test public void validateProviderEmptyZone() { expectedException.expect(InvalidMfaProviderException.class); expectedException.expectMessage("Provider must belong to a zone"); MfaProvider provider = createValidGoogleMfaProvider() .setIdentityZoneId(""); validator.validate(provider); }