use of org.wildfly.security.password.Password in project fuse-karaf by jboss-fuse.
the class Activator method replaced.
/**
* Replaces any value that is given in Credential Store reference format with the value from the Credential Store by
* using {@link System#setProperty(String, String)}.
*
* @param credentialStore
* {@link CredentialStore} containing the secret values
* @param key
* property key
* @param value
* property value, expected to be in Credential store reference format
* @return true if any replacement was done
*/
boolean replaced(final CredentialStore credentialStore, final String key, final String value) {
if (!CredentialStoreHelper.couldBeCredentialStoreAlias(value)) {
return false;
}
final String alias = CredentialStoreHelper.toCredentialStoreAlias(value);
final PasswordCredential passwordCredential;
try {
passwordCredential = credentialStore.retrieve(alias, PasswordCredential.class);
} catch (final CredentialStoreException e) {
return false;
}
if (passwordCredential == null) {
return false;
}
final Password password = passwordCredential.getPassword();
final ClearPassword clearPassword = password.castAs(ClearPassword.class);
final char[] rawClearPassword = clearPassword.getPassword();
System.setProperty(key, String.valueOf(rawClearPassword));
return true;
}
use of org.wildfly.security.password.Password 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);
}
}
}
use of org.wildfly.security.password.Password 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;
}
use of org.wildfly.security.password.Password in project wildfly by wildfly.
the class ReadCredentialServlet method doGet.
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/plain");
resp.setCharacterEncoding("UTF-8");
final PrintWriter writer = resp.getWriter();
final String credentialStore = req.getParameter(PARAM_CREDENTIAL_STORE);
final String alias = req.getParameter(PARAM_ALIAS);
String separator = req.getParameter(PARAM_SEPARATOR);
if (separator == null) {
separator = PARAM_SEPARATOR_DEFAULT;
}
ServiceRegistry registry = CurrentServiceContainer.getServiceContainer();
if (credentialStore == null || credentialStore.length() == 0) {
for (ServiceName name : registry.getServiceNames()) {
if (SERVICE_NAME_CRED_STORE.equals(name.getParent())) {
writer.print(name.getSimpleName());
writer.print(separator);
}
}
return;
}
ServiceController<?> credStoreService = registry.getService(ServiceName.of(SERVICE_NAME_CRED_STORE, credentialStore));
if (credStoreService == null) {
resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
writer.print(credentialStore + " not found");
return;
}
CredentialStore cs = (CredentialStore) credStoreService.getValue();
if (alias == null || alias.length() == 0) {
try {
for (String csAlias : cs.getAliases()) {
writer.print(csAlias);
writer.print(separator);
}
} catch (UnsupportedOperationException | CredentialStoreException e) {
throw new ServletException("Unable to list aliases", e);
}
return;
}
String clearPassword = null;
try {
if (cs.exists(alias, PasswordCredential.class)) {
Password password = cs.retrieve(alias, PasswordCredential.class).getPassword();
if (password instanceof ClearPassword) {
clearPassword = new String(((ClearPassword) password).getPassword());
}
}
} catch (CredentialStoreException | IllegalStateException e) {
throw new ServletException("Unable to retrieve password from credential store", e);
}
if (clearPassword == null) {
resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
writer.print(alias + " password not found in " + credentialStore);
} else {
writer.print(clearPassword);
}
}
use of org.wildfly.security.password.Password in project fuse-karaf by jboss-fuse.
the class MaskedPasswordHelper method createCredentialSource.
@Override
public CredentialSource createCredentialSource(final Map<String, String> configuration) throws GeneralSecurityException, IOException {
final String algorithmParamsBase64 = option(configuration, CREDENTIAL_STORE_PROTECTION_PARAMS, "");
final Decoder decoder = Base64.getDecoder();
final byte[] encodedAlgorithmParams = decoder.decode(algorithmParamsBase64);
final String algorithm = option(configuration, CREDENTIAL_STORE_PROTECTION_ALGORITHM, DEFAULT_ALGORITHM);
final Provider provider = ProviderHelper.provider(option(configuration, CREDENTIAL_STORE_PROTECTION_PROVIDER, ProviderHelper.WILDFLY_PROVIDER));
final AlgorithmParameters algorithmParameters = AlgorithmParameters.getInstance(algorithm, provider);
algorithmParameters.init(encodedAlgorithmParams);
final MaskedPasswordAlgorithmSpec maskedPasswordAlgorithmSpec = algorithmParameters.getParameterSpec(MaskedPasswordAlgorithmSpec.class);
final char[] initialKeyMaterial = maskedPasswordAlgorithmSpec.getInitialKeyMaterial();
final int iterationCount = maskedPasswordAlgorithmSpec.getIterationCount();
final byte[] salt = maskedPasswordAlgorithmSpec.getSalt();
final String maskedPasswordBase64 = option(configuration, CREDENTIAL_STORE_PROTECTION, "");
final byte[] maskedPasswordBytes = decoder.decode(maskedPasswordBase64);
final MaskedPasswordSpec maskedPasswordSpec = new MaskedPasswordSpec(initialKeyMaterial, iterationCount, salt, maskedPasswordBytes);
final PasswordFactory passwordFactory = PasswordFactory.getInstance(algorithm, provider);
final Password maskedPassword = passwordFactory.generatePassword(maskedPasswordSpec);
final PasswordFactory clearPasswordFactory = PasswordFactory.getInstance(ClearPassword.ALGORITHM_CLEAR, provider);
final ClearPasswordSpec clearPasswordSpec = passwordFactory.getKeySpec(maskedPassword, ClearPasswordSpec.class);
final Password password = clearPasswordFactory.generatePassword(clearPasswordSpec);
final PasswordCredential passwordCredential = new PasswordCredential(password);
return IdentityCredentials.NONE.withCredential(passwordCredential);
}
Aggregations