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.SAML1Constants;
29  import org.opensaml.common.SAMLVersion;
30  
31  import javax.security.auth.callback.Callback;
32  import javax.security.auth.callback.UnsupportedCallbackException;
33  
34  import java.io.IOException;
35  
36  /**
37   * A Callback Handler implementation for a SAML 1.1 assertion. By default it creates an
38   * authentication assertion using Sender Vouches.
39   */
40  public class SAML1CallbackHandler extends AbstractSAMLCallbackHandler {
41      
42      public SAML1CallbackHandler() throws Exception {
43          if (certs == null) {
44              Crypto crypto = CryptoFactory.getInstance("wss40.properties");
45              CryptoType cryptoType = new CryptoType(CryptoType.TYPE.ALIAS);
46              cryptoType.setAlias("wss40");
47              certs = crypto.getX509Certificates(cryptoType);
48          }
49          
50          subjectName = "uid=joe,ou=people,ou=saml-demo,o=example.com";
51          subjectQualifier = "www.example.com";
52          confirmationMethod = SAML1Constants.CONF_SENDER_VOUCHES;
53      }
54      
55      public void handle(Callback[] callbacks)
56          throws IOException, UnsupportedCallbackException {
57          for (int i = 0; i < callbacks.length; i++) {
58              if (callbacks[i] instanceof SAMLCallback) {
59                  SAMLCallback callback = (SAMLCallback) callbacks[i];
60                  callback.setSamlVersion(SAMLVersion.VERSION_11);
61                  callback.setIssuer(issuer);
62                  if (conditions != null) {
63                      callback.setConditions(conditions);
64                  }
65                  
66                  SubjectBean subjectBean = 
67                      new SubjectBean(
68                          subjectName, subjectQualifier, confirmationMethod
69                      );
70                  if (subjectNameIDFormat != null) {
71                      subjectBean.setSubjectNameIDFormat(subjectNameIDFormat);
72                  }
73                  if (SAML1Constants.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                  createAndSetStatement(subjectBean, callback);
82              } else {
83                  throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
84              }
85          }
86      }
87      
88  }