Search in sources :

Example 1 with Credential

use of org.wildfly.security.credential.Credential in project wildfly-swarm by wildfly-swarm.

the class AuthCallbackHandler method handle.

@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
    for (Callback current : callbacks) {
        if (current instanceof NameCallback) {
            NameCallback ncb = (NameCallback) current;
            ncb.setName(this.userName);
        } else if (current instanceof RealmCallback) {
            RealmCallback rcb = (RealmCallback) current;
            rcb.setText(rcb.getDefaultText());
        } else if (current instanceof CredentialCallback) {
            CredentialCallback ccb = (CredentialCallback) current;
            try {
                DigestPasswordAlgorithmSpec algoSpec = new DigestPasswordAlgorithmSpec(this.userName, this.realm);
                EncryptablePasswordSpec passwordSpec = new EncryptablePasswordSpec(this.password.toCharArray(), algoSpec);
                Password passwd = PasswordFactory.getInstance(ALGORITHM_DIGEST_MD5).generatePassword(passwordSpec);
                Credential creds = new PasswordCredential(passwd);
                ccb.setCredential(creds);
            } catch (InvalidKeySpecException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        } else if (current instanceof PasswordCallback) {
            PasswordCallback pcb = (PasswordCallback) current;
            pcb.setPassword(this.password.toCharArray());
        } else {
            throw new UnsupportedCallbackException(current);
        }
    }
}
Also used : PasswordCredential(org.wildfly.security.credential.PasswordCredential) Credential(org.wildfly.security.credential.Credential) PasswordCredential(org.wildfly.security.credential.PasswordCredential) CredentialCallback(org.wildfly.security.auth.callback.CredentialCallback) EncryptablePasswordSpec(org.wildfly.security.password.spec.EncryptablePasswordSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) RealmCallback(javax.security.sasl.RealmCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) CredentialCallback(org.wildfly.security.auth.callback.CredentialCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) DigestPasswordAlgorithmSpec(org.wildfly.security.password.spec.DigestPasswordAlgorithmSpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) RealmCallback(javax.security.sasl.RealmCallback) Password(org.wildfly.security.password.Password)

Example 2 with Credential

use of org.wildfly.security.credential.Credential in project wildfly by wildfly.

the class SubjectUtil method convertToSecurityIdentity.

public static SecurityIdentity convertToSecurityIdentity(Subject subject, Principal principal, SecurityDomain domain, String roleCategory) {
    SecurityIdentity identity = null;
    for (Object obj : subject.getPrivateCredentials()) {
        if (obj instanceof SecurityIdentity) {
            identity = (SecurityIdentity) obj;
            break;
        }
    }
    if (identity == null) {
        identity = domain.createAdHocIdentity(principal);
    }
    // convert public credentials
    IdentityCredentials publicCredentials = IdentityCredentials.NONE;
    for (Object credential : subject.getPublicCredentials()) {
        if (credential instanceof PublicKey) {
            publicCredentials = publicCredentials.withCredential(new PublicKeyCredential((PublicKey) credential));
        } else if (credential instanceof X509Certificate) {
            publicCredentials = publicCredentials.withCredential(new X509CertificateChainPublicCredential((X509Certificate) credential));
        } else if (credential instanceof Credential) {
            publicCredentials = publicCredentials.withCredential((Credential) credential);
        }
    }
    if (!publicCredentials.equals(IdentityCredentials.NONE)) {
        identity = identity.withPublicCredentials(publicCredentials);
    }
    // convert private credentials
    IdentityCredentials privateCredentials = IdentityCredentials.NONE;
    for (Object credential : subject.getPrivateCredentials()) {
        if (credential instanceof Password) {
            privateCredentials = privateCredentials.withCredential(new PasswordCredential((Password) credential));
        } else if (credential instanceof SecretKey) {
            privateCredentials = privateCredentials.withCredential(new SecretKeyCredential((SecretKey) credential));
        } else if (credential instanceof KeyPair) {
            privateCredentials = privateCredentials.withCredential(new KeyPairCredential((KeyPair) credential));
        } else if (credential instanceof PrivateKey) {
            privateCredentials = privateCredentials.withCredential(new X509CertificateChainPrivateCredential((PrivateKey) credential));
        } else if (credential instanceof Credential) {
            privateCredentials = privateCredentials.withCredential((Credential) credential);
        }
    }
    if (!privateCredentials.equals(IdentityCredentials.NONE)) {
        identity = identity.withPrivateCredentials(privateCredentials);
    }
    return identity;
}
Also used : X509CertificateChainPrivateCredential(org.wildfly.security.credential.X509CertificateChainPrivateCredential) X509CertificateChainPublicCredential(org.wildfly.security.credential.X509CertificateChainPublicCredential) PublicKeyCredential(org.wildfly.security.credential.PublicKeyCredential) KeyPairCredential(org.wildfly.security.credential.KeyPairCredential) PasswordCredential(org.wildfly.security.credential.PasswordCredential) SecretKeyCredential(org.wildfly.security.credential.SecretKeyCredential) Credential(org.wildfly.security.credential.Credential) KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) X509CertificateChainPrivateCredential(org.wildfly.security.credential.X509CertificateChainPrivateCredential) PublicKey(java.security.PublicKey) PasswordCredential(org.wildfly.security.credential.PasswordCredential) PublicKeyCredential(org.wildfly.security.credential.PublicKeyCredential) X509Certificate(java.security.cert.X509Certificate) SecretKeyCredential(org.wildfly.security.credential.SecretKeyCredential) SecurityIdentity(org.wildfly.security.auth.server.SecurityIdentity) SecretKey(javax.crypto.SecretKey) KeyPairCredential(org.wildfly.security.credential.KeyPairCredential) X509CertificateChainPublicCredential(org.wildfly.security.credential.X509CertificateChainPublicCredential) IdentityCredentials(org.wildfly.security.auth.server.IdentityCredentials) Password(org.wildfly.security.password.Password)

Example 3 with Credential

use of org.wildfly.security.credential.Credential in project fuse-karaf by jboss-fuse.

the class ActivatorTest method initializeCredentialStore.

@Before
public void initializeCredentialStore() throws Exception {
    activator.start(null);
    final WildFlyElytronProvider elytron = new WildFlyElytronProvider();
    Security.addProvider(elytron);
    final PasswordFactory passwordFactory = PasswordFactory.getInstance(ClearPassword.ALGORITHM_CLEAR, elytron);
    final Password password = passwordFactory.generatePassword(new ClearPasswordSpec("it was the best of times it was the worst of times".toCharArray()));
    final Credential credential = new PasswordCredential(password);
    final CredentialSource credentialSource = IdentityCredentials.NONE.withCredential(credential);
    credentialStore = CredentialStore.getInstance(KeyStoreCredentialStore.KEY_STORE_CREDENTIAL_STORE, elytron);
    final String storePath = new File(tmp.getRoot(), "credential.store").getAbsolutePath();
    final Map<String, String> parameters = new HashMap<>();
    parameters.put("location", storePath);
    parameters.put("keyStoreType", "JCEKS");
    credentialStore.initialize(parameters, new CredentialStore.CredentialSourceProtectionParameter(credentialSource));
    final Password secret = passwordFactory.generatePassword(new ClearPasswordSpec("this is a password".toCharArray()));
    final Credential value = new PasswordCredential(secret);
    credentialStore.store("alias", value);
    credentialStore.flush();
}
Also used : PasswordCredential(org.wildfly.security.credential.PasswordCredential) Credential(org.wildfly.security.credential.Credential) HashMap(java.util.HashMap) PasswordCredential(org.wildfly.security.credential.PasswordCredential) ClearPasswordSpec(org.wildfly.security.password.spec.ClearPasswordSpec) WildFlyElytronProvider(org.wildfly.security.WildFlyElytronProvider) PasswordFactory(org.wildfly.security.password.PasswordFactory) CredentialStore(org.wildfly.security.credential.store.CredentialStore) KeyStoreCredentialStore(org.wildfly.security.credential.store.impl.KeyStoreCredentialStore) File(java.io.File) Password(org.wildfly.security.password.Password) ClearPassword(org.wildfly.security.password.interfaces.ClearPassword) CredentialSource(org.wildfly.security.credential.source.CredentialSource) Before(org.junit.Before)

Example 4 with Credential

use of org.wildfly.security.credential.Credential in project wildfly by wildfly.

the class SubjectUtil method fromSecurityIdentity.

public static Subject fromSecurityIdentity(final SecurityIdentity securityIdentity, Subject subject) {
    if (subject == null) {
        subject = new Subject();
    }
    // The first principal added must be the security identity principal
    // as logic in both CXF and JBoss WS look for the first non-Group principal
    subject.getPrincipals().add(securityIdentity.getPrincipal());
    Roles identityRoles = securityIdentity.getRoles();
    // Just add a simple principal for each role instead of aggregating them in a Group.
    // CXF can use such principals when identifying the subject's roles
    String principalName = securityIdentity.getPrincipal().getName();
    Set<Principal> principals = subject.getPrincipals();
    for (String role : identityRoles) {
        if (!principalName.equals(role)) {
            principals.add(new NamePrincipal(role));
        }
    }
    // process the identity's public and private credentials.
    for (Credential credential : securityIdentity.getPublicCredentials()) {
        if (credential instanceof PublicKeyCredential) {
            subject.getPublicCredentials().add(credential.castAs(PublicKeyCredential.class).getPublicKey());
        } else if (credential instanceof X509CertificateChainPublicCredential) {
            subject.getPublicCredentials().add(credential.castAs(X509CertificateChainPublicCredential.class).getCertificateChain());
        } else {
            subject.getPublicCredentials().add(credential);
        }
    }
    for (Credential credential : securityIdentity.getPrivateCredentials()) {
        if (credential instanceof PasswordCredential) {
            addPrivateCredential(subject, credential.castAs(PasswordCredential.class).getPassword());
        } else if (credential instanceof SecretKeyCredential) {
            addPrivateCredential(subject, credential.castAs(SecretKeyCredential.class).getSecretKey());
        } else if (credential instanceof KeyPairCredential) {
            addPrivateCredential(subject, credential.castAs(KeyPairCredential.class).getKeyPair());
        } else if (credential instanceof X509CertificateChainPrivateCredential) {
            addPrivateCredential(subject, credential.castAs(X509CertificateChainPrivateCredential.class).getCertificateChain());
        } else {
            addPrivateCredential(subject, credential);
        }
    }
    // add the identity itself as a private credential - integration code can interact with the SI instead of the Subject if desired.
    addPrivateCredential(subject, securityIdentity);
    return subject;
}
Also used : X509CertificateChainPrivateCredential(org.wildfly.security.credential.X509CertificateChainPrivateCredential) X509CertificateChainPublicCredential(org.wildfly.security.credential.X509CertificateChainPublicCredential) PublicKeyCredential(org.wildfly.security.credential.PublicKeyCredential) KeyPairCredential(org.wildfly.security.credential.KeyPairCredential) PasswordCredential(org.wildfly.security.credential.PasswordCredential) SecretKeyCredential(org.wildfly.security.credential.SecretKeyCredential) Credential(org.wildfly.security.credential.Credential) X509CertificateChainPrivateCredential(org.wildfly.security.credential.X509CertificateChainPrivateCredential) NamePrincipal(org.wildfly.security.auth.principal.NamePrincipal) PasswordCredential(org.wildfly.security.credential.PasswordCredential) Roles(org.wildfly.security.authz.Roles) PublicKeyCredential(org.wildfly.security.credential.PublicKeyCredential) Subject(javax.security.auth.Subject) SecretKeyCredential(org.wildfly.security.credential.SecretKeyCredential) KeyPairCredential(org.wildfly.security.credential.KeyPairCredential) X509CertificateChainPublicCredential(org.wildfly.security.credential.X509CertificateChainPublicCredential) NamePrincipal(org.wildfly.security.auth.principal.NamePrincipal) Principal(java.security.Principal)

Aggregations

Credential (org.wildfly.security.credential.Credential)4 PasswordCredential (org.wildfly.security.credential.PasswordCredential)4 Password (org.wildfly.security.password.Password)3 KeyPairCredential (org.wildfly.security.credential.KeyPairCredential)2 PublicKeyCredential (org.wildfly.security.credential.PublicKeyCredential)2 SecretKeyCredential (org.wildfly.security.credential.SecretKeyCredential)2 X509CertificateChainPrivateCredential (org.wildfly.security.credential.X509CertificateChainPrivateCredential)2 X509CertificateChainPublicCredential (org.wildfly.security.credential.X509CertificateChainPublicCredential)2 File (java.io.File)1 KeyPair (java.security.KeyPair)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Principal (java.security.Principal)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 X509Certificate (java.security.cert.X509Certificate)1 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)1 HashMap (java.util.HashMap)1 SecretKey (javax.crypto.SecretKey)1 Subject (javax.security.auth.Subject)1 Callback (javax.security.auth.callback.Callback)1