Search in sources :

Example 1 with CipherAuthToken

use of org.jboss.as.clustering.jgroups.auth.CipherAuthToken in project wildfly by wildfly.

the class CipherAuthTokenServiceConfigurator method apply.

@Override
public CipherAuthToken apply(String authValue) {
    KeyStore store = this.keyStore.get();
    String alias = this.keyAlias;
    try {
        if (!store.containsAlias(alias)) {
            throw JGroupsLogger.ROOT_LOGGER.keyEntryNotFound(alias);
        }
        if (!store.entryInstanceOf(alias, KeyStore.PrivateKeyEntry.class)) {
            throw JGroupsLogger.ROOT_LOGGER.unexpectedKeyStoreEntryType(alias, KeyStore.PrivateKeyEntry.class.getSimpleName());
        }
        PasswordCredential credential = this.keyCredentialSource.get().getCredential(PasswordCredential.class);
        if (credential == null) {
            throw JGroupsLogger.ROOT_LOGGER.unexpectedCredentialSource();
        }
        ClearPassword password = credential.getPassword(ClearPassword.class);
        if (password == null) {
            throw JGroupsLogger.ROOT_LOGGER.unexpectedCredentialSource();
        }
        KeyStore.PrivateKeyEntry entry = (KeyStore.PrivateKeyEntry) store.getEntry(alias, new KeyStore.PasswordProtection(password.getPassword()));
        KeyPair pair = new KeyPair(entry.getCertificate().getPublicKey(), entry.getPrivateKey());
        Cipher cipher = Cipher.getInstance(this.transformation);
        return new CipherAuthToken(cipher, pair, authValue.getBytes(StandardCharsets.UTF_8));
    } catch (GeneralSecurityException | IOException e) {
        throw new IllegalArgumentException(e);
    }
}
Also used : ClearPassword(org.wildfly.security.password.interfaces.ClearPassword) KeyPair(java.security.KeyPair) GeneralSecurityException(java.security.GeneralSecurityException) PasswordCredential(org.wildfly.security.credential.PasswordCredential) IOException(java.io.IOException) KeyStore(java.security.KeyStore) Cipher(javax.crypto.Cipher) CipherAuthToken(org.jboss.as.clustering.jgroups.auth.CipherAuthToken)

Aggregations

IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 KeyPair (java.security.KeyPair)1 KeyStore (java.security.KeyStore)1 Cipher (javax.crypto.Cipher)1 CipherAuthToken (org.jboss.as.clustering.jgroups.auth.CipherAuthToken)1 PasswordCredential (org.wildfly.security.credential.PasswordCredential)1 ClearPassword (org.wildfly.security.password.interfaces.ClearPassword)1