-
Notifications
You must be signed in to change notification settings - Fork 30
/
TSAuthConfigProvider.java
231 lines (206 loc) · 10.6 KB
/
TSAuthConfigProvider.java
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* Copyright (c) 2007, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package ee.jakarta.tck.authentication.test.basic.sam.config;
import static java.util.logging.Level.INFO;
import ee.jakarta.tck.authentication.test.basic.sam.AuthDataCallbackHandler;
import ee.jakarta.tck.authentication.test.basic.sam.TSFileHandler;
import ee.jakarta.tck.authentication.test.basic.servlet.JASPICData;
import ee.jakarta.tck.authentication.test.common.logging.server.TSLogger;
import ee.jakarta.tck.authentication.test.common.logging.server.TSXMLFormatter;
import jakarta.security.auth.message.AuthException;
import jakarta.security.auth.message.config.AuthConfigFactory;
import jakarta.security.auth.message.config.ClientAuthConfig;
import jakarta.security.auth.message.config.ServerAuthConfig;
import java.util.HashMap;
import java.util.Map;
import javax.security.auth.callback.CallbackHandler;
/**
*
* @author Raja Perumal
*/
public class TSAuthConfigProvider implements jakarta.security.auth.message.config.AuthConfigProvider {
private static TSLogger logger;
private static Map<String, Object> properties;
private HashMap clientAuthConfigMap = new HashMap();
private HashMap serverAuthConfigMap = new HashMap();
// This will be called when a vendor registers TSAuthConfigProvider
public TSAuthConfigProvider(Map props, AuthConfigFactory factory) {
properties = props;
// For self registration
if (factory != null) {
factory.registerConfigProvider(this, null, null, "TSAuthConfig Provider self registration");
}
if (logger == null) {
initializeTSLogger();
}
}
// TSAuthConfigFactory invokes this constructor with TSLogger
public TSAuthConfigProvider(Map props, AuthConfigFactory factory, TSLogger tsLogger) {
properties = props;
// For self registration
if (factory != null) {
factory.registerConfigProvider(this, null, null, "TSAuthConfig Provider self registration");
}
if (tsLogger != null)
logger = tsLogger;
}
/**
* Get an instance of ClientAuthConfig from this provider.
*
* <p>
* The implementation of this method returns a ClientAuthConfig instance that describes the configuration of
* ClientAuthModules at a given message layer, and for use in an identified application context.
*
* @param layer a String identifying the message layer for the returned ClientAuthConfig object. This argument must not
* be null.
*
* @param appContext a String that identifies the messaging context for the returned ClientAuthConfig object. This
* argument must not be null.
*
* @param handler a CallbackHandler to be passed to the ClientAuthModules encapsulated by ClientAuthContext objects
* derived from the returned ClientAuthConfig. This argument may be null, in which case the implementation may assign a
* default handler to the configuration.
*
* @return a ClientAuthConfig Object that describes the configuration of ClientAuthModules at the message layer and
* messaging context identified by the layer and appContext arguments. This method does not return null.
*
* @exception AuthException if this provider does not support the assignment of a default CallbackHandler to the
* returned ClientAuthConfig.
*
* @exception SecurityException if the caller does not have permission to retrieve the configuration.
*
* The CallbackHandler assigned to the configuration must support the Callback objects required to be supported by the
* profile of this specification being followed by the messaging runtime. The CallbackHandler instance must be
* initialized with any application context needed to process the required callbacks on behalf of the corresponding
* application.
*/
@Override
public ClientAuthConfig getClientAuthConfig(String layer, String appContext, CallbackHandler handler) throws AuthException {
String logStr = "TSAuthConfigProvider.getClientAuthConfig called for " + "layer=" + layer + " : " + "appContext=" + appContext;
logger.log(INFO, logStr);
try {
if (handler == null) {
// instantiate a default callback handler that gets
// username and password from the environment using
// system property j2eelogin.name j2eelogin.password
handler = new AuthDataCallbackHandler();
} else {
// even if we receive vendor callbackhandler replace it with our own
// callbackhandler for jaspic test.
System.out.println("Received callbackHandler =" + handler.getClass().getName());
handler = new AuthDataCallbackHandler();
}
ClientAuthConfig clientAuthConfig = new TSClientAuthConfig(layer, appContext, handler, properties, logger);
clientAuthConfigMap.put(layer + appContext, clientAuthConfig);
return clientAuthConfig;
} catch (Exception e) {
throw new AuthException(e.getMessage());
}
}
/**
* Get an instance of ServerAuthConfig from this provider.
*
* <p>
* The implementation of this method returns a ServerAuthConfig instance that describes the configuration of
* ServerAuthModules at a given message layer, and for a particular application context.
*
* @param layer a String identifying the message layer for the returned ServerAuthConfig object. This argument must not
* be null.
*
* @param appContext a String that identifies the messaging context for the returned ServerAuthConfig object. This
* argument must not be null.
*
* @param handler a CallbackHandler to be passed to the ServerAuthModules encapsulated by ServerAuthContext objects
* derived from the returned ServerAuthConfig. This argument may be null, in which case the implementation may assign a
* default handler to the configuration.
*
* @return a ServerAuthConfig Object that describes the configuration of ServerAuthModules at a given message layer, and
* for a particular application context. This method does not return null.
*
* @exception AuthException if this provider does not support the assignment of a default CallbackHandler to the
* returned ServerAuthConfig.
*
* @exception SecurityException if the caller does not have permission to retrieve the configuration.
* <p>
* The CallbackHandler assigned to the configuration must support the Callback objects required to be supported by the
* profile of this specification being followed by the messaging runtime. The CallbackHandler instance must be
* initialized with any application context needed to process the required callbacks on behalf of the corresponding
* application.
*/
@Override
public ServerAuthConfig getServerAuthConfig(String layer, String appContext, CallbackHandler handler) throws AuthException {
String logStr = "TSAuthConfigProvider.getServerAuthConfig called for " + "layer=" + layer + " : " + "appContext=" + appContext;
logger.log(INFO, logStr);
try {
if ((!layer.equals(JASPICData.LAYER_SERVLET)) && (handler == null)) {
// instantiate a default callback handler that gets
// username and password from the environment using
// system property j2eelogin.name j2eelogin.password
handler = new AuthDataCallbackHandler();
} else if ((layer.equals(JASPICData.LAYER_SERVLET)) && (handler == null)) {
// this is used to help verify assertion JASPI:SPEC:71 which
// that we should NOT have a null cbh passed in
String msg = "FAILURE: layer=" + layer + " appContext=" + appContext;
msg += " getServerAuthConfig() received CallbackHandler=null";
logger.log(INFO, msg);
}
ServerAuthConfig serverAuthConfig = null;
if (JASPICData.LAYER_SOAP.equals(layer)) {
serverAuthConfig = new SOAPTSServerAuthConfig(layer, appContext, handler, properties, logger);
} else {
serverAuthConfig = new TSServerAuthConfig(layer, appContext, handler, properties, logger);
}
serverAuthConfigMap.put(layer + appContext, serverAuthConfig);
return serverAuthConfig;
} catch (Exception e) {
throw new AuthException(e.getMessage());
}
}
/**
* Causes a dynamic configuration provider to update its internal state such that any resulting change to its state is
* reflected in the corresponding authentication context configuration objects previously created by the provider within
* the current process context.
*
* @exception AuthException if an error occured during the refresh.
*
* @exception SecurityException if the caller does not have permission to refresh the provider.
*/
@Override
public void refresh() {
}
private static void initializeTSLogger() {
String logFileLocation = null;
if (logger != null)
return;
else {
try {
logFileLocation = System.getProperty("log.file.location");
if (logFileLocation != null) {
logger = TSLogger.getTSLogger(JASPICData.LOGGER_NAME);
boolean appendMode = true;
// if log file already exists, just append to it
TSFileHandler fileHandler = new TSFileHandler(logFileLocation + "/" + JASPICData.DEFAULT_LOG_FILE, appendMode);
fileHandler.setFormatter(new TSXMLFormatter());
logger.addHandler(fileHandler);
} else {
throw new RuntimeException("log.file.location not set");
}
} catch (Exception e) {
throw new RuntimeException("TSLogger Initialization failed", e);
}
}
}
}