1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.soap.security.xwss.callback.jaas;
18  
19  import java.io.InputStream;
20  import java.security.KeyStore;
21  import java.security.cert.X509Certificate;
22  
23  import org.springframework.core.io.ClassPathResource;
24  
25  import com.sun.xml.wss.impl.callback.CertificateValidationCallback;
26  import org.junit.Assert;
27  import org.junit.Before;
28  import org.junit.Test;
29  
30  public class JaasCertificateValidationCallbackHandlerTest {
31  
32      private JaasCertificateValidationCallbackHandler callbackHandler;
33  
34      private CertificateValidationCallback callback;
35  
36      @Before
37      public void setUp() throws Exception {
38          System.setProperty("java.security.auth.login.config", getClass().getResource("jaas.config").toString());
39          callbackHandler = new JaasCertificateValidationCallbackHandler();
40          callbackHandler.setLoginContextName("Certificate");
41          KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
42          InputStream is = null;
43          try {
44              is = new ClassPathResource("/org/springframework/ws/soap/security/xwss/test-keystore.jks").getInputStream();
45              keyStore.load(is, "password".toCharArray());
46          }
47          finally {
48              if (is != null) {
49                  is.close();
50              }
51          }
52          X509Certificate certificate = (X509Certificate) keyStore.getCertificate("alias");
53          callback = new CertificateValidationCallback(certificate);
54      }
55  
56      @Test
57      public void testValidateCertificateValid() throws Exception {
58          callbackHandler.handleInternal(callback);
59          boolean authenticated = callback.getResult();
60          Assert.assertTrue("Not authenticated", authenticated);
61      }
62  
63  }