1   /*
2    * Copyright 2006 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.security.Principal;
20  import java.util.Iterator;
21  import javax.security.auth.Subject;
22  import javax.security.auth.callback.CallbackHandler;
23  import javax.security.auth.login.LoginException;
24  import javax.security.auth.spi.LoginModule;
25  import javax.security.auth.x500.X500Principal;
26  
27  public class CertificateLoginModule implements LoginModule {
28  
29      private Subject subject;
30  
31      private boolean loginSuccessful = false;
32  
33      public boolean abort() {
34          return true;
35      }
36  
37      public boolean commit() {
38          if (!loginSuccessful) {
39              subject.getPrincipals().clear();
40              subject.getPrivateCredentials().clear();
41              return false;
42          }
43          return true;
44      }
45  
46      public void initialize(Subject subject,
47                             CallbackHandler callbackHandler,
48                             java.util.Map sharedState,
49                             java.util.Map options) {
50          this.subject = subject;
51      }
52  
53      public boolean login() throws LoginException {
54          if (subject == null) {
55              return false;
56          }
57  
58          String name = getName(subject);
59  
60          loginSuccessful = "CN=Arjen Poutsma,OU=Spring-WS,O=Interface21,L=Amsterdam,ST=Unknown,C=NL".equals(name);
61          return loginSuccessful;
62      }
63  
64      public boolean logout() {
65          subject.getPrincipals().clear();
66          subject.getPrivateCredentials().clear();
67          return true;
68      }
69  
70      private String getName(Subject subject) {
71          for (Iterator iterator = subject.getPrincipals().iterator(); iterator.hasNext();) {
72              Principal principal = (Principal) iterator.next();
73              if (principal instanceof X500Principal) {
74                  return principal.getName();
75              }
76          }
77          return null;
78      }
79  }