View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
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  
17  package org.gwe.utils.security;
18  
19  import java.io.FileNotFoundException;
20  import java.io.IOException;
21  import java.io.Serializable;
22  import java.util.Arrays;
23  import java.util.Properties;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.gwe.utils.IOUtils;
28  
29  /**
30   * @author Marco Ruiz
31   * @since Jan 26, 2007
32   */
33  public class AccountInfo implements Serializable {
34  	
35  	private static Log log = LogFactory.getLog(AccountInfo.class);
36  	
37  	private static final String MISSING_VALUE = "?";
38  	private static byte[] NULL_BYTE_ARRAY = new byte[]{};
39  	
40  	public static final AccountInfo NO_AUTH_ACCOUNT = new AccountInfo("", "");
41  	
42  	public static AccountInfo createLocalAccount() {
43  		return new AccountInfo(System.getProperty("user.name"), null);
44  	}
45  	
46  	// Account identification info
47  	private String user;
48  	private Properties props = new Properties();
49  	
50  	// Account authentication info - password based
51  	private String password = null;
52  	
53  	// Account authentication info - private key based
54  	private String passphrase = null;
55  	
56  	private String privateKeyFileName;
57  	private String publicKeyFileName;
58  	
59  	private byte[] privateKey = null;
60  	private byte[] publicKey = null;
61  	
62  	private boolean keyFilesError = false;
63  	
64  	// Account authentication info - gss based
65  //	private GSSCredential credential;
66  	
67  	public AccountInfo(String user, String passphrase, String privateKeyFilename, String publicKeyFilename) throws FileNotFoundException, IOException {
68  		this(user, passphrase, IOUtils.readFile(privateKeyFilename), IOUtils.readFile(publicKeyFilename));
69  		if (privateKeyFilename != null &&  !"".equals(privateKeyFilename) && publicKey == null)
70  			this.publicKey = IOUtils.readFile(privateKeyFilename + ".pub");
71  	}
72  
73  	/**
74  	 * Private key based authenticated account
75  	 * 
76  	 * @param accountName
77  	 * @param user
78  	 * @param passKey
79  	 * @param privateKey
80  	 */
81  	public AccountInfo(String user, String passKey, byte[] privateKey, byte[] publicKey) {
82  		this(user);
83  		if (privateKey != null && privateKey != NULL_BYTE_ARRAY) { 
84  			this.passphrase = passKey;
85  			this.privateKey = privateKey;
86  			this.publicKey = publicKey;
87  		} else {
88  			this.password = passKey;
89  		}
90  	}
91  	
92  	/**
93  	 * Password based authenticated account
94  	 * 
95  	 */
96  	public AccountInfo(String user, String password) {
97  		this(user, password, NULL_BYTE_ARRAY, NULL_BYTE_ARRAY);
98  //		this.password = (password == null) ? "" : password;
99  	}
100 
101 	private AccountInfo(String userName) {
102 		this.user = userName;
103 	}
104 	
105 	public void init() {
106 		if (privateKeyFileName != null &&  !"".equals(privateKeyFileName) && (publicKey == null || "".equals(publicKey)))
107 			this.publicKeyFileName = privateKeyFileName + ".pub";
108 		
109 		if (privateKey == null || privateKey == NULL_BYTE_ARRAY) { 
110             this.privateKey = readKeyFileWithNoExceptions(privateKeyFileName);
111 			this.publicKey = readKeyFileWithNoExceptions(publicKeyFileName);
112 		}
113     }
114 	
115 	public boolean isKeyFilesError() {
116     	return keyFilesError;
117     }
118 
119 	private byte[] readKeyFileWithNoExceptions(String fileName) {
120 		try {
121 	        return IOUtils.readFile(fileName);
122         } catch (Exception e) {
123         	keyFilesError = true;
124         	log.warn("Key file " + fileName + "could not be loaded.", e);
125         	this.passphrase = "";
126         	return NULL_BYTE_ARRAY;
127         }
128 	}
129 
130 	public String getUser() {
131 		return user;
132 	}
133 	
134 	public String getProperty(String propName) {
135 		return props.getProperty(propName);
136 	}
137 	
138 	public void setProperties(Properties properties) {
139 		this.props = properties;
140 	}
141 	
142 	public String getPassphrase() {
143 		return passphrase;
144 	}
145 	
146 	public String getPrivateKeyFileName() {
147     	return privateKeyFileName;
148     }
149 
150 	public byte[] getPrivateKey() {
151 		return (privateKey == null) ? privateKey : privateKey.clone();
152 	}
153 	
154 	public byte[] getPublicKey() {
155 		return (publicKey == null) ? publicKey : publicKey.clone();
156 	}
157 	
158 	public String getPassword() {
159 		return password;
160 	}
161 /*
162 	public GSSCredential getCredential() {
163 		return credential;
164 	}
165 	
166 	public void setCredential(GSSCredential credential) {
167 		this.credential = credential;
168 	}
169 */
170 /*
171 	public void setCredential(String proxyStr) throws GSSException {
172 		GlobusGSSManagerImpl mgr = new GlobusGSSManagerImpl();
173 		credential = mgr.createCredential(proxyStr.getBytes(), ExtendedGSSCredential.IMPEXP_OPAQUE,
174 		        GSSCredential.DEFAULT_LIFETIME, null, GSSCredential.ACCEPT_ONLY);
175 	}
176 */
177 
178 	public AccountInfo clone() {
179 		return (this.password != null) ? 
180 			new AccountInfo(user, password) : 
181 			new AccountInfo(user, passphrase, privateKey, publicKey);
182 	}
183 	
184 	public String getPasskey() {
185 		return (this.password != null) ? password : passphrase;
186 	}
187 
188 	public void setPassword(String password) {
189     	this.password = password;
190     }
191 
192 	public void setPassphrase(String passphrase) {
193     	this.passphrase = passphrase;
194     }
195 	
196 	public void setPasskey(String passkey) {
197 		if (missingPassword())   setPassword(passkey);
198 		if (missingPassphrase()) setPassphrase(passkey);
199 	}
200 
201 	public boolean missingPasskey() {
202 		return missingPassword() || missingPassphrase();
203 	}
204 	
205 	public boolean missingPassword() {
206 	    return MISSING_VALUE.equals(password);
207     }
208 
209 	public boolean missingPassphrase() {
210 	    return MISSING_VALUE.equals(passphrase);
211     }
212     	
213 	@Override
214     public int hashCode() {
215 	    final int prime = 31;
216 	    int result = 1;
217 	    result = prime * result + ((passphrase == null) ? 0 : passphrase.hashCode());
218 	    result = prime * result + ((password == null) ? 0 : password.hashCode());
219 	    result = prime * result + Arrays.hashCode(privateKey);
220 	    result = prime * result + Arrays.hashCode(publicKey);
221 	    result = prime * result + ((user == null) ? 0 : user.hashCode());
222 	    return result;
223     }
224 
225 	@Override
226     public boolean equals(Object obj) {
227 	    if (this == obj)
228 		    return true;
229 	    if (obj == null)
230 		    return false;
231 	    if (getClass() != obj.getClass())
232 		    return false;
233 	    AccountInfo other = (AccountInfo) obj;
234 	    if (passphrase == null) {
235 		    if (other.passphrase != null)
236 			    return false;
237 	    } else if (!passphrase.equals(other.passphrase))
238 		    return false;
239 	    if (password == null) {
240 		    if (other.password != null)
241 			    return false;
242 	    } else if (!password.equals(other.password))
243 		    return false;
244 	    if (!Arrays.equals(privateKey, other.privateKey))
245 		    return false;
246 	    if (!Arrays.equals(publicKey, other.publicKey))
247 		    return false;
248 	    if (user == null) {
249 		    if (other.user != null)
250 			    return false;
251 	    } else if (!user.equals(other.user))
252 		    return false;
253 	    return true;
254     }
255 }