use of org.apache.cxf.ws.security.sts.provider.STSException in project cxf by apache.
the class CustomUsernameTokenProvider method createToken.
public TokenProviderResponse createToken(TokenProviderParameters tokenParameters) {
try {
Document doc = DOMUtils.getEmptyDocument();
// Mock up a UsernameToken
UsernameToken usernameToken = new UsernameToken(true, doc, WSS4JConstants.PASSWORD_TEXT);
usernameToken.setName("alice");
usernameToken.setPassword("password");
String id = "UT-1234";
usernameToken.addWSSENamespace();
usernameToken.addWSUNamespace();
usernameToken.setID(id);
TokenProviderResponse response = new TokenProviderResponse();
response.setToken(usernameToken.getElement());
response.setTokenId(id);
// Store the token in the cache
if (tokenParameters.getTokenStore() != null) {
SecurityToken securityToken = new SecurityToken(usernameToken.getID());
securityToken.setToken(usernameToken.getElement());
int hashCode = usernameToken.hashCode();
String identifier = Integer.toString(hashCode);
securityToken.setTokenHash(hashCode);
tokenParameters.getTokenStore().add(identifier, securityToken);
}
return response;
} catch (Exception e) {
e.printStackTrace();
throw new STSException("Can't serialize SAML assertion", e, STSException.REQUEST_FAILED);
}
}
use of org.apache.cxf.ws.security.sts.provider.STSException in project cxf by apache.
the class SCTSAMLTokenProvider method createToken.
/**
* Create a token given a TokenProviderParameters
*/
public TokenProviderResponse createToken(TokenProviderParameters tokenParameters) {
testKeyType(tokenParameters);
byte[] entropyBytes = null;
long keySize = 0;
boolean computedKey = false;
KeyRequirements keyRequirements = tokenParameters.getKeyRequirements();
TokenRequirements tokenRequirements = tokenParameters.getTokenRequirements();
LOG.fine("Handling token of type: " + tokenRequirements.getTokenType());
keyRequirements.setKeyType(STSConstants.SYMMETRIC_KEY_KEYTYPE);
byte[] secret = (byte[]) tokenParameters.getAdditionalProperties().get(SCTValidator.SCT_VALIDATOR_SECRET);
try {
Document doc = DOMUtils.createDocument();
SamlAssertionWrapper assertion = createSamlToken(tokenParameters, secret, doc);
Element token = assertion.toDOM(doc);
TokenProviderResponse response = new TokenProviderResponse();
response.setToken(token);
String tokenType = tokenRequirements.getTokenType();
if (WSS4JConstants.WSS_SAML2_TOKEN_TYPE.equals(tokenType) || WSS4JConstants.SAML2_NS.equals(tokenType)) {
response.setTokenId(token.getAttributeNS(null, "ID"));
} else {
response.setTokenId(token.getAttributeNS(null, "AssertionID"));
}
response.setCreated(assertion.getNotBefore());
response.setExpires(assertion.getNotOnOrAfter());
response.setEntropy(entropyBytes);
if (keySize > 0) {
response.setKeySize(keySize);
}
response.setComputedKey(computedKey);
return response;
} catch (Exception e) {
LOG.log(Level.WARNING, "", e);
throw new STSException("Can't serialize SAML assertion", e, STSException.REQUEST_FAILED);
}
}
use of org.apache.cxf.ws.security.sts.provider.STSException in project cxf by apache.
the class AbstractOperation method parseRequest.
/**
* Check the arguments from the STSProvider and parse the request.
*/
protected RequestRequirements parseRequest(RequestSecurityTokenType request, Map<String, Object> messageContext) {
if (messageContext == null) {
throw new STSException("No message context found");
}
if (stsProperties == null) {
throw new STSException("No STSProperties object found");
}
stsProperties.configureProperties();
RequestParser requestParser = new RequestParser();
requestParser.setAllowCustomContent(allowCustomContent);
return requestParser.parseRequest(request, messageContext, stsProperties, claimsManager.getClaimParsers());
}
use of org.apache.cxf.ws.security.sts.provider.STSException in project cxf by apache.
the class AbstractOperation method encryptSecret.
/**
* Encrypt a secret using the given arguments producing a DOM EncryptedKey element
*/
protected Element encryptSecret(byte[] secret, EncryptionProperties encryptionProperties, KeyRequirements keyRequirements) throws WSSecurityException {
String name = encryptionProperties.getEncryptionName();
if (name == null) {
name = stsProperties.getEncryptionUsername();
}
if (name == null) {
throw new STSException("No encryption alias is configured", STSException.REQUEST_FAILED);
}
// Get the key-wrap algorithm to use
String keyWrapAlgorithm = keyRequirements.getKeywrapAlgorithm();
if (keyWrapAlgorithm == null) {
// If none then default to what is configured
keyWrapAlgorithm = encryptionProperties.getKeyWrapAlgorithm();
} else {
List<String> supportedAlgorithms = encryptionProperties.getAcceptedKeyWrapAlgorithms();
if (!supportedAlgorithms.contains(keyWrapAlgorithm)) {
keyWrapAlgorithm = encryptionProperties.getKeyWrapAlgorithm();
if (LOG.isLoggable(Level.FINE)) {
LOG.fine("KeyWrapAlgorithm not supported, defaulting to: " + keyWrapAlgorithm);
}
}
}
Document doc = DOMUtils.getEmptyDocument();
WSSecEncryptedKey builder = new WSSecEncryptedKey(doc);
builder.setUserInfo(name);
builder.setKeyIdentifierType(encryptionProperties.getKeyIdentifierType());
builder.setKeyEncAlgo(keyWrapAlgorithm);
final SecretKey symmetricKey;
if (secret != null) {
symmetricKey = KeyUtils.prepareSecretKey(encryptionProperties.getEncryptionAlgorithm(), secret);
} else {
KeyGenerator keyGen = KeyUtils.getKeyGenerator(encryptionProperties.getEncryptionAlgorithm());
symmetricKey = keyGen.generateKey();
}
builder.prepare(stsProperties.getEncryptionCrypto(), symmetricKey);
return builder.getEncryptedKeyElement();
}
use of org.apache.cxf.ws.security.sts.provider.STSException in project cxf by apache.
the class TokenCancelOperation method cancel.
public RequestSecurityTokenResponseType cancel(RequestSecurityTokenType request, Principal principal, Map<String, Object> messageContext) {
long start = System.currentTimeMillis();
TokenCancellerParameters cancellerParameters = new TokenCancellerParameters();
try {
RequestRequirements requestRequirements = parseRequest(request, messageContext);
KeyRequirements keyRequirements = requestRequirements.getKeyRequirements();
TokenRequirements tokenRequirements = requestRequirements.getTokenRequirements();
cancellerParameters.setStsProperties(stsProperties);
cancellerParameters.setPrincipal(principal);
cancellerParameters.setMessageContext(messageContext);
cancellerParameters.setTokenStore(getTokenStore());
cancellerParameters.setKeyRequirements(keyRequirements);
cancellerParameters.setTokenRequirements(tokenRequirements);
ReceivedToken cancelTarget = tokenRequirements.getCancelTarget();
if (cancelTarget == null || cancelTarget.getToken() == null) {
throw new STSException("No element presented for cancellation", STSException.INVALID_REQUEST);
}
cancellerParameters.setToken(cancelTarget);
if (tokenRequirements.getTokenType() == null) {
tokenRequirements.setTokenType(STSConstants.STATUS);
LOG.fine("Received TokenType is null, falling back to default token type: " + STSConstants.STATUS);
}
//
// Cancel token
//
TokenCancellerResponse tokenResponse = null;
for (TokenCanceller tokenCanceller : tokencancellers) {
if (tokenCanceller.canHandleToken(cancelTarget)) {
try {
tokenResponse = tokenCanceller.cancelToken(cancellerParameters);
} catch (RuntimeException ex) {
LOG.log(Level.WARNING, "", ex);
throw new STSException("Error while cancelling a token", ex, STSException.REQUEST_FAILED);
}
break;
}
}
if (tokenResponse == null || tokenResponse.getToken() == null) {
LOG.fine("No Token Canceller has been found that can handle this token");
throw new STSException("No token canceller found for requested token type: " + tokenRequirements.getTokenType(), STSException.REQUEST_FAILED);
}
if (tokenResponse.getToken().getState() != STATE.CANCELLED) {
LOG.log(Level.WARNING, "Token cancellation failed.");
throw new STSException("Token cancellation failed.");
}
// prepare response
try {
RequestSecurityTokenResponseType response = createResponse(tokenRequirements);
STSCancelSuccessEvent event = new STSCancelSuccessEvent(cancellerParameters, System.currentTimeMillis() - start);
publishEvent(event);
cleanRequest(requestRequirements);
return response;
} catch (Throwable ex) {
LOG.log(Level.WARNING, "", ex);
throw new STSException("Error in creating the response", ex, STSException.REQUEST_FAILED);
}
} catch (RuntimeException ex) {
STSCancelFailureEvent event = new STSCancelFailureEvent(cancellerParameters, System.currentTimeMillis() - start, ex);
publishEvent(event);
throw ex;
}
}
Aggregations