Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
AuthConfig |
|
| 1.5;1.5 |
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 | 15 | public AuthConfig( HTTPBuilder builder ) { |
45 | 15 | this.builder = builder; |
46 | 15 | } |
47 | ||
48 | /** | |
49 | * Set authentication credentials to be used for the current | |
50 | * {@link HTTPBuilder#getURL() default host}. | |
51 | * @param user | |
52 | * @param pass | |
53 | */ | |
54 | public void basic( String user, String pass ) { | |
55 | 3 | URL url = (URL)builder.getURL(); |
56 | 3 | if ( url == null ) throw new IllegalStateException( "a default URL must be set" ); |
57 | 3 | this.basic( url.getHost(), url.getPort(), user, pass ); |
58 | 3 | } |
59 | ||
60 | /** | |
61 | * Set authentication credentials to be used for the given host and port. | |
62 | * @param host | |
63 | * @param port | |
64 | * @param user | |
65 | * @param pass | |
66 | */ | |
67 | public void basic( String host, int port, String user, String pass ) { | |
68 | 3 | builder.getClient().getCredentialsProvider().setCredentials( |
69 | new AuthScope( host, port ), | |
70 | new UsernamePasswordCredentials( user, pass ) | |
71 | ); | |
72 | 3 | } |
73 | ||
74 | /** | |
75 | * Sets a certificate to be used for SSL authentication. | |
76 | * @param certURL URL to a JKS keystore where the certificate is stored | |
77 | * @param password password to decrypt the keystore | |
78 | */ | |
79 | public void certificate( String certURL, String password ) | |
80 | throws GeneralSecurityException, MalformedURLException, IOException { | |
81 | ||
82 | 0 | KeyStore keyStore = KeyStore.getInstance( KeyStore.getDefaultType() ); |
83 | 0 | InputStream jksStream = new URL(certURL).openStream(); |
84 | try { | |
85 | 0 | keyStore.load( jksStream, password.toCharArray() ); |
86 | 0 | } finally { jksStream.close(); } |
87 | ||
88 | 0 | SSLSocketFactory ssl = new SSLSocketFactory(keyStore, password); |
89 | 0 | ssl.setHostnameVerifier( SSLSocketFactory.STRICT_HOSTNAME_VERIFIER ); |
90 | ||
91 | 0 | builder.getClient().getConnectionManager().getSchemeRegistry() |
92 | .register( new Scheme("https", ssl, 443) ); | |
93 | 0 | } |
94 | } |