Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/intabulas/sandler/authentication/AtomAuthentication.java


1   /**
2    * Copyright (c) 2003, Mark Lussier
3    * All rights reserved.
4    *
5    * Portions Copyright (c) 2003 by David A. Czarnecki
6    *
7    * Redistribution and use in source and binary forms, with or without
8    * modification, are permitted provided that the following conditions are met:
9    *
10   * Redistributions of source code must retain the above copyright notice,
11   *      this list of conditions and the following disclaimer.
12   * Redistributions in binary form must reproduce the above copyright notice,
13   *      this list of conditions and the following disclaimer in the documentation
14   *      and/or other materials provided with the distribution.
15   * Neither the name of the "Mark Lussier" and "Sandler" nor the names of
16   * its contributors may be used to endorse or promote products derived from
17   * this software without specific prior written permission.
18   * Products derived from this software may not be called "Sandler",
19   * nor may "Sandler" appear in their name, without prior written permission of
20   * Mark Lussier
21   *
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
23   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
24   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25   * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
26   * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
27   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31   * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35   */
36  package org.intabulas.sandler.authentication;
37  
38  import org.intabulas.sandler.AtomConstants;
39  
40  /**
41   * AtomAuthentication
42   *
43   * @author Mark Lussier
44   * @version $Id: AtomAuthentication.java,v 1.1 2003/09/11 17:21:08 intabulas Exp $
45   */
46  public class AtomAuthentication implements AtomConstants {
47  
48      private String _username;
49      private String _relm;
50      private String _nonce;
51      private String _uri;
52      private String _qop;
53      private String _nc;
54      private String _cnonce;
55      private String _response;
56  
57  
58      /**
59       *
60       * @return
61       */
62      public String getUsername() {
63          return _username;
64      }
65  
66      /**
67       *
68       * @param _username
69       */
70      public void setUsername(String _username) {
71          this._username = _username;
72      }
73  
74      /**
75       *
76       * @param authHeader
77       */
78      public AtomAuthentication(String authHeader) {
79          parseAuthHeader(authHeader);
80      }
81  
82      /**
83       *
84       * @param header
85       */
86      private void parseAuthHeader(String header) {
87          String[] tokens = header.split(",");
88          for (int x = 0; x < tokens.length; x++) {
89              int index = tokens[x].indexOf('=');
90              if (index != -1) {
91                  String key = tokens[x].substring(0, index).trim();
92                  String value = tokens[x].substring(index + 1).trim();
93                  value = value.replaceAll("\"", "");
94                  if (key.startsWith(ATOM_REALM_KEY)) {
95                      key = key.substring(ATOM_REALM_KEY_LENGTH).trim();
96                  }
97                  if (key.equalsIgnoreCase(ATOM_AUTHTOKEN_USERNAME)) {
98                      _username = value;
99                  } else if (key.equalsIgnoreCase(ATOM_AUTHTOKEN_REALM)) {
100                     _relm = value;
101                 } else if (key.equalsIgnoreCase(ATOM_AUTHTOKEN_NONCE)) {
102                     _nonce = value;
103                 } else if (key.equalsIgnoreCase(ATOM_AUTHTOKEN_URI)) {
104                     _uri = value;
105                 } else if (key.equalsIgnoreCase(ATOM_AUTHTOKEN_QOP)) {
106                     _qop = value;
107                 } else if (key.equalsIgnoreCase(ATOM_AUTHTOKEN_NC)) {
108                     _nc = value;
109                 } else if (key.equalsIgnoreCase(ATOM_AUTHTOKEN_CNONCE)) {
110                     _cnonce = value;
111                 } else if (key.equalsIgnoreCase(ATOM_AUTHTOKEN_RESPONSE)) {
112                     _response = value;
113 
114                 }
115             }
116         }
117 
118     }
119 
120     /**
121      *
122      * @param password
123      * @param verb
124      * @return
125      */
126     public boolean authenticate(String password, String verb) {
127         String a1 = DigestUtilities.digestString(_username + ATOM_DIGEST_SEPERATOR + _relm + ATOM_DIGEST_SEPERATOR + password);
128         String a2 = DigestUtilities.digestString(verb + ATOM_DIGEST_SEPERATOR + _uri);
129         String check = DigestUtilities.digestString(a1 + ATOM_DIGEST_SEPERATOR + _nonce + ATOM_DIGEST_SEPERATOR + _nc
130                 + ATOM_DIGEST_SEPERATOR + _cnonce + ATOM_DIGEST_SEPERATOR + _qop + ATOM_DIGEST_SEPERATOR + a2);
131         return check.equals(_response);
132     }
133 
134 
135 }