KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > tomcat > security > DigestCallbackHandler


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * 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 JavaDoc;
25 import javax.security.auth.callback.CallbackHandler JavaDoc;
26 import javax.security.auth.callback.Callback JavaDoc;
27 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
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: 37459 $
38  */

39 public class DigestCallbackHandler implements CallbackHandler JavaDoc
40 {
41    private String JavaDoc username;
42    private String JavaDoc nonce;
43    private String JavaDoc nc;
44    private String JavaDoc cnonce;
45    private String JavaDoc qop;
46    private String JavaDoc realm;
47    private String JavaDoc md5a2;
48
49    DigestCallbackHandler(String JavaDoc username, String JavaDoc nonce,
50       String JavaDoc nc, String JavaDoc cnonce, String JavaDoc qop, String JavaDoc realm, String JavaDoc md5a2)
51    {
52       this.username = username;
53       this.nonce = nonce;
54       this.nc = nc;
55       this.cnonce = cnonce;
56       this.qop = qop;
57       this.realm = realm;
58       this.md5a2 = md5a2;
59    }
60
61    public void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc
62    {
63       boolean foundCallback = false;
64       Callback JavaDoc firstUnknown = null;
65       int count = callbacks != null ? callbacks.length : 0;
66       for(int n = 0; n < count; n ++)
67       {
68          Callback JavaDoc c = callbacks[n];
69          if( c instanceof MapCallback )
70          {
71             MapCallback mc = (MapCallback) c;
72             mc.setInfo(RFC2617Digest.USERNAME, username);
73             mc.setInfo(RFC2617Digest.CNONCE, cnonce);
74             mc.setInfo(RFC2617Digest.NONCE, nonce);
75             mc.setInfo(RFC2617Digest.NONCE_COUNT, nc);
76             mc.setInfo(RFC2617Digest.QOP, qop);
77             mc.setInfo(RFC2617Digest.REALM, realm);
78             mc.setInfo(RFC2617Digest.A2HASH, md5a2);
79             foundCallback = true;
80          }
81          else if( firstUnknown == null )
82          {
83             firstUnknown = c;
84          }
85       }
86       if( foundCallback == false )
87          throw new UnsupportedCallbackException JavaDoc(firstUnknown, "Unrecognized Callback");
88    }
89 }
90
Popular Tags