View Javadoc

1   package org.sonatype.aether.repository;
2   
3   /*******************************************************************************
4    * Copyright (c) 2010-2011 Sonatype, Inc.
5    * All rights reserved. This program and the accompanying materials
6    * are made available under the terms of the Eclipse Public License v1.0
7    * which accompanies this distribution, and is available at
8    *   http://www.eclipse.org/legal/epl-v10.html
9    *******************************************************************************/
10  
11  import java.util.Arrays;
12  
13  /**
14   * The authentication to use for accessing a protected resource. <em>Note:</em> Instances of this class are immutable
15   * and the exposed mutators return new objects rather than changing the current instance.
16   * 
17   * @author Benjamin Bentmann
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          // NOTE: This constructor assumes ownership/immutability of the provided arrays
48          this.username = username;
49          this.password = password;
50          this.privateKeyFile = privateKeyFile;
51          this.passphrase = passphrase;
52      }
53  
54      /**
55       * Creates a new authentication with the specified properties
56       * 
57       * @param username The username, may be {@code null}.
58       * @param password The password, may be {@code null}.
59       * @param privateKeyFile The path to the private key file, may be {@code null}.
60       * @param passphrase The passphrase for the private key file, may be {@code null}.
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       * Creates a new authentication with the specified properties
69       * 
70       * @param username The username, may be {@code null}.
71       * @param password The password, may be {@code null}.
72       * @param privateKeyFile The path to the private key file, may be {@code null}.
73       * @param passphrase The passphrase for the private key file, may be {@code null}.
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       * Creates a basic username+password authentication.
82       * 
83       * @param username The username, may be {@code null}.
84       * @param password The password, may be {@code null}.
85       */
86      public Authentication( String username, String password )
87      {
88          this( username, (String) null, toChars( password ), null );
89      }
90  
91      /**
92       * Creates a basic username+password authentication.
93       * 
94       * @param username The username, may be {@code null}.
95       * @param password The password, may be {@code null}.
96       */
97      public Authentication( String username, char[] password )
98      {
99          this( username, password, null, null );
100     }
101 
102     /**
103      * Gets the username.
104      * 
105      * @return The username or {@code null} if none.
106      */
107     public String getUsername()
108     {
109         return username;
110     }
111 
112     /**
113      * Sets the username to use for authentication.
114      * 
115      * @param username The username, may be {@code null}.
116      * @return The new authentication, never {@code null}.
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      * Gets the password.
129      * 
130      * @return The password or {@code null} if none.
131      */
132     public String getPassword()
133     {
134         return toStr( password );
135     }
136 
137     /**
138      * Sets the password to use for authentication.
139      * 
140      * @param password The password, may be {@code null}.
141      * @return The new authentication, never {@code null}.
142      */
143     public Authentication setPassword( String password )
144     {
145         return setPasswordInternal( toChars( password ) );
146     }
147 
148     /**
149      * Sets the password to use for authentication.
150      * 
151      * @param password The password, may be {@code null}.
152      * @return The new authentication, never {@code null}.
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      * Gets the path to the private key file to use for authentication.
170      * 
171      * @return The path to the private key file or {@code null} if none.
172      */
173     public String getPrivateKeyFile()
174     {
175         return privateKeyFile;
176     }
177 
178     /**
179      * Sets the path to the private key file to use for authentication.
180      * 
181      * @param privateKeyFile The path to the private key file, may be {@code null}.
182      * @return The new authentication, never {@code null}.
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      * Gets the passphrase for the private key.
195      * 
196      * @return The passphrase for the private key or {@code null} if none.
197      */
198     public String getPassphrase()
199     {
200         return toStr( passphrase );
201     }
202 
203     /**
204      * Sets the passphrase for the private key file.
205      * 
206      * @param passphrase The passphrase for the private key file, may be {@code null}.
207      * @return The new authentication, never {@code null}.
208      */
209     public Authentication setPassphrase( String passphrase )
210     {
211         return setPassphraseInternal( toChars( passphrase ) );
212     }
213 
214     /**
215      * Sets the passphrase for the private key file.
216      * 
217      * @param passphrase The passphrase for the private key file, may be {@code null}.
218      * @return The new authentication, never {@code null}.
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 }