SecurityKeyLoader { public AsymmetricKeys load() throws Exception { Preconditions.checkState(jmapDraftConfiguration.isEnabled(), "JMAP is not enabled"); KeyStore keystore = KeyStore.getInstance(JKS); char[] secret; try (InputStream fis = fileSystem.getResource(jmapDraftConfiguration.getKeystore())) { secret = jmapDraftConfiguration.getSecret().toCharArray(); keystore.load(fis, secret); } Certificate aliasCertificate = Optional .ofNullable(keystore.getCertificate(ALIAS)) .orElseThrow(() -> new KeyStoreException("Alias '" + ALIAS + "' keystore can't be found")); PublicKey publicKey = aliasCertificate.getPublicKey(); Key key = keystore.getKey(ALIAS, secret); if (! (key instanceof PrivateKey)) { throw new KeyStoreException("Provided key is not a PrivateKey"); } return new AsymmetricKeys((PrivateKey) key, publicKey); } @VisibleForTesting @Inject  SecurityKeyLoader(FileSystem fileSystem, JMAPDraftConfiguration jmapDraftConfiguration); AsymmetricKeys load();  }
@Test void loadShouldReturnAsymmetricKeysWhenCorrectPassword() throws Exception { JMAPDraftConfiguration jmapDraftConfiguration = JMAPDraftConfiguration.builder() .enable() .jwtPublicKeyPem(Optional.of(JWT_PUBLIC_KEY)) .keystore("keystore") .secret("james72laBalle") .build(); SecurityKeyLoader loader = new SecurityKeyLoader( FileSystemFixture.CLASSPATH_FILE_SYSTEM, jmapDraftConfiguration); assertThat(loader.load()) .isNotNull(); }