1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements. See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership. The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License. You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied. See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.ws.security.common;
21  
22  import org.apache.ws.security.components.crypto.Crypto;
23  import org.apache.ws.security.components.crypto.CryptoFactory;
24  import org.apache.ws.security.components.crypto.CryptoType;
25  import org.apache.ws.security.saml.ext.SAMLCallback;
26  import org.apache.ws.security.saml.ext.bean.KeyInfoBean;
27  import org.apache.ws.security.saml.ext.bean.SubjectBean;
28  import org.apache.ws.security.saml.ext.builder.SAML2Constants;
29  import org.opensaml.common.SAMLVersion;
30  
31  import javax.security.auth.callback.Callback;
32  import javax.security.auth.callback.UnsupportedCallbackException;
33  import java.io.IOException;
34  
35  /**
36   * A Callback Handler implementation for a SAML 2 assertion. By default it creates an
37   * authentication assertion using Sender Vouches.
38   */
39  public class SAML2CallbackHandler extends AbstractSAMLCallbackHandler {
40      
41      public SAML2CallbackHandler() throws Exception {
42          if (certs == null) {
43              Crypto crypto = CryptoFactory.getInstance("wss40.properties");
44              CryptoType cryptoType = new CryptoType(CryptoType.TYPE.ALIAS);
45              cryptoType.setAlias("wss40");
46              certs = crypto.getX509Certificates(cryptoType);
47          }
48          
49          subjectName = "uid=joe,ou=people,ou=saml-demo,o=example.com";
50          subjectQualifier = "www.example.com";
51          confirmationMethod = SAML2Constants.CONF_SENDER_VOUCHES;
52      }
53      
54      public void handle(Callback[] callbacks)
55          throws IOException, UnsupportedCallbackException {
56          for (int i = 0; i < callbacks.length; i++) {
57              if (callbacks[i] instanceof SAMLCallback) {
58                  SAMLCallback callback = (SAMLCallback) callbacks[i];
59                  callback.setSamlVersion(SAMLVersion.VERSION_20);
60                  callback.setIssuer(issuer);
61                  if (conditions != null) {
62                      callback.setConditions(conditions);
63                  }
64                  
65                  SubjectBean subjectBean = 
66                      new SubjectBean(
67                          subjectName, subjectQualifier, confirmationMethod
68                      );
69                  if (subjectNameIDFormat != null) {
70                      subjectBean.setSubjectNameIDFormat(subjectNameIDFormat);
71                  }
72                  subjectBean.setSubjectConfirmationData(subjectConfirmationData);
73                  if (SAML2Constants.CONF_HOLDER_KEY.equals(confirmationMethod)) {
74                      try {
75                          KeyInfoBean keyInfo = createKeyInfo();
76                          subjectBean.setKeyInfo(keyInfo);
77                      } catch (Exception ex) {
78                          throw new IOException("Problem creating KeyInfo: " +  ex.getMessage());
79                      }
80                  }
81                  callback.setSubject(subjectBean);
82                  createAndSetStatement(null, callback);
83              } else {
84                  throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
85              }
86          }
87      }
88      
89  }