001: /*
002: * $Id: PasswordValidationCallback.java,v 1.5 2007/01/08 16:06:10 shyam_rao Exp $
003: */
004:
005: /*
006: * The contents of this file are subject to the terms
007: * of the Common Development and Distribution License
008: * (the License). You may not use this file except in
009: * compliance with the License.
010: *
011: * You can obtain a copy of the license at
012: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
013: * See the License for the specific language governing
014: * permissions and limitations under the License.
015: *
016: * When distributing Covered Code, include this CDDL
017: * Header Notice in each file and include the License file
018: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
019: * If applicable, add the following below the CDDL Header,
020: * with the fields enclosed by brackets [] replaced by
021: * you own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
025: */
026:
027: package com.sun.xml.wss.impl.callback;
028:
029: import java.io.UnsupportedEncodingException;
030: import java.security.MessageDigest;
031:
032: import javax.security.auth.callback.*;
033:
034: import com.sun.xml.wss.impl.misc.Base64;
035: import com.sun.org.apache.xml.internal.security.exceptions.Base64DecodingException;
036: import com.sun.xml.wss.RealmAuthenticationAdapter;
037:
038: /**
039: * This Callback is intended for Username-Password validation.
040: * A validator that implements the PasswordValidator interface
041: * should be set on the callback by the callback handler.
042: *
043: * <p>Note: A validator for WSS Digested Username-Password is provided
044: * as part of this callback.
045: *
046: * @author XWS-Security Team
047: */
048: public class PasswordValidationCallback extends XWSSCallback implements
049: Callback {
050:
051: private Request request;
052: private boolean result = false;
053: private PasswordValidator validator;
054: private RealmAuthenticationAdapter authenticator = null;
055:
056: public PasswordValidationCallback(Request request) {
057: this .request = request;
058: }
059:
060: public boolean getResult() {
061: try {
062: if (validator != null)
063: result = validator.validate(request);
064: } catch (Exception e) {
065: return false;
066: }
067: return result;
068: }
069:
070: public Request getRequest() {
071: return request;
072: }
073:
074: /**
075: * This method must be invoked by the CallbackHandler while handling
076: * this callback.
077: */
078: public void setValidator(PasswordValidator validator) {
079: this .validator = validator;
080: }
081:
082: public PasswordValidator getValidator() {
083: return this .validator;
084: }
085:
086: public void setRealmAuthentcationAdapter(
087: RealmAuthenticationAdapter adapter) {
088: this .authenticator = adapter;
089: }
090:
091: public RealmAuthenticationAdapter getRealmAuthenticationAdapter() {
092: return this .authenticator;
093: }
094:
095: public static interface Request {
096: }
097:
098: /**
099: * Represents a validation request when the password in the username token
100: * is in plain text.
101: */
102: public static class PlainTextPasswordRequest implements Request {
103:
104: private String password;
105: private String userName;
106:
107: /**
108: * Constructor.
109: *
110: * @param userName <code>java.lang.String</code> representation of User name.
111: * @param password <code>java.lang.String</code> representation of password.
112: */
113: public PlainTextPasswordRequest(String userName, String password) {
114: this .password = password;
115: this .userName = userName;
116: }
117:
118: /**
119: * Get the username stored in this Request.
120: *
121: * @return <code>java.lang.String</code> representation of username.
122: */
123: public String getUsername() {
124: return userName;
125: }
126:
127: /**
128: * Get the password stored in the Request.
129: *
130: * @return <code>java.lang.String</code> representation of password.
131: */
132: public String getPassword() {
133: return password;
134: }
135:
136: }
137:
138: /**
139: * Represents a validation request when the password in the username token
140: * is in digested form.
141: */
142: public static class DigestPasswordRequest implements Request {
143:
144: private String password;
145: private String userName;
146: private String digest;
147: private String nonce;
148: private String created;
149:
150: /**
151: * Constructor.
152: *
153: * @param userName <code>java.lang.String</code> representing Username.
154: * @param digest <code>java.lang.String</code> Base64 encoded form of Digested Password.
155: * @param nonce <code>java.lang.String</code> representation of unique Nonce
156: * used for calculating Digested password.
157: * @param created <code>java.security.String</code> representation of created time
158: * used for password digest calculation.
159: *
160: */
161: public DigestPasswordRequest(String userName, String digest,
162: String nonce, String created) {
163:
164: this .userName = userName;
165: this .digest = digest;
166: this .nonce = nonce;
167: this .created = created;
168: }
169:
170: /**
171: * This method must be invoked by the CallbackHandler while handling
172: * Callback initialized with DigestPasswordRequest.
173: */
174: public void setPassword(String password) {
175: this .password = password;
176: }
177:
178: public String getPassword() {
179: return password;
180: }
181:
182: public String getUsername() {
183: return userName;
184: }
185:
186: public String getDigest() {
187: return digest;
188: }
189:
190: public String getNonce() {
191: return nonce;
192: }
193:
194: public String getCreated() {
195: return created;
196: }
197:
198: }
199:
200: /**
201: * Interface for validating password.
202: */
203: public static interface PasswordValidator {
204:
205: /**
206: * @param request PasswordValidationRequest
207: * @return true if password validation succeeds else false
208: */
209: public boolean validate(Request request)
210: throws PasswordValidationException;
211: }
212:
213: /**
214: * Implements WSS digest Password Validation.
215: * The method to compute password digest is described in http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0.pdf
216: */
217: public static class DigestPasswordValidator implements
218: PasswordValidator {
219:
220: public boolean validate(Request request)
221: throws PasswordValidationException {
222:
223: DigestPasswordRequest req = (DigestPasswordRequest) request;
224: String passwd = req.getPassword();
225: String nonce = req.getNonce();
226: String created = req.getCreated();
227: String passwordDigest = req.getDigest();
228: //String username = req.getUsername();
229:
230: if (null == passwd)
231: return false;
232: byte[] decodedNonce = null;
233: if (null != nonce) {
234: try {
235: decodedNonce = Base64.decode(nonce);
236: } catch (Base64DecodingException bde) {
237: throw new PasswordValidationException(bde);
238: }
239: }
240: String utf8String = "";
241: if (created != null) {
242: utf8String += created;
243: }
244: utf8String += passwd;
245: byte[] utf8Bytes;
246: try {
247: utf8Bytes = utf8String.getBytes("utf-8");
248: } catch (UnsupportedEncodingException uee) {
249: throw new PasswordValidationException(uee);
250: }
251:
252: byte[] bytesToHash;
253: if (decodedNonce != null) {
254: bytesToHash = new byte[utf8Bytes.length
255: + decodedNonce.length];
256: for (int i = 0; i < decodedNonce.length; i++)
257: bytesToHash[i] = decodedNonce[i];
258: for (int i = decodedNonce.length; i < utf8Bytes.length
259: + decodedNonce.length; i++)
260: bytesToHash[i] = utf8Bytes[i - decodedNonce.length];
261: } else {
262: bytesToHash = utf8Bytes;
263: }
264: byte[] hash;
265: try {
266: MessageDigest sha = MessageDigest.getInstance("SHA-1");
267: hash = sha.digest(bytesToHash);
268: } catch (Exception e) {
269: throw new PasswordValidationException(
270: "Password Digest could not be created" + e);
271: }
272: return (passwordDigest.equals(Base64.encode(hash)));
273: }
274: }
275:
276: public static class PasswordValidationException extends Exception {
277:
278: public PasswordValidationException(String message) {
279: super (message);
280: }
281:
282: public PasswordValidationException(String message,
283: Throwable cause) {
284: super (message, cause);
285: }
286:
287: public PasswordValidationException(Throwable cause) {
288: super(cause);
289: }
290: }
291: }
|