1 package org.sonatype.aether.repository;
2
3
4
5
6
7
8
9
10
11 import java.util.Arrays;
12
13
14
15
16
17
18
19 public final class Authentication
20 {
21
22 private final String username;
23
24 private final char[] password;
25
26 private final String privateKeyFile;
27
28 private final char[] passphrase;
29
30 private static char[] toChars( String str )
31 {
32 return ( str != null ) ? str.toCharArray() : null;
33 }
34
35 private static String toStr( char[] chars )
36 {
37 return ( chars != null ) ? new String( chars ) : null;
38 }
39
40 private static char[] clone( char[] chars )
41 {
42 return ( chars != null ) ? chars.clone() : null;
43 }
44
45 private Authentication( String username, String privateKeyFile, char[] password, char[] passphrase )
46 {
47
48 this.username = username;
49 this.password = password;
50 this.privateKeyFile = privateKeyFile;
51 this.passphrase = passphrase;
52 }
53
54
55
56
57
58
59
60
61
62 public Authentication( String username, char[] password, String privateKeyFile, char[] passphrase )
63 {
64 this( username, privateKeyFile, clone( password ), clone( passphrase ) );
65 }
66
67
68
69
70
71
72
73
74
75 public Authentication( String username, String password, String privateKeyFile, String passphrase )
76 {
77 this( username, privateKeyFile, toChars( password ), toChars( passphrase ) );
78 }
79
80
81
82
83
84
85
86 public Authentication( String username, String password )
87 {
88 this( username, (String) null, toChars( password ), null );
89 }
90
91
92
93
94
95
96
97 public Authentication( String username, char[] password )
98 {
99 this( username, password, null, null );
100 }
101
102
103
104
105
106
107 public String getUsername()
108 {
109 return username;
110 }
111
112
113
114
115
116
117
118 public Authentication setUsername( String username )
119 {
120 if ( eq( this.username, username ) )
121 {
122 return this;
123 }
124 return new Authentication( username, privateKeyFile, password, passphrase );
125 }
126
127
128
129
130
131
132 public String getPassword()
133 {
134 return toStr( password );
135 }
136
137
138
139
140
141
142
143 public Authentication setPassword( String password )
144 {
145 return setPasswordInternal( toChars( password ) );
146 }
147
148
149
150
151
152
153
154 public Authentication setPassword( char[] password )
155 {
156 return setPasswordInternal( clone( password ) );
157 }
158
159 private Authentication setPasswordInternal( char[] password )
160 {
161 if ( Arrays.equals( this.password, password ) )
162 {
163 return this;
164 }
165 return new Authentication( username, privateKeyFile, password, passphrase );
166 }
167
168
169
170
171
172
173 public String getPrivateKeyFile()
174 {
175 return privateKeyFile;
176 }
177
178
179
180
181
182
183
184 public Authentication setPrivateKeyFile( String privateKeyFile )
185 {
186 if ( eq( this.privateKeyFile, privateKeyFile ) )
187 {
188 return this;
189 }
190 return new Authentication( username, privateKeyFile, password, passphrase );
191 }
192
193
194
195
196
197
198 public String getPassphrase()
199 {
200 return toStr( passphrase );
201 }
202
203
204
205
206
207
208
209 public Authentication setPassphrase( String passphrase )
210 {
211 return setPassphraseInternal( toChars( passphrase ) );
212 }
213
214
215
216
217
218
219
220 public Authentication setPassphrase( char[] passphrase )
221 {
222 return setPassphraseInternal( clone( passphrase ) );
223 }
224
225 private Authentication setPassphraseInternal( char[] passphrase )
226 {
227 if ( Arrays.equals( this.passphrase, passphrase ) )
228 {
229 return this;
230 }
231 return new Authentication( username, privateKeyFile, password, passphrase );
232 }
233
234 @Override
235 public String toString()
236 {
237 return getUsername();
238 }
239
240 @Override
241 public boolean equals( Object obj )
242 {
243 if ( this == obj )
244 {
245 return true;
246 }
247 if ( obj == null || !getClass().equals( obj.getClass() ) )
248 {
249 return false;
250 }
251
252 Authentication that = (Authentication) obj;
253
254 return eq( username, that.username ) && Arrays.equals( password, that.password )
255 && eq( privateKeyFile, that.privateKeyFile ) && Arrays.equals( passphrase, passphrase );
256 }
257
258 private static <T> boolean eq( T s1, T s2 )
259 {
260 return s1 != null ? s1.equals( s2 ) : s2 == null;
261 }
262
263 @Override
264 public int hashCode()
265 {
266 int hash = 17;
267 hash = hash * 31 + hash( username );
268 hash = hash * 31 + Arrays.hashCode( password );
269 hash = hash * 31 + hash( privateKeyFile );
270 return hash;
271 }
272
273 private static int hash( Object obj )
274 {
275 return obj != null ? obj.hashCode() : 0;
276 }
277
278 }