1 18 19 package sync4j.framework.security; 20 21 import java.security.Principal ; 22 23 import sync4j.framework.core.Cred; 24 import sync4j.framework.tools.Base64; 25 26 35 public class Sync4jPrincipal implements Principal , java.io.Serializable { 36 38 40 private String id = null; private String username = null; 42 private String deviceId = null; 43 44 private String encodedUserPwd = null; 45 46 50 public Sync4jPrincipal() {}; 51 52 59 public Sync4jPrincipal(final String id , 60 final String username, 61 final String deviceId) { 62 this.id = id ; 63 this.username = username; 64 this.deviceId = deviceId; 65 } 66 67 74 public Sync4jPrincipal(final String username, final String deviceId) { 75 this(null, username, deviceId); 76 } 77 78 79 84 public Sync4jPrincipal(final String id) { 85 this(id, null, null); 86 } 87 88 97 public static Sync4jPrincipal fromCredential( String userpwd , 98 String type , 99 String deviceId) 100 throws IllegalArgumentException { 101 102 Sync4jPrincipal ret = new Sync4jPrincipal(); 103 104 if (Cred.AUTH_SUPPORTED_TYPES.indexOf(type) < 0) { 105 throw new IllegalArgumentException ( "Authorization type '" 106 + type 107 + "' not supported" 108 ); 109 } 110 111 if (Cred.AUTH_TYPE_BASIC.equals(type)) { 112 String s = new String (Base64.decode(userpwd.getBytes())); 113 114 int p = s.indexOf(':'); 115 116 if (p == -1) { 117 ret.setUsername(s); 118 } else { 119 ret.setUsername((p>0) ? s.substring(0, p) : ""); 120 } 121 ret.setEncodedUserPwd(userpwd); 122 } else { 123 ret.setUsername(userpwd); 124 } 125 126 ret.setId(null); 127 ret.setDeviceId(deviceId); 128 129 return ret; 130 } 131 132 134 140 public String getName() { 141 142 return (deviceId == null) 143 ? username 144 : (deviceId + '/' + username); 145 } 146 147 152 public String getUsername() { 153 return username; 154 } 155 156 161 public void setUsername(String username) { 162 this.username = username; 163 } 164 165 170 public String getDeviceId() { 171 return deviceId; 172 } 173 174 179 public void setDeviceId(String deviceId) { 180 this.deviceId = deviceId; 181 } 182 183 188 public String getId() { 189 return id; 190 } 191 192 197 public void setId(String id) { 198 this.id = id; 199 } 200 201 213 public boolean equals(Object o) { 214 if ((o == null) || !(o instanceof Sync4jPrincipal)) { 215 return false; 216 } 217 218 return (getName().equals(((Sync4jPrincipal)o).getName())); 219 } 220 221 public int hashCode() { 222 return getName().hashCode(); 223 } 224 225 public String toString() { 226 return getName(); 227 } 228 229 232 public String getEncodedCredentials() { 233 return encodedUserPwd; 234 } 235 236 241 public void setEncodedUserPwd(String newEncodedUserPwd) { 242 this.encodedUserPwd = newEncodedUserPwd; 243 } 244 } 245 | Popular Tags |