-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathaccount.java
More file actions
211 lines (177 loc) · 5.34 KB
/
account.java
File metadata and controls
211 lines (177 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* SmallTalk Account Object
*
* Tyler Barber
* Updated 2013-05-10
*
* The purpose of this file is for the creation and modification
* of SmallTalk accounts. A user can create an account, or use
* their existing account information in the Client to log in.
*
* TODO LIST:
* 1) Add ability to change username and password
* 2) Actually handle errors.
*
* For existing account information see accounts file.
*/
package Server;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
public class Account{
private String email;
private String username;
private String password;
// public String lastLogin;
/*
* Creates an Account object.
* Identifies its Username that was passed in by loginHandler or Client.
* Fetches the rest of account info from accounts file afterwards.
*/
public Account() {
}
/*
* Used to create a new SmallTalk account.
* Parameters will be passed in from client.
* Identifies username, password and user identity.
* Afterwards, the account information is written to the accounts file.
*/
public void createUserAccount(String username, String password, String email) {
this.email = email;
this.username = username;
this.password = password;
saveAccount();
}
/*
* Used to populate the password and identity field for Account objects.
* If the username exists in the accounts file, then this method fetches
* the password and identity.
* If the username does not exist in the accounts file, the password
* and the identity are left as null.
* This allows for the creation of new accounts.
*/
public void fetchAccountInfo(String email) {
this.email = email;
File accounts = new java.io.File("accounts.txt");
try {
Scanner accountFinder = new Scanner(accounts);
while(accountFinder.hasNextLine()) {
if (accountFinder.nextLine().equals("email: " + email)) {
String userPassword = accountFinder.nextLine().toString();
this.username = userPassword.replace("username: ", "");
String userIdentity = accountFinder.nextLine().toString();
this.password = userIdentity.replace("password: ", "");
}
}
accountFinder.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* Used to see if a username is taken.
* As of now this is not used locally. Client will use this later on
* in order to see if a username is free for use.
* Checks if password field is null, if so then the user account is not
* already in the accounts file.
*/
public boolean doesAccountExist(String email) {
fetchAccountInfo(email);
if(this.password != null) return true;
else return false;
}
public boolean isUsernameTaken(String username) {
File accounts = new java.io.File("accounts.txt");
try {
Scanner accountFinder = new Scanner(accounts);
while(accountFinder.hasNextLine()) {
if (accountFinder.nextLine().equals("username: " + username)) {
accountFinder.close();
return true;
}
}
accountFinder.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
/*
* Currently writes the users new account out to a plain text accounts file.
* Writes real name, username, and then password.
* TODO Write to some sort of database.
* Encrypt stored user information.
* Return an actual error.
*/
private void saveAccount() {
try{
FileWriter accountWriter = new FileWriter("accounts.txt", true);
BufferedWriter out = new BufferedWriter(accountWriter);
out.newLine();
out.write("email: " + this.email);
out.newLine();
out.write("username: " + this.username);
out.newLine();
out.write("password: " + this.password);
out.newLine();
out.flush();
out.close();
}catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
}
/*
* Used to output account information to a string.
* Will be used by the Client to display account information if requested.
*/
public String toString() {
return "username: " + username + "\npassword: " + password
+ "\nemail: " + email;
}
/*
* Used to send password information to other classes
* Parameters - none
* Returns - password as a String
*
*/
public String getPassword(){
return this.password;
}
/*
*
* Used to encrypt account information
* Parameters - String that is to be encrypted
* Returns - The encrypted string in hex format
*
*/
public String hashPassword(String pass) throws NoSuchAlgorithmException{
MessageDigest mess = MessageDigest.getInstance("MD5");
mess.update(pass.getBytes());
byte digest[] = mess.digest();
return toHexString(digest);
}
/*
*
* Used to convert a byte array to hex
* Parameters - byte array
* Returns - String in hex format
*
*/
private String toHexString(byte[] bytes) {
final char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
char[] hexChars = new char[bytes.length * 2];
int v;
for ( int j = 0; j < bytes.length; j++ ) {
v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
}