Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7af4162a1845041f4bbcdd7022aa8a5a3eed7fe5 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2011 SAP AG
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Lazar Kirchev, SAP AG - initial API and implementation
 *******************************************************************************/

package org.eclipse.equinox.console.jaas;

import java.security.Principal;
import java.util.HashSet;
import java.util.Set;

/**
 * This class represents a user with password and roles
 *
 */
public class UserPrincipal implements Principal {
	private String username;
	private char[] password;
	private Set<RolePrincipal> rolePrincipals = new HashSet<RolePrincipal>();
	
	public UserPrincipal(String username, String password) {
		this.username = username;
		this.password = new char[password.length()];
		System.arraycopy(password.toCharArray(), 0, this.password, 0, this.password.length);
	}
	
	public String getName() {
		return username;
	}
	
	public boolean authenticate(char[] password) {
		if (password == null) {
			return false;
		}
		
		if (this.password == null) {
			return false;
		}
		
		if (this.password.length != password.length) {
			return false;
		}
		
		for(int i = 0; i < this.password.length; i++) {
			if(this.password[i] != password[i]) {
				return false;
			}
		}
		
		return true;
	}
	
	public Set<RolePrincipal> getRoles() {
		return rolePrincipals;
	}
	
	public synchronized void addRole(RolePrincipal rolePrincipal) {
		rolePrincipals.add(rolePrincipal);
	}
	
	public boolean equals(Object userPrincipal) {
		if (userPrincipal == null) {
			return false;
		}
		
		if (this == userPrincipal) {
			return true;
		}
		
		if (! (userPrincipal instanceof UserPrincipal)) {
			return false;
		}
		
		UserPrincipal otherUser = (UserPrincipal) userPrincipal;
		if (username != null) {
			if (!username.equals(otherUser.username)) {
				return false;
			}
		} else {
			if (otherUser.username != null) {
				return false;
			}
		}
		
		if (password != null) {
			if (otherUser.password == null) {
				return false;
			}
			
			if (password.length != otherUser.password.length) {
				return false;
			}
			
			for(int i = 0; i < password.length; i++) {
				if (password[i] != otherUser.password[i]) {
					return false;
				}
			}
		} else {
			if (otherUser.username != null) {
				return false;
			}
		}
		
		if (rolePrincipals != null) {
			if (!(rolePrincipals.equals(otherUser.rolePrincipals))) {
				return false;
			}
		} else {
			if (otherUser.rolePrincipals != null) {
				return false;
			}
		}
		
		return true;
	}
	
	public void destroy() {
		for(int i = 0; i < password.length; i++) {
			password[i] = ' ';
		}
		
		password = null;
	}

	@Override
	public int hashCode() {
		int result = 1;
		result = 73 * result + (username == null ? 0 : username.hashCode());
		result = 73 * result + (password == null ? 0 : new String(password).hashCode());
		result = 73 * result + (rolePrincipals == null ? 0 : rolePrincipals.hashCode());
		return result;
	}
}

Back to the top