01: /*
02: * JBoss, Home of Professional Open Source.
03: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
04: * as indicated by the @author tags. See the copyright.txt file in the
05: * distribution for a full listing of individual contributors.
06: *
07: * This is free software; you can redistribute it and/or modify it
08: * under the terms of the GNU Lesser General Public License as
09: * published by the Free Software Foundation; either version 2.1 of
10: * the License, or (at your option) any later version.
11: *
12: * This software is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15: * Lesser General Public License for more details.
16: *
17: * You should have received a copy of the GNU Lesser General Public
18: * License along with this software; if not, write to the Free
19: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21: */
22: package org.jboss.web.tomcat.security;
23:
24: import java.io.IOException;
25: import javax.security.auth.callback.CallbackHandler;
26: import javax.security.auth.callback.Callback;
27: import javax.security.auth.callback.UnsupportedCallbackException;
28:
29: import org.jboss.security.auth.callback.MapCallback;
30: import org.jboss.security.auth.spi.RFC2617Digest;
31:
32: /**
33: A CallbackHandler that is used to pass the RFC2617 parameters to the login
34: module DigestCallback.
35:
36: @author Scott.Stark@jboss.org
37: @version $Revision: 57206 $
38: */
39: public class DigestCallbackHandler implements CallbackHandler {
40: private String username;
41: private String nonce;
42: private String nc;
43: private String cnonce;
44: private String qop;
45: private String realm;
46: private String md5a2;
47:
48: DigestCallbackHandler(String username, String nonce, String nc,
49: String cnonce, String qop, String realm, String md5a2) {
50: this .username = username;
51: this .nonce = nonce;
52: this .nc = nc;
53: this .cnonce = cnonce;
54: this .qop = qop;
55: this .realm = realm;
56: this .md5a2 = md5a2;
57: }
58:
59: public void handle(Callback[] callbacks) throws IOException,
60: UnsupportedCallbackException {
61: boolean foundCallback = false;
62: Callback firstUnknown = null;
63: int count = callbacks != null ? callbacks.length : 0;
64: for (int n = 0; n < count; n++) {
65: Callback c = callbacks[n];
66: if (c instanceof MapCallback) {
67: MapCallback mc = (MapCallback) c;
68: mc.setInfo(RFC2617Digest.USERNAME, username);
69: mc.setInfo(RFC2617Digest.CNONCE, cnonce);
70: mc.setInfo(RFC2617Digest.NONCE, nonce);
71: mc.setInfo(RFC2617Digest.NONCE_COUNT, nc);
72: mc.setInfo(RFC2617Digest.QOP, qop);
73: mc.setInfo(RFC2617Digest.REALM, realm);
74: mc.setInfo(RFC2617Digest.A2HASH, md5a2);
75: foundCallback = true;
76: } else if (firstUnknown == null) {
77: firstUnknown = c;
78: }
79: }
80: if (foundCallback == false)
81: throw new UnsupportedCallbackException(firstUnknown,
82: "Unrecognized Callback");
83: }
84: }
|