1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package net.sf.jguard.ext.authentication.loginmodules;
29
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.Map;
33 import java.util.Set;
34
35 import java.util.logging.Level;
36 import javax.security.auth.Subject;
37 import javax.security.auth.callback.CallbackHandler;
38 import javax.security.auth.login.CredentialException;
39 import javax.security.auth.login.LoginException;
40 import javax.security.auth.spi.LoginModule;
41
42 import net.sf.jguard.core.CoreConstants;
43 import net.sf.jguard.core.PolicyEnforcementPointOptions;
44 import net.sf.jguard.core.authentication.AuthenticationException;
45 import net.sf.jguard.core.authentication.credentials.JGuardCredential;
46 import net.sf.jguard.core.authentication.manager.AuthenticationManager;
47 import net.sf.jguard.core.authentication.manager.AuthenticationManagerFactory;
48 import net.sf.jguard.ext.authentication.manager.XmlAuthenticationManager;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public class XmlLoginModule extends UserLoginModule implements LoginModule{
70
71
72
73 private static final Logger logger = LoggerFactory.getLogger(XmlLoginModule.class.getName());
74
75 private Set users;
76
77
78
79
80
81
82
83
84 public void initialize(Subject subj,CallbackHandler cbkHandler,Map sState,Map opts) {
85 super.initialize(subj,cbkHandler,sState,opts);
86
87 if (AuthenticationManagerFactory.getAuthenticationManager() == null){
88
89 Map newOpts = new HashMap();
90 newOpts.putAll(opts);
91
92 if (opts.get(PolicyEnforcementPointOptions.APPLICATION_NAME.getLabel()) != null){
93
94 newOpts.put(PolicyEnforcementPointOptions.APPLICATION_NAME.getLabel(), opts.get(PolicyEnforcementPointOptions.APPLICATION_NAME.getLabel()));
95
96 }else{
97 String appNameProp = System.getProperty("net.sf.jguard.application.name");
98
99 if (appNameProp != null){
100
101 newOpts.put(PolicyEnforcementPointOptions.APPLICATION_NAME.getLabel(), appNameProp);
102
103 }else{
104 String appNameJMXProp = System.getProperty("com.sun.management.jmxremote.login.config");
105
106 if (appNameJMXProp != null){
107 logger.warn("Using JMX config for application name! " +
108 "If you're not running JMX, prefer XmlLoginModule options or net.sf.jguard.applicationName vmarg");
109 newOpts.put(PolicyEnforcementPointOptions.APPLICATION_NAME.getLabel(), appNameJMXProp);
110
111 }else{
112
113 newOpts.put(PolicyEnforcementPointOptions.APPLICATION_NAME.getLabel(), CoreConstants.DEFAULT_APPLICATION_NAME);
114 }
115 }
116 }
117
118 try {
119 AuthenticationManagerFactory.setAuthenticationManager(AuthenticationManagerFactory.createAuthenticationManager(XmlAuthenticationManager.class, newOpts));
120 } catch (AuthenticationException e) {
121 logger.error( " initialize ", e);
122 }
123 }
124 try {
125 users = AuthenticationManagerFactory.getAuthenticationManager().getUsers();
126 } catch (AuthenticationException e) {
127 logger.error( " initialize ", e);
128 }
129 }
130
131
132
133
134
135
136
137 public boolean login() throws LoginException{
138 super.login();
139 if (skipPasswordCheck || password==null) {
140 return false;
141 }
142 AuthenticationManager authenticationManager = AuthenticationManagerFactory.getAuthenticationManager();
143 JGuardCredential loginCredential = new JGuardCredential();
144 loginCredential.setName(authenticationManager.getCredentialId());
145 loginCredential.setValue(login);
146
147 JGuardCredential passwordCredential = new JGuardCredential();
148 passwordCredential.setName(authenticationManager.getCredentialPassword());
149 passwordCredential.setValue(new String(password));
150
151 Subject user;
152 Iterator it = users.iterator();
153 boolean authenticationSucceed = false;
154
155 while(it.hasNext()){
156 user = (Subject)it.next();
157 Set privateCredentialsTemp = user.getPrivateCredentials();
158 if(privateCredentialsTemp.contains(loginCredential)){
159 if((privateCredentialsTemp.contains(passwordCredential))||skipPasswordCheck){
160
161
162 globalPrincipals = user.getPrincipals();
163 globalPrivateCredentials = user.getPrivateCredentials();
164 globalPublicCredentials = user.getPublicCredentials();
165 authenticationSucceed = true;
166 }
167 break;
168 }
169 }
170
171 if(authenticationSucceed==false){
172 loginOK = false;
173 throw new CredentialException(XmlLoginModule.LOGIN_ERROR);
174 }
175
176 return true;
177 }
178
179
180
181
182
183 }