KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > framework > core > HMACAuthentication


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package sync4j.framework.core;
20
21 /**
22  * Contains the information for HMAC autenthication
23  *
24  * @author Stefano Nichele @ Funambol
25  *
26  * @version $Id: HMACAuthentication.java,v 1.1 2005/05/16 17:32:54 nichele Exp $
27  */

28
29 public class HMACAuthentication extends Authentication {
30
31     // --------------------------------------------------------------- Constants
32
private final String JavaDoc DEFAULT_ALGORITHM = "MD5";
33
34
35     // ------------------------------------------------------------ Private data
36
private String JavaDoc userMac;
37     private String JavaDoc algorithm;
38     private String JavaDoc calculatedMac;
39
40
41     // ------------------------------------------------------------ Constructors
42

43     /** For serialization purposes */
44     protected HMACAuthentication() {}
45
46
47
48     public HMACAuthentication(final String JavaDoc data) {
49         super(Constants.AUTH_TYPE_HMAC, Constants.FORMAT_B64, data);
50     }
51
52
53     // ---------------------------------------------------------- Public Methods
54

55     /**
56      * Gets the userMac property
57      *
58      * @return the userMac property
59      */

60     public String JavaDoc getUserMac() {
61         return this.userMac;
62     }
63
64
65     /**
66      * Sets the userMac property
67      * @param userMac the userMac property
68      */

69     public void setUserMac(String JavaDoc userMac) {
70         this.userMac = userMac;
71     }
72
73     /**
74      * Gets the calculatedMac property
75      *
76      * @return the calculatedMac property
77      */

78     public String JavaDoc getCalculatedMac() {
79         return this.calculatedMac;
80     };
81
82
83     /**
84      * Sets the calculatedMac property
85      * @param calculatedMac the calculatedMac property
86      */

87     public void setCalculatedMac(String JavaDoc calculatedMac) {
88         this.calculatedMac = calculatedMac;
89     };
90
91     /**
92      * Gets the algorithm property
93      *
94      * @return the algorithm property
95      */

96     public String JavaDoc getAlgorithm() {
97         return this.algorithm;
98     };
99
100
101     /**
102      * Sets the data property
103      *
104      * @param data the data property
105      *
106      */

107     public void setData(String JavaDoc data) {
108         if (data == null) {
109             throw new IllegalArgumentException JavaDoc("data cannot be null");
110         }
111
112
113         // example
114
// hmacHeader: algorithm=MD5, username="Robert Jordan", mac=Dgpoz8Pbs4XC0ecp6kLw4Q==
115

116
117         // parse hmac header for algorithm
118

119         int indexAlgorithm = data.indexOf("algorithm");
120         int indexEndAlgorithm = -1;
121
122         if (indexAlgorithm == -1) {
123             algorithm = DEFAULT_ALGORITHM;
124         } else {
125             indexEndAlgorithm = data.indexOf(",", indexAlgorithm);
126             algorithm = data.substring(indexAlgorithm + 10, indexEndAlgorithm);
127         }
128
129         // parse hmac header for username
130

131         int indexUsername = data.indexOf("username", indexEndAlgorithm + 1);
132
133         if (indexUsername == -1) {
134             throw new IllegalArgumentException JavaDoc("Username missing in hmac header");
135         }
136
137         int indexEndUsername = data.indexOf("\"", indexUsername + 10);
138
139         if (indexEndUsername == -1) {
140             throw new IllegalArgumentException JavaDoc("Unable to get username from hmac header [" + data +
141                                                "]");
142         }
143
144         // checks if the previous char is '\'
145
while (data.charAt(indexEndUsername - 1) == '\\') {
146             indexEndUsername = data.indexOf("\"", indexEndUsername + 1);
147         }
148
149         if (indexEndUsername == -1) {
150             throw new IllegalArgumentException JavaDoc("Unable to get username from hmac header [" + data +
151                                                "]");
152         }
153
154         setUsername(data.substring(indexUsername + 10, indexEndUsername));
155
156         // parse hmac header for mac
157

158         int indexMac = data.indexOf("mac", indexEndUsername);
159
160         if (indexMac == -1) {
161             throw new IllegalArgumentException JavaDoc("Mac value missing in hmac header");
162         } else {
163             userMac = data.substring(indexMac + 4);
164         }
165
166     }
167
168 }
Popular Tags