KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > userrepository > DefaultUser


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.userrepository;
19
20 import org.apache.james.security.DigestUtil;
21 import org.apache.james.services.User;
22
23 import java.io.Serializable JavaDoc;
24 import java.security.NoSuchAlgorithmException JavaDoc;
25
26 /**
27  * Implementation of User Interface. Instances of this class do not allow
28  * the the user name to be reset.
29  *
30  *
31  * @version CVS $Revision: 1.6.4.3 $
32  */

33
34 public class DefaultUser implements User, Serializable JavaDoc {
35
36     private String JavaDoc userName;
37     private String JavaDoc hashedPassword;
38     private String JavaDoc algorithm ;
39
40     /**
41      * Standard constructor.
42      *
43      * @param name the String name of this user
44      * @param hashAlg the algorithm used to generate the hash of the password
45      */

46     public DefaultUser(String JavaDoc name, String JavaDoc hashAlg) {
47         userName = name;
48         algorithm = hashAlg;
49     }
50
51     /**
52      * Constructor for repositories that are construcing user objects from
53      * separate fields, e.g. databases.
54      *
55      * @param name the String name of this user
56      * @param passwordHash the String hash of this users current password
57      * @param hashAlg the String algorithm used to generate the hash of the
58      * password
59      */

60     public DefaultUser(String JavaDoc name, String JavaDoc passwordHash, String JavaDoc hashAlg) {
61         userName = name;
62         hashedPassword = passwordHash;
63         algorithm = hashAlg;
64     }
65
66     /**
67      * Accessor for immutable name
68      *
69      * @return the String of this users name
70      */

71     public String JavaDoc getUserName() {
72         return userName;
73     }
74
75     /**
76      * Method to verify passwords.
77      *
78      * @param pass the String that is claimed to be the password for this user
79      * @return true if the hash of pass with the current algorithm matches
80      * the stored hash.
81      */

82     public boolean verifyPassword(String JavaDoc pass) {
83         try {
84             String JavaDoc hashGuess = DigestUtil.digestString(pass, algorithm);
85             return hashedPassword.equals(hashGuess);
86         } catch (NoSuchAlgorithmException JavaDoc nsae) {
87         throw new RuntimeException JavaDoc("Security error: " + nsae);
88     }
89     }
90
91     /**
92      * Sets new password from String. No checks made on guessability of
93      * password.
94      *
95      * @param newPass the String that is the new password.
96      * @return true if newPass successfuly hashed
97      */

98     public boolean setPassword(String JavaDoc newPass) {
99         try {
100             hashedPassword = DigestUtil.digestString(newPass, algorithm);
101             return true;
102         } catch (NoSuchAlgorithmException JavaDoc nsae) {
103             throw new RuntimeException JavaDoc("Security error: " + nsae);
104         }
105     }
106
107     /**
108      * Method to access hash of password
109      *
110      * @return the String of the hashed Password
111      */

112     protected String JavaDoc getHashedPassword() {
113         return hashedPassword;
114     }
115
116     /**
117      * Method to access the hashing algorithm of the password.
118      *
119      * @return the name of the hashing algorithm used for this user's password
120      */

121     protected String JavaDoc getHashAlgorithm() {
122         return algorithm;
123     }
124
125
126 }
127
Popular Tags