A API da biblioteca SAML pode ser usada para incluir atributos ou para excluir atributos de um token SAML que não está criptografado e, em seguida, assinar ou reassinar o token SAML. Sempre que ocorrer uma ação para modificar o objeto do token SAML, se já houver uma assinatura no objeto, a assinatura digital existente será removida.
Sobre Esta Tarefa
O procedimento a seguir descreve como modificar um objeto SAMLToken existente e, em seguida, aplicar uma nova assinatura digital ao token.
Essa tarefa não mostra como obter o objeto SAMLToken existente.
O objeto SAMLToken existente pode vir de um SOAP de entrada por meio do estado compartilhado LoginModule, de uma SSO da web de SAML por meio do Assunto de Segurança base, de um STS que usa o cliente de confiança ou de um token autoemitido, criado com as APIs SAML.
Quando as APIs SAML inicializam objetos de configuração, a configuração é obtida a partir de SamlIssuerConfig.properties.
É possível escolher usar as configurações existentes em SamlIssuerConfig.properties ou substituí-las. Esse procedimento mostra como substituir todas as configurações neste arquivo.
Procedimento
- Clone o token SAML existente
import java.util.ArrayList;
import org.apache.axiom.om.OMElement;
import com.ibm.websphere.wssecurity.wssapi.token.SAMLToken;
import com.ibm.websphere.wssecurity.wssapi.token.SAMLTokenFactory;
import com.ibm.wsspi.wssecurity.saml.config.ProviderConfig;
import com.ibm.wsspi.wssecurity.saml.config.RequesterConfig;
import com.ibm.wsspi.wssecurity.saml.data.SAMLAttribute;
import com.ibm.wsspi.wssecurity.wssapi.OMStructure;
....
//someSAMLToken is the existing SAMLToken object
SAMLTokenFactory samlFactory = null;
//initialize the desired SAML token factory
//samlFactory = SAMLTokenFactory.getInstance(SAMLTokenFactory.WssSamlV11Token11);
samlFactory = SAMLTokenFactory.getInstance(SAMLTokenFactory.WssSamlV20Token11);
//clone the existing SAMLToken object if desired.
SAMLToken mySamlToken = factory.newSAMLToken(someSamlToken);
- Inclua atributos ou exclua atributos do token
//add a single attribute
SAMLAttribute sattribute1 = new SAMLAttribute("Purchases", new String[] {"TooMany"}, null, null, null, null);
mySamlToken.addAttribute(sattribute1);
//after this first addAttribute, there will not be a digital signature in the
//token's XML. Doing a token modification invalidated the signature.
//add a list of attributes
SAMLAttribute sattribute2 = new SAMLAttribute("Address", new String[] {"Austin, Texas"},null,null,"IBM NameFormat","IBM FriendlyName");
SAMLAttribute sattribute3 = new SAMLAttribute("Membership",new String[] {"Blue team", "Green Team"},null,null,null,null );
ArrayList al = new ArrayList();
al.add(sattribute2);
al.add(sattribute3);
mySamlToken.addAttribute(al);
//delete an attribute
mySamlToken.deleteAttribute(sattribute3);
....
- Reassine o token SAML.
RequesterConfig reqData = null;
//initialize the desired requester config
//reqData = samlFactory.newSenderVouchesTokenGenerateConfig();
reqData = samlFactory.newBearerTokenGenerateConfig();
//initialize the provider config object with an issuer name
ProviderConfig samlIssuerCfg = samlFactory.newDefaultProviderConfig("myIssuer");
//Or preserve the existing issuer by setting the issuer URI to null
//samlIssuerCfg.setIssuerURI(null);
//set the keystore information for use with digital signature in the provider config object
KeyStoreConfig ksc = samlFactory.newKeyStoreConfig( "jks", "/myx509.ks", "myx509");
samlIssuerCfg.setKeyStoreConfig(ksc);
//set the key information for use with digital signature in the provider config object
KeyInformationConfig kic = samlFactory.newKeyInformationConfig("mySignAlias", "password", "CN=ME");
samlIssuerCfg.setKeyInformationConfig(kic);
//create a new SAMLToken object that is a signed clone of the input token
SAMLToken myNewSamlToken = samlFactory.newSAMLToken(mySamlToken,reqData,samlIssuerCfg);
- Inspecione o XML do token SAML para ver as modificações
//get the SAML Assertion element
OMElement samlElement = ((OMStructure) myNewSamlToken.getXML()).getNode();
//convert the element to a String
String xmlString = samlElement.toString();