View Javadoc

1   /*
2    * Copyright 2003-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   * You are receiving this code free of charge, which represents many hours of
17   * effort from other individuals and corporations.  As a responsible member 
18   * of the community, you are asked (but not required) to donate any 
19   * enhancements or improvements back to the community under a similar open 
20   * source license.  Thank you. -TMN
21   */
22  package groovyx.net.http;
23  
24  import java.io.IOException;
25  import java.io.InputStream;
26  import java.net.MalformedURLException;
27  import java.net.URL;
28  import java.security.GeneralSecurityException;
29  import java.security.KeyStore;
30  
31  import org.apache.http.auth.AuthScope;
32  import org.apache.http.auth.UsernamePasswordCredentials;
33  import org.apache.http.conn.scheme.Scheme;
34  import org.apache.http.conn.ssl.SSLSocketFactory;
35  
36  /**
37   * Encapsulates all configuration related to HTTP authentication methods.
38   * @see HTTPBuilder#getAuth()
39   * 
40   * @author <a href='mailto:tnichols@enernoc.com'>Tom Nichols</a>
41   */
42  public class AuthConfig {
43  	protected HTTPBuilder builder;
44  	public AuthConfig( HTTPBuilder builder ) {
45  		this.builder = builder;
46  	}
47  	
48  	/**
49  	 * Set authentication credentials to be used for the current 
50  	 * {@link HTTPBuilder#getURL() default host}.  This method name is a bit of 
51  	 * a misnomer, since these credentials will actually work for "digest" 
52  	 * authentication as well.
53  	 * @param user
54  	 * @param pass
55  	 */
56  	public void basic( String user, String pass ) {
57  		URL url = (URL)builder.getURL();
58  		if ( url == null ) throw new IllegalStateException( "a default URL must be set" );
59  		this.basic( url.getHost(), url.getPort(), user, pass );
60  	}
61  	
62  	/**
63  	 * Set authentication credentials to be used for the given host and port. 
64  	 * @param host
65  	 * @param port
66  	 * @param user
67  	 * @param pass
68  	 */
69  	public void basic( String host, int port, String user, String pass ) {
70  		builder.getClient().getCredentialsProvider().setCredentials( 
71  			new AuthScope( host, port ),
72  			new UsernamePasswordCredentials( user, pass )
73  		);
74  	}
75  	
76  	/**
77  	 * Sets a certificate to be used for SSL authentication.  
78  	 * @param certURL URL to a JKS keystore where the certificate is stored
79  	 * @param password password to decrypt the keystore
80  	 */
81  	public void certificate( String certURL, String password ) 
82  			throws GeneralSecurityException, MalformedURLException, IOException {
83  		
84  		KeyStore keyStore = KeyStore.getInstance( KeyStore.getDefaultType() );
85          InputStream jksStream = new URL(certURL).openStream();
86          try {
87          	keyStore.load( jksStream, password.toCharArray() );
88          } finally { jksStream.close(); }
89  
90          SSLSocketFactory ssl = new SSLSocketFactory(keyStore, password);
91          ssl.setHostnameVerifier( SSLSocketFactory.STRICT_HOSTNAME_VERIFIER );
92          
93          builder.getClient().getConnectionManager().getSchemeRegistry()
94          	.register( new Scheme("https", ssl, 443) );
95  	}
96  }