Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #37 - Non-default Keystore Types #38

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
target/
*.iml
.idea/
etc/config/twitter.properties

14 changes: 14 additions & 0 deletions etc/config/twitter-template.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
## Copy this file to etc/config/twitter.properties and provide
## user-specific values so tests can successfully access twitter
## APIs.

# Access Token Username (as shown on API settings page; case-sensitive for tests)
twitter.user=
# Application API Key
twitter.oauth.consumerKey=
# Application API secret
twitter.oauth.consumerSecret=
# Access Token
twitter.oauth.accessToken=
# Acccess Token Secret
twitter.oauth.secretToken=
19 changes: 19 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,25 @@
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.1</version>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<execution>
<phase>initialize</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>etc/config/twitter.properties</file>
</files>
<quiet>true</quiet>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<extensions>
<extension>
Expand Down
35 changes: 33 additions & 2 deletions src/main/java/groovyx/net/http/AuthConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,50 @@ public void ntlm( String host, int port, String user, String pass, String workst
public void certificate( String certURL, String password )
throws GeneralSecurityException, IOException {

KeyStore keyStore = KeyStore.getInstance( KeyStore.getDefaultType() );
certificate( certURL, password, "jks" );
}

/**
* Sets a certificate to be used for SSL authentication. See
* {@link Class#getResource(String)} for how to get a URL from a resource
* on the classpath.
* @param certURL URL to a keystore where the certificate is stored.
* @param password password to decrypt the keystore
* @param keyStoreType the type of keystore (e.g. JKS, PKCS12) containing the certificate
*/
public void certificate( String certURL, String password, String keyStoreType )
throws GeneralSecurityException, IOException {

if (keyStoreType == null) {
keyStoreType = KeyStore.getDefaultType();
}
KeyStore keyStore = KeyStore.getInstance( keyStoreType );
InputStream jksStream = new URL(certURL).openStream();
try {
keyStore.load( jksStream, password.toCharArray() );
} finally { jksStream.close(); }

certificate( keyStore, password );
}

/**
* Provide a Keystore containing the certificate to be used for
* SSL authentication. This method moves the responsibility of
* loading the certificates to the application, allowing access
* to custom Keystore types such as SmartCard access or other
* proprietary forms.
* @param keyStore the keystore containing the authentication certificate
* @param password password to decrypt the keystore
*/
public void certificate( KeyStore keyStore, String password )
throws GeneralSecurityException, IOException {
SSLSocketFactory ssl = new SSLSocketFactory(keyStore, password);
ssl.setHostnameVerifier( SSLSocketFactory.STRICT_HOSTNAME_VERIFIER );

builder.getClient().getConnectionManager().getSchemeRegistry()
.register( new Scheme("https", ssl, 443) );
}

/**
* </p>OAuth sign all requests. Note that this currently does <strong>not</strong>
* wait for a <code>WWW-Authenticate</code> challenge before sending the
Expand Down