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.WSSecurityException;
23  import org.apache.ws.security.components.crypto.Crypto;
24  import org.apache.ws.security.components.crypto.CryptoFactory;
25  import org.apache.ws.security.components.crypto.CryptoType;
26  import org.apache.ws.security.saml.ext.SAMLCallback;
27  import org.apache.ws.security.saml.ext.bean.AuthenticationStatementBean;
28  import org.apache.ws.security.saml.ext.bean.KeyInfoBean;
29  import org.apache.ws.security.saml.ext.bean.SubjectBean;
30  import org.apache.ws.security.saml.ext.builder.SAML1Constants;
31  import org.opensaml.common.SAMLVersion;
32  
33  import javax.security.auth.callback.Callback;
34  import javax.security.auth.callback.CallbackHandler;
35  import javax.security.auth.callback.UnsupportedCallbackException;
36  import java.io.IOException;
37  import java.security.cert.X509Certificate;
38  import java.util.Collections;
39  
40  /**
41   * A Callback Handler implementation for a SAML 1.1 authentication assertion using
42   * Holder of Key.
43   */
44  public class SAML1AuthnHOKHandler implements CallbackHandler {
45      
46      private String subjectName = "uid=joe,ou=people,ou=saml-demo,o=example.com";
47      private String subjectQualifier = "www.example.com";
48      private X509Certificate[] certs;
49      
50      public SAML1AuthnHOKHandler() throws WSSecurityException {
51          Crypto crypto = CryptoFactory.getInstance("wss40.properties");
52          CryptoType cryptoType = new CryptoType(CryptoType.TYPE.ALIAS);
53          cryptoType.setAlias("wss40");
54          certs = crypto.getX509Certificates(cryptoType);
55      }
56      
57      public void handle(Callback[] callbacks)
58          throws IOException, UnsupportedCallbackException {
59          for (int i = 0; i < callbacks.length; i++) {
60              if (callbacks[i] instanceof SAMLCallback) {
61                  SAMLCallback callback = (SAMLCallback) callbacks[i];
62                  callback.setSamlVersion(SAMLVersion.VERSION_11);
63                  SubjectBean subjectBean = 
64                      new SubjectBean(
65                          subjectName, subjectQualifier, SAML1Constants.CONF_HOLDER_KEY
66                      );
67                  KeyInfoBean keyInfo = new KeyInfoBean();
68                  keyInfo.setCertificate(certs[0]);
69                  subjectBean.setKeyInfo(keyInfo);
70                  AuthenticationStatementBean authBean = new AuthenticationStatementBean();
71                  authBean.setSubject(subjectBean);
72                  authBean.setAuthenticationMethod("Password");
73                  callback.setAuthenticationStatementData(Collections.singletonList(authBean));
74              } else {
75                  throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
76              }
77          }
78      }
79  }