Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.sun.xml.ws.security.trust.impl.client.DefaultSTSIssuedTokenConfiguration Maven / Gradle / Ivy
/*
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package com.sun.xml.ws.security.trust.impl.client;
import com.sun.xml.ws.security.addressing.policy.Address;
import com.sun.xml.ws.api.security.trust.Claims;
import com.sun.xml.ws.api.security.trust.client.SecondaryIssuedTokenParameters;
import com.sun.xml.ws.api.security.trust.client.STSIssuedTokenConfiguration;
import com.sun.xml.ws.policy.PolicyAssertion;
import com.sun.xml.ws.security.Token;
import com.sun.xml.ws.security.policy.Issuer;
import com.sun.xml.ws.security.policy.IssuedToken;
import com.sun.xml.ws.security.policy.RequestSecurityTokenTemplate;
import com.sun.xml.ws.security.secext10.AttributedString;
import com.sun.xml.ws.security.secext10.BinarySecurityTokenType;
import com.sun.xml.ws.security.secext10.ObjectFactory;
import com.sun.xml.ws.security.secext10.UsernameTokenType;
import com.sun.xml.ws.security.trust.GenericToken;
import com.sun.xml.ws.security.trust.WSTrustElementFactory;
import com.sun.xml.ws.security.trust.WSTrustVersion;
import com.sun.xml.ws.security.trust.logging.LogStringsMessages;
import com.sun.xml.wss.impl.MessageConstants;
import java.net.URI;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.util.Iterator;
import java.util.Map;
import javax.xml.namespace.QName;
import jakarta.xml.ws.WebServiceException;
import org.w3c.dom.Element;
/**
*
* @author Jiandong Guo
*/
public class DefaultSTSIssuedTokenConfiguration extends STSIssuedTokenConfiguration{
private static final String PRE_CONFIGURED_STS = "PreconfiguredSTS";
private static final String NAMESPACE = "namespace";
private static final String CONFIG_NAMESPACE = "";
private static final String ENDPOINT = "endPoint";
private static final String METADATA = "metadata";
private static final String WSDL_LOCATION = "wsdlLocation";
private static final String SERVICE_NAME = "serviceName";
private static final String PORT_NAME = "portName";
private static final String REQUEST_SECURITY_TOKEN_TEMPLATE = "RequestSecurityTokenTemplate";
private static final String CLAIMS = "Claims";
private static final String DIALECT = "Dialect";
private static final String IDENTITY = "Identity";
private static final String WST_VERSION ="wstVersion";
private String tokenType = null;
private String keyType = null;
private long keySize = -1;
private String signatureAlg = null;
private String encAlg = null;
private String canAlg = null;
private String keyWrapAlg = null;
private Token oboToken = null;
private String signWith = null;
private String encryptWith = null;
private Claims claims = null;
public DefaultSTSIssuedTokenConfiguration(){
}
public DefaultSTSIssuedTokenConfiguration(String protocol, IssuedToken issuedToken, PolicyAssertion localToken){
if (protocol != null){
this.protocol = protocol;
}
parseAssertions(issuedToken, localToken);
}
public DefaultSTSIssuedTokenConfiguration(String stsEndpoint, String stsMEXAddress){
super(stsEndpoint, stsMEXAddress);
}
public DefaultSTSIssuedTokenConfiguration(String stsEndpoint,
String stsWSDLLocation, String stsServiceName, String stsPortName, String stsNamespace){
super(stsEndpoint, stsWSDLLocation, stsServiceName, stsPortName, stsNamespace);
}
public DefaultSTSIssuedTokenConfiguration(String protocol, String stsEndpoint, String stsMEXAddress){
super(protocol, stsEndpoint, stsMEXAddress);
}
public DefaultSTSIssuedTokenConfiguration(String protocol, String stsEndpoint,
String stsWSDLLocation, String stsServiceName, String stsPortName, String stsNamespace){
super(protocol, stsEndpoint, stsWSDLLocation, stsServiceName, stsPortName, stsNamespace);
}
public void setProtocol(String protocol){
this.protocol = protocol;
}
public void setSTSInfo(String stsEndpoint, String stsMEXAddress){
this.stsEndpoint = stsEndpoint;
this.stsMEXAddress = stsMEXAddress;
}
public void setSTSInfo (String protocol, String stsEndpoint,
String stsWSDLLocation, String stsServiceName, String stsPortName, String stsNamespace){
this.protocol = protocol;
this.stsEndpoint = stsEndpoint;
this.stsWSDLLocation = stsWSDLLocation;
this.stsServiceName = stsServiceName;
this.stsPortName = stsPortName;
this.stsNamespace = stsNamespace;
}
public void setTokenType(String tokenType){
this.tokenType = tokenType;
}
public void setKeyType(String keyType){
this.keyType = keyType;
}
public void setKeySize(long keySize){
this.keySize = keySize;
}
public void setSignWith(String signWithAlg){
this.signWith = signWithAlg;
}
public void setEncryptWith(String encWithAlg){
this.encryptWith = encWithAlg;
}
public void setSignatureAlgorithm(String sigAlg){
this.signatureAlg = sigAlg;
}
public void setEncryptionAlgorithm(String encAlg){
this.encAlg = encAlg;
}
public void setCanonicalizationAlgorithm(String canAlg){
this.canAlg = canAlg;
}
public void setKeyWrapAlgorithm(String keyWrapAlg){
this.keyWrapAlg = keyWrapAlg;
}
public void setClaims(Claims claims){
this.claims = claims;
}
public void setOBOToken(Token token){
this.oboToken = token;
}
public void setOBOToken(String username, String password){
this.oboToken = createUsernameToken(username, password);
}
public void setOBOToken(X509Certificate cert){
this.oboToken = this.createBinaryTokenForCertificate(cert);
}
public void setActAsToken(String username, String password){
this.getOtherOptions().put(STSIssuedTokenConfiguration.ACT_AS, this.createUsernameToken(username, password));
}
public void setActAsToken(X509Certificate cert){
this.getOtherOptions().put(STSIssuedTokenConfiguration.ACT_AS, this.createBinaryTokenForCertificate(cert));
}
@Override
public String getTokenType(){
return this.tokenType;
}
@Override
public String getKeyType(){
return this.keyType;
}
@Override
public long getKeySize(){
return this.keySize;
}
@Override
public String getSignatureAlgorithm(){
return this.signatureAlg;
}
@Override
public String getEncryptionAlgorithm(){
return this.encAlg;
}
@Override
public String getCanonicalizationAlgorithm(){
return this.canAlg;
}
@Override
public String getKeyWrapAlgorithm(){
return this.keyWrapAlg;
}
@Override
public String getSignWith(){
return signWith;
}
@Override
public String getEncryptWith(){
return encryptWith;
}
@Override
public Claims getClaims(){
return this.claims;
}
@Override
public Token getOBOToken(){
return this.oboToken;
}
public void setSecondaryIssuedTokenParameters(SecondaryIssuedTokenParameters sisPara){
this.sisPara = sisPara;
}
private void parseAssertions(IssuedToken issuedToken, PolicyAssertion localToken){
Issuer issuer = issuedToken.getIssuer();
URI stsURI = null;
if (issuer != null){
stsURI = issuedToken.getIssuer().getAddress().getURI();
if(issuer.getIdentity() != null){
this.getOtherOptions().put(IDENTITY, issuer.getIdentity());
}
}
// Get STS information from IssuedToken
if (stsURI != null){
this.stsEndpoint = stsURI.toString();
Address metadataIssuerAddress = issuer.getMetadataAddress();
if(metadataIssuerAddress != null){
URI metadataIssuerAddressURI = metadataIssuerAddress.getURI();
if(metadataIssuerAddressURI != null){
stsMEXAddress = metadataIssuerAddressURI.toString();
}
}
if(stsMEXAddress == null){
stsMEXAddress = stsEndpoint + "/mex";
}
}
String stsProtocol = null;
if (localToken != null){
// Get STS information from local configuration
if (PRE_CONFIGURED_STS.equals(localToken.getName().getLocalPart())) {
final Map attrs = localToken.getAttributes();
stsProtocol = trim(attrs.get(new QName(CONFIG_NAMESPACE, WST_VERSION)));
if (stsURI == null){
this.stsNamespace = trim(attrs.get(new QName(CONFIG_NAMESPACE,NAMESPACE)));
this.stsEndpoint = trim(attrs.get(new QName(CONFIG_NAMESPACE,ENDPOINT)));
if (stsEndpoint == null){
stsEndpoint = trim(attrs.get(new QName(CONFIG_NAMESPACE,ENDPOINT.toLowerCase())));
}
this.stsMEXAddress = trim(attrs.get(new QName(CONFIG_NAMESPACE, METADATA)));
if (stsMEXAddress == null){
this.stsWSDLLocation = trim(attrs.get(new QName(CONFIG_NAMESPACE,WSDL_LOCATION)));
this.stsServiceName = trim(attrs.get(new QName(CONFIG_NAMESPACE,SERVICE_NAME)));
this.stsPortName = trim(attrs.get(new QName(CONFIG_NAMESPACE,PORT_NAME)));
}
}
// check if shareToken is set
String shareToken = attrs.get(new QName(CONFIG_NAMESPACE, SHARE_TOKEN));
if ("true".equals(shareToken)){
this.getOtherOptions().put(SHARE_TOKEN, shareToken);
}
// check if renewExpiredToken is set
String renewExpiredToken = attrs.get(new QName(CONFIG_NAMESPACE, RENEW_EXPIRED_TOKEN));
if ("true".equals(renewExpiredToken)){
this.getOtherOptions().put(RENEW_EXPIRED_TOKEN, renewExpiredToken);
}
// maxClockSkew
String maxClockSkew = attrs.get(new QName(CONFIG_NAMESPACE, MAX_CLOCK_SKEW));
if (maxClockSkew != null){
this.getOtherOptions().put(MAX_CLOCK_SKEW, maxClockSkew);
}
// handle LifeTime
if (localToken.hasParameters()){
Iterator pas = localToken.getParametersIterator();
while (pas.hasNext()){
PolicyAssertion pa = pas.next();
if (LIFE_TIME.equals(pa.getName().getLocalPart())){
this.getOtherOptions().put(LIFE_TIME, Integer.parseInt(pa.getValue()));
break;
}
}
}
}
}
if (stsProtocol == null){
stsProtocol = protocol;
}
RequestSecurityTokenTemplate rstt = issuedToken.getRequestSecurityTokenTemplate();
if (rstt != null){
Claims claims = null;
if (protocol.equals(WSTrustVersion.WS_TRUST_13.getNamespaceURI())){
if(issuedToken.getClaims() != null){
claims = getClaims(issuedToken, stsProtocol);
}
}else{
if(rstt.getClaims() != null){
claims = getClaims(issuedToken, stsProtocol);
}
}
if (!protocol.equals(stsProtocol)){
// Mixed versions of trust
copy(rstt, stsProtocol, protocol);
setClaims(claims);
protocol = stsProtocol;
}else if (protocol.equals(WSTrustVersion.WS_TRUST_13.getNamespaceURI())){
SecondaryIssuedTokenParametersImpl sitp = new SecondaryIssuedTokenParametersImpl();
copy(rstt, sitp);
sitp.setClaims(claims);
this.sisPara = sitp;
}else{
copy(rstt);
setClaims(claims);
}
}
}
private Claims getClaims(final IssuedToken issuedToken, String stsWstProtocol){
Claims cs = null;
try {
//Element claimsEle = null;
if (protocol.equals(WSTrustVersion.WS_TRUST_13.getNamespaceURI())){
Element claimsEle = issuedToken.getClaims().getClaimsAsElement();
cs = WSTrustElementFactory.newInstance(WSTrustVersion.WS_TRUST_13.getNamespaceURI()).createClaims(claimsEle);
}else{
RequestSecurityTokenTemplate rstt = issuedToken.getRequestSecurityTokenTemplate();
Element claimsEle = rstt.getClaims().getClaimsAsElement();
cs = WSTrustElementFactory.newInstance(WSTrustVersion.WS_TRUST_10.getNamespaceURI()).createClaims(claimsEle);
}
cs = WSTrustElementFactory.newInstance(WSTrustVersion.getInstance(stsWstProtocol)).createClaims(cs);
} catch (Exception e) {
throw new WebServiceException(e);
}
return cs;
}
private void copy(RequestSecurityTokenTemplate rstt){
this.setTokenType(trim(rstt.getTokenType()));
this.setKeyType(trim(rstt.getKeyType()));
this.setKeySize(rstt.getKeySize());
this.setSignWith(trim(rstt.getSignWith()));
this.setEncryptWith(trim(rstt.getEncryptWith()));
this.setSignatureAlgorithm(trim(rstt.getSignatureAlgorithm()));
this.setEncryptionAlgorithm(trim(rstt.getEncryptionAlgorithm()));
this.setCanonicalizationAlgorithm(trim(rstt.getCanonicalizationAlgorithm()));
}
private void copy(RequestSecurityTokenTemplate rstt, SecondaryIssuedTokenParametersImpl sitp){
sitp.setTokenType(trim(rstt.getTokenType()));
sitp.setKeyType(trim(rstt.getKeyType()));
sitp.setKeySize(rstt.getKeySize());
sitp.setSignWith(trim(rstt.getSignWith()));
sitp.setEncryptWith(trim(rstt.getEncryptWith()));
sitp.setSignatureAlgorithm(trim(rstt.getSignatureAlgorithm()));
sitp.setEncryptionAlgorithm(trim(rstt.getEncryptionAlgorithm()));
sitp.setCanonicalizationAlgorithm(trim(rstt.getCanonicalizationAlgorithm()));
sitp.setKeyWrapAlgorithm(trim(rstt.getKeyWrapAlgorithm()));
}
private void copy(RequestSecurityTokenTemplate rstt, String stsWstProtocol, String serviceWstProtocol){
// Convert KeyType
WSTrustVersion stsWstVer = WSTrustVersion.getInstance(stsWstProtocol);
WSTrustVersion serviceWstVer = WSTrustVersion.getInstance(serviceWstProtocol);
String rsttKeyType = trim(rstt.getKeyType());
if (serviceWstVer.getPublicKeyTypeURI().equals(rsttKeyType)){
setKeyType(stsWstVer.getPublicKeyTypeURI());
}else if (serviceWstVer.getSymmetricKeyTypeURI().equals(rsttKeyType)){
setKeyType(stsWstVer.getSymmetricKeyTypeURI());
}else if (serviceWstVer.getBearerKeyTypeURI().equals(rsttKeyType)){
setKeyType(stsWstVer.getBearerKeyTypeURI());
}
this.setTokenType(trim(rstt.getTokenType()));
this.setKeySize(rstt.getKeySize());
this.setSignWith(trim(rstt.getSignWith()));
this.setEncryptWith(trim(rstt.getEncryptWith()));
this.setSignatureAlgorithm(trim(rstt.getSignatureAlgorithm()));
this.setEncryptionAlgorithm(trim(rstt.getEncryptionAlgorithm()));
this.setCanonicalizationAlgorithm(trim(rstt.getCanonicalizationAlgorithm()));
}
public void copy(STSIssuedTokenConfiguration config){
if (config.getProtocol() != null){
this.protocol = config.getProtocol();
}
if (stsEndpoint == null && config.getSTSEndpoint() != null){
this.stsEndpoint = config.getSTSEndpoint();
if (config.getSTSMEXAddress()!= null){
this.stsMEXAddress = config.getSTSMEXAddress();
}else if (config.getSTSWSDLLocation()!= null){
this.stsWSDLLocation = config.getSTSWSDLLocation();
this.stsServiceName = config.getSTSServiceName();
this.stsPortName = config.getSTSPortName();
this.stsNamespace = config.getSTSNamespace();
}
}
if (tokenType == null && config.getTokenType() != null){
this.tokenType = config.getTokenType();
}
if (keyType == null && config.getKeyType() != null){
this.keyType = config.getKeyType();
}
if (keySize < 1 && config.getKeySize() > 0){
this.keySize = config.getKeySize();
}
if (signatureAlg == null && config.getSignatureAlgorithm() != null){
this.signatureAlg = config.getSignatureAlgorithm();
}
if (encAlg == null && config.getEncryptionAlgorithm() != null){
this.encAlg = config.getEncryptionAlgorithm();
}
if (config.getCanonicalizationAlgorithm() != null){
this.canAlg = config.getCanonicalizationAlgorithm();
}
if (keyWrapAlg == null && config.getKeyWrapAlgorithm() != null){
this.keyWrapAlg = config.getKeyWrapAlgorithm();
}
if (signWith == null && config.getSignWith() != null){
this.signWith = config.getSignWith();
}
if (encryptWith == null && config.getEncryptWith() != null){
this.encryptWith = config.getEncryptWith();
}
if (config.getOBOToken() != null){
this.oboToken = config.getOBOToken();
}
if (claims == null && config.getClaims() != null){
this.claims = config.getClaims();
}
this.getOtherOptions().putAll(config.getOtherOptions());
if (config.getOtherOptions().containsKey(ISSUED_TOKEN)){
this.getOtherOptions().remove(ISSUED_TOKEN);
}
}
private Token createUsernameToken(String username, String password){
ObjectFactory fact = new ObjectFactory();
UsernameTokenType ut = fact.createUsernameTokenType();
AttributedString un = fact.createAttributedString();
un.setValue(username);
AttributedString pwd = fact.createAttributedString();
pwd.setValue(password);
ut.setUsername(un);
ut.setPassword(pwd);
return new GenericToken(fact.createUsernameToken(ut));
}
private Token createBinaryTokenForCertificate(X509Certificate cert){
ObjectFactory fact = new ObjectFactory();
BinarySecurityTokenType bst = fact.createBinarySecurityTokenType();
bst.setValueType(MessageConstants.X509v3_NS);
bst.setEncodingType(MessageConstants.BASE64_ENCODING_NS);
try{
bst.setValue(cert.getEncoded());
}catch(CertificateEncodingException ex){
throw new RuntimeException(ex);
}
return new GenericToken(fact.createBinarySecurityToken(bst));
}
private String trim(String uriStr){
if (uriStr != null){
return uriStr.trim();
}
return uriStr;
}
}