KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > tools > ChangeAdminPassword


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

16 package org.jmanage.core.tools;
17
18 import org.jmanage.core.auth.UserManager;
19 import org.jmanage.core.auth.User;
20 import org.jmanage.core.auth.AuthConstants;
21 import org.jmanage.core.util.PasswordField;
22 import org.jmanage.core.crypto.EncryptedKey;
23 import org.jmanage.core.crypto.KeyManager;
24 import org.jmanage.core.crypto.Crypto;
25
26 import java.util.Arrays JavaDoc;
27
28 /**
29  *
30  * date: Aug 3, 2004
31  * @author Rakesh Kalra
32  */

33 public class ChangeAdminPassword {
34
35     public static void main(String JavaDoc[] args)
36         throws Exception JavaDoc {
37
38         final UserManager userManager = UserManager.getInstance();
39         final char[] oldPassword =
40                 PasswordField.getPassword("Enter Old password:");
41         final User admin = userManager.verifyUsernamePassword(AuthConstants.USER_ADMIN,
42                 oldPassword);
43         if(admin == null){
44             System.out.println("Invalid password.");
45             return;
46         }
47
48         final char[] newPassword = getNewPassword();
49         if(newPassword == null){
50             return;
51         }
52
53         /* update the password for admin */
54         admin.setPassword(Crypto.hash(newPassword));
55         userManager.updateUser(admin);
56
57         /* re-encrypt the key */
58         EncryptedKey encryptedKey = KeyManager.readKey(oldPassword);
59         encryptedKey.setPassword(newPassword);
60         /* write the encryptedKey to the key file */
61         KeyManager.writeKey(encryptedKey);
62
63         System.out.println("Password has been changed");
64     }
65
66     private static char[] getNewPassword()
67         throws Exception JavaDoc {
68
69         final char[] password = PasswordField.getPassword("Enter new password:");
70         final char[] password2 = PasswordField.getPassword("Re-enter new password:");
71         if(!Arrays.equals(password, password2)){
72             System.out.println("Passwords do not match.");
73             return null;
74         }
75         return password;
76     }
77 }
78
Popular Tags