View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2005, 2009
3   
4     THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5     ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6     CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7   
8     You can obtain a current copy of the Eclipse Public License from
9     http://www.opensource.org/licenses/eclipse-1.0.php
10  
11    @author : Roberto Pineiro, IBM, roberto.pineiro@us.ibm.com
12   * @author : Chung-hao Tan, IBM, chungtan@us.ibm.com
13   * 
14   * 
15   * Change History
16   * Flag       Date        Prog         Description
17   *------------------------------------------------------------------------------- 
18   *   17970    2005-08-11  pineiro5     Logon from z/OS not possible
19   * 1516242    2006-07-05  lupusalex    Support of OpenPegasus local authentication
20   * 1565892    2006-11-28  lupusalex    Make SBLIM client JSR48 compliant
21   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
22   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
23   * 2531371    2009-02-10  raman_arora  Upgrade client to JDK 1.5 (Phase 2)
24   * 2763216    2009-04-14  blaschke-oss Code cleanup: visible spelling/grammar errors
25   */
26  
27  package org.metricshub.wbem.sblim.cimclient.internal.http;
28  
29  /*-
30   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
31   * WBEM Java Client
32   * ჻჻჻჻჻჻
33   * Copyright 2023 - 2025 MetricsHub
34   * ჻჻჻჻჻჻
35   * Licensed under the Apache License, Version 2.0 (the "License");
36   * you may not use this file except in compliance with the License.
37   * You may obtain a copy of the License at
38   *
39   *      http://www.apache.org/licenses/LICENSE-2.0
40   *
41   * Unless required by applicable law or agreed to in writing, software
42   * distributed under the License is distributed on an "AS IS" BASIS,
43   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
44   * See the License for the specific language governing permissions and
45   * limitations under the License.
46   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
47   */
48  
49  import java.net.PasswordAuthentication;
50  import java.net.URI;
51  import java.security.NoSuchAlgorithmException;
52  import java.util.logging.Level;
53  import org.metricshub.wbem.sblim.cimclient.internal.logging.LogAndTraceBroker;
54  import org.metricshub.wbem.sblim.cimclient.internal.logging.Messages;
55  import org.metricshub.wbem.sblim.cimclient.internal.util.WBEMConfiguration;
56  
57  /**
58   * Abstract superclass for HTTP authorization information.
59   *
60   * @see WwwAuthInfo
61   * @see PegasusLocalAuthInfo
62   */
63  public abstract class AuthorizationInfo {
64  	protected String iAddr;
65  
66  	protected int iPort;
67  
68  	protected String iProtocol;
69  
70  	protected String iRealm;
71  
72  	protected String iScheme;
73  
74  	protected PasswordAuthentication iCredentials;
75  
76  	protected long iNc = 1;
77  
78  	protected String iCnonce;
79  
80  	protected String iOpaque;
81  
82  	protected String iAlgorithm;
83  
84  	protected String iUri;
85  
86  	protected String iNonce;
87  
88  	protected String iQop;
89  
90  	protected String iA1;
91  
92  	protected String iResponse;
93  
94  	// protected byte[] iDigest;
95  
96  	/**
97  	 * Initialize
98  	 *
99  	 * @param pProxy
100 	 *            Proxy authentication ?
101 	 * @param pAddress
102 	 *            Server address
103 	 * @param pPort
104 	 *            Server port
105 	 * @param pProtocol
106 	 *            Protocol
107 	 * @param pRealm
108 	 *            Realm
109 	 * @param pScheme
110 	 *            Scheme
111 	 */
112 	public void init(Boolean pProxy, String pAddress, int pPort, String pProtocol, String pRealm, String pScheme) {
113 		this.iAddr = pAddress;
114 		this.iPort = pPort;
115 		this.iProtocol = pProtocol;
116 		this.iRealm = pRealm;
117 		this.iScheme = pScheme;
118 	}
119 
120 	/**
121 	 * Sets opaque
122 	 *
123 	 * @param opaque
124 	 *            new Value
125 	 */
126 	public void setOpaque(String opaque) {
127 		this.iOpaque = opaque;
128 	}
129 
130 	/**
131 	 * Returns opaque
132 	 *
133 	 * @return Opaque
134 	 */
135 	public String getOpaque() {
136 		return this.iOpaque;
137 	}
138 
139 	/**
140 	 * Returns Qop
141 	 *
142 	 * @return Qop
143 	 */
144 	public String getQop() {
145 		return this.iQop;
146 	}
147 
148 	/**
149 	 * Sets Qop
150 	 *
151 	 * @param qop
152 	 *            New value
153 	 */
154 	public void setQop(String qop) {
155 		this.iQop = qop;
156 	}
157 
158 	/**
159 	 * Returns nc
160 	 *
161 	 * @return nc
162 	 */
163 	public long getNc() {
164 		return this.iNc;
165 	}
166 
167 	/**
168 	 * Sets nc
169 	 *
170 	 * @param nc
171 	 *            New value
172 	 */
173 	public void setNc(long nc) {
174 		this.iNc = nc;
175 	}
176 
177 	/**
178 	 * Sets nonce
179 	 *
180 	 * @param nonce
181 	 *            New Value
182 	 */
183 	public void setNonce(String nonce) {
184 		this.iNonce = nonce;
185 	}
186 
187 	/**
188 	 * Returns nonce
189 	 *
190 	 * @return nonce
191 	 */
192 	public String getNonce() {
193 		return this.iNonce;
194 	}
195 
196 	/**
197 	 * Set cnonce
198 	 *
199 	 * @param cnonce
200 	 *            New value
201 	 */
202 	public void setCnonce(String cnonce) {
203 		this.iCnonce = cnonce;
204 	}
205 
206 	/**
207 	 * Returns cnonce
208 	 *
209 	 * @return cnonce
210 	 */
211 	public String getCnonce() {
212 		return this.iCnonce;
213 	}
214 
215 	/**
216 	 * Set algorithm
217 	 *
218 	 * @param algorithm
219 	 *            New value
220 	 */
221 	public void setAlgorithm(String algorithm) {
222 		this.iAlgorithm = algorithm;
223 	}
224 
225 	/**
226 	 * Returns algorithm
227 	 *
228 	 * @return algorithm
229 	 */
230 	public String getAlgorithm() {
231 		return this.iAlgorithm;
232 	}
233 
234 	/**
235 	 * Returns A1
236 	 *
237 	 * @return A1
238 	 */
239 	public String getA1() {
240 		return this.iA1;
241 	}
242 
243 	/**
244 	 * Sets A1
245 	 *
246 	 * @param A1
247 	 *            New value
248 	 */
249 	public void setA1(String A1) {
250 		this.iA1 = A1;
251 	}
252 
253 	/**
254 	 * Sets response
255 	 *
256 	 * @param response
257 	 *            New value
258 	 */
259 	public void setResponse(String response) {
260 		this.iResponse = response;
261 	}
262 
263 	/**
264 	 * Returns response
265 	 *
266 	 * @return New value
267 	 */
268 	public String getResponse() {
269 		return this.iResponse;
270 	}
271 
272 	/**
273 	 * Returns URI
274 	 *
275 	 * @return URI
276 	 */
277 	public String getURI() {
278 		return this.iUri;
279 	}
280 
281 	/**
282 	 * Sets URI
283 	 *
284 	 * @param uri
285 	 *            New value
286 	 */
287 	public void setURI(String uri) {
288 		this.iUri = uri;
289 	}
290 
291 	/**
292 	 * Sets credentials
293 	 *
294 	 * @param credentials
295 	 *            New value
296 	 */
297 	public void setCredentials(PasswordAuthentication credentials) {
298 		this.iCredentials = credentials;
299 	}
300 
301 	/**
302 	 * Returns the address
303 	 *
304 	 * @return The server address
305 	 */
306 	public String getAddr() {
307 		return this.iAddr;
308 	}
309 
310 	/**
311 	 * Returns the port
312 	 *
313 	 * @return The server port
314 	 */
315 	public int getPort() {
316 		return this.iPort;
317 	}
318 
319 	/**
320 	 * Returns the protocol
321 	 *
322 	 * @return The protocol
323 	 */
324 	public String getProtocol() {
325 		return this.iProtocol;
326 	}
327 
328 	/**
329 	 * Returns the realm
330 	 *
331 	 * @return The realm
332 	 */
333 	public String getRealm() {
334 		return this.iRealm;
335 	}
336 
337 	/**
338 	 * Sets the realm
339 	 *
340 	 * @param realm
341 	 *            New value
342 	 */
343 	public void setRealm(String realm) {
344 		this.iRealm = realm;
345 	}
346 
347 	/**
348 	 * Returns the scheme
349 	 *
350 	 * @return The scheme
351 	 */
352 	public String getScheme() {
353 		return this.iScheme;
354 	}
355 
356 	/**
357 	 * Sets the scheme
358 	 *
359 	 * @param scheme
360 	 *            New value
361 	 */
362 	public void setScheme(String scheme) {
363 		this.iScheme = scheme;
364 	}
365 
366 	/**
367 	 * Returns the credentials
368 	 *
369 	 * @return The credentials
370 	 */
371 	public PasswordAuthentication getCredentials() {
372 		return this.iCredentials;
373 	}
374 
375 	/**
376 	 * Compares two authorization informations.
377 	 *
378 	 * @param obj
379 	 *            The other authorization information
380 	 * @return <code>true</code> if type, realm, scheme, address, protocol and
381 	 *         port of both authorization informations are equal,
382 	 *         <code>false</code> otherwise.
383 	 */
384 	public boolean match(Object obj) {
385 		if (obj == null || !(obj instanceof AuthorizationInfo)) return false;
386 		AuthorizationInfo that = (AuthorizationInfo) obj;
387 
388 		boolean type = getClass().equals(that.getClass());
389 		// boolean prxt = (iProxy == null || that.iProxy == null)?
390 		// true:iProxy.equals(that.iProxy);
391 		boolean prmpt = (this.iRealm == null || that.iRealm == null) ? true : this.iRealm.equals(that.iRealm);
392 		boolean schm = (this.iScheme == null || that.iScheme == null) ? true : this.iScheme.equals(that.iScheme);
393 		boolean adr = (this.iAddr == null || that.iAddr == null) ? true : this.iAddr.equals(that.iAddr);
394 		boolean prot = (this.iProtocol == null || that.iProtocol == null) ? true : this.iProtocol.equals(that.iProtocol);
395 		boolean prt = (this.iPort <= 0 || that.iPort <= 0) ? true : (this.iPort == that.iPort);
396 		return (type && prmpt && schm && adr && prot && prt);
397 	}
398 
399 	/**
400 	 * Updates the authorization information according to a received challenge.
401 	 *
402 	 * @param challenge
403 	 *            The received challenge
404 	 * @param authenticate
405 	 *            The authenticate header field
406 	 * @param url
407 	 *            The url of the CIM server
408 	 * @param requestMethod
409 	 *            The HTTP request method (POST or MPOST)
410 	 * @throws NoSuchAlgorithmException
411 	 */
412 	public abstract void updateAuthenticationInfo(
413 		Challenge challenge,
414 		String authenticate,
415 		URI url,
416 		String requestMethod
417 	)
418 		throws NoSuchAlgorithmException;
419 
420 	/*
421 	 * (non-Javadoc)
422 	 *
423 	 * @see java.lang.Object#toString()
424 	 */
425 	@Override
426 	public abstract String toString();
427 
428 	/**
429 	 * Gets the HTTP header field name for this authentication information
430 	 *
431 	 * @return The field name
432 	 */
433 	public abstract String getHeaderFieldName();
434 
435 	/**
436 	 * Determines if the authorization information is already sent on the very
437 	 * first http request or after the "401 Unauthorized" response
438 	 *
439 	 * @return <code>true</code> or <code>false</code>
440 	 */
441 	public abstract boolean isSentOnFirstRequest();
442 
443 	/**
444 	 * Determines if the connection is kept alive after the "401 Unauthorized"
445 	 * response
446 	 *
447 	 * @return <code>true</code> or <code>false</code>
448 	 */
449 	public abstract boolean isKeptAlive();
450 
451 	/**
452 	 * Factory method for AuthorizationInfo objects. Returns an instance of a
453 	 * subclass according to the requested type.
454 	 *
455 	 * @param pModule
456 	 *            The authorization info type to be constructed
457 	 * @param pProxy
458 	 *            Proxy authentication ?
459 	 * @param pAddress
460 	 *            Server address
461 	 * @param pPort
462 	 *            Server port
463 	 * @param pProtocol
464 	 *            Protocol (http/https)
465 	 * @param pRealm
466 	 *            Realm
467 	 * @param pScheme
468 	 *            Scheme (e.g. Basic, Digest)
469 	 * @return An instance of a AuthorizationInfo subclass or <code>null</code>
470 	 * @see WBEMConfiguration#getHttpAuthenticationModule()
471 	 * @see WwwAuthInfo
472 	 * @see PegasusLocalAuthInfo
473 	 */
474 	public static AuthorizationInfo createAuthorizationInfo(
475 		String pModule,
476 		Boolean pProxy,
477 		String pAddress,
478 		int pPort,
479 		String pProtocol,
480 		String pRealm,
481 		String pScheme
482 	) {
483 		AuthorizationInfo info = createAuthorizationInfo(pModule);
484 
485 		if (info != null) {
486 			info.init(pProxy, pAddress, pPort, pProtocol, pRealm, pScheme);
487 		}
488 
489 		return info;
490 	}
491 
492 	/**
493 	 * Factory method for AuthorizationInfo objects. Returns an instance of a
494 	 * subclass according to the requested type.
495 	 *
496 	 * @param pModule
497 	 *            The authorization info type to be constructed
498 	 * @return An instance of a AuthorizationInfo subclass or <code>null</code>
499 	 */
500 	public static AuthorizationInfo createAuthorizationInfo(String pModule) {
501 		if (WwwAuthInfo.class.getName().equals(pModule)) {
502 			return new WwwAuthInfo();
503 		}
504 		if (PegasusLocalAuthInfo.class.getName().equals(pModule)) {
505 			return new PegasusLocalAuthInfo();
506 		}
507 
508 		try {
509 			Class<?> module = Class.forName(pModule);
510 			AuthorizationInfo info = (AuthorizationInfo) module.newInstance();
511 			return info;
512 		} catch (Exception e) {
513 			LogAndTraceBroker.getBroker().trace(Level.FINER, "Exception while loading authentication module", e);
514 			LogAndTraceBroker.getBroker().message(Messages.HTTP_AUTH_MODULE_INVALID, pModule);
515 		}
516 		return null;
517 	}
518 }