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

Added ability to sign requests with OAuth #10

Open
wants to merge 1 commit into
base: main
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
.classpath
.project
.settings
target
jcurl-all.jar
14 changes: 12 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.4</version>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
Expand Down Expand Up @@ -168,10 +168,15 @@
<artifactId>jopt-simple</artifactId>
<version>4.9</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
Expand Down Expand Up @@ -300,6 +305,11 @@
<artifactId>jetty-client</artifactId>
<version>9.3.7.v20160115</version>
</dependency>
<dependency>
<groupId>oauth.signpost</groupId>
<artifactId>signpost-core</artifactId>
<version>1.2.1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/JCurl.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ public static void main(String[] args) throws Exception {
final OptionSpec<String> headerSpec;
@NonNull
final OptionSpec<String> engineOptionSpec;
@NonNull
final OptionSpec<String> consumerKeySpec;
@NonNull
final OptionSpec<String> consumerSecretSpec;
@NonNull
final OptionSpec<String> userTokenSpec;
@NonNull
final OptionSpec<String> userSecretSpec;

public JCurl() {
parser = new OptionParser();
Expand All @@ -67,6 +75,22 @@ public JCurl() {
)
.withRequiredArg()
.ofType(String.class);
consumerKeySpec = parser
.acceptsAll(asList("consumerKey", "ck"), "OAuth consumer key")
.withRequiredArg()
.ofType(String.class);
consumerSecretSpec = parser
.acceptsAll(asList("consumerSecret", "cs"), "OAuth consumer secret")
.withRequiredArg()
.ofType(String.class);
userTokenSpec = parser
.acceptsAll(asList("userToken", "ut"), "OAuth user token")
.withRequiredArg()
.ofType(String.class);
userSecretSpec = parser
.acceptsAll(asList("userSecret", "us"), "OAuth user secret")
.withRequiredArg()
.ofType(String.class);
}

public ResponseEntity<String> execute(String... args) throws Exception {
Expand Down Expand Up @@ -109,6 +133,15 @@ public ResponseEntity<String> execute(String... args) throws Exception {
}
}

if (optionSet.has("consumerKey") && optionSet.has("consumerSecret")) {
options.setOAuthConsumerCredentials(new OAuthConsumerCredentials(optionSet.valueOf(consumerKeySpec),
optionSet.valueOf(consumerSecretSpec)));
}
if (optionSet.has("userToken") && optionSet.has("userSecret")) {
options.setOAuthUserCredentials(
new OAuthUserCredentials(optionSet.valueOf(userTokenSpec), optionSet.valueOf(userSecretSpec)));
}

return engineType.getEngine().submit(options);
}

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/JCurlRequestOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class JCurlRequestOptions {
private boolean parallel = false;
private int count = 1;
private final Map<String, String> headerMap = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
private OAuthConsumerCredentials oAuthConsumerCredentials;
private OAuthUserCredentials oAuthUserCredentials;

public JCurlRequestOptions() {
headerMap.put("User-Agent", USER_AGENT);
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/OAuthConsumerCredentials.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import lombok.AllArgsConstructor;
import lombok.Data;

@AllArgsConstructor
@Data
public class OAuthConsumerCredentials {
private String consumerKey;
private String consumerSecret;
}
17 changes: 17 additions & 0 deletions src/main/java/OAuthUserCredentials.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import java.util.HashMap;
import java.util.Map;
import java.util.SortedSet;

import lombok.Data;

@Data
public class OAuthUserCredentials {
private String userToken;
private String userTokenSecret;
private Map<String, SortedSet<String>> userParameters = new HashMap<>();

public OAuthUserCredentials(String userToken, String userSecret) {
this.userToken = userToken;
this.userTokenSecret = userSecret;
}
}
44 changes: 43 additions & 1 deletion src/main/java/UrlEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,29 @@
*/

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.http.HttpParameters;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Random;
import java.util.SortedSet;

/**
* @author Erich Eichinger
* @since 01/03/2016
*/
public class UrlEngine implements Engine {
private Random random = new Random();

@Override
public ResponseEntity<String> submit(JCurlRequestOptions requestOptions) throws Exception {
System.setProperty("http.keepAlive", "true");
Expand All @@ -37,6 +47,29 @@ public ResponseEntity<String> submit(JCurlRequestOptions requestOptions) throws

for (int i=0;i< requestOptions.getCount();i++) {
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
OAuthConsumerCredentials oAuthConsumerCredentials = requestOptions.getOAuthConsumerCredentials();
if (oAuthConsumerCredentials != null) {
OAuthConsumer consumer = new DefaultOAuthConsumer(oAuthConsumerCredentials.getConsumerKey(),
oAuthConsumerCredentials.getConsumerSecret());
OAuthUserCredentials oAuthUserCredentials = requestOptions.getOAuthUserCredentials();
if (oAuthUserCredentials != null) {
consumer.setTokenWithSecret(oAuthUserCredentials.getUserToken(),
oAuthUserCredentials.getUserTokenSecret());
HttpParameters parameters = new HttpParameters();
for (Map.Entry<String, SortedSet<String>> parameterEntry : oAuthUserCredentials.getUserParameters()
.entrySet()) {
parameters.put(parameterEntry.getKey(), parameterEntry.getValue());
}
if (!parameters.containsKey(OAuth.OAUTH_NONCE)) {
String nonce = generateNonce();
if (StringUtils.isNotBlank(nonce)) {
parameters.put(OAuth.OAUTH_NONCE, nonce, true);
}
}
consumer.setAdditionalParameters(parameters);
}
consumer.sign(con);
}

// add request header
con.setRequestMethod("GET");
Expand All @@ -46,10 +79,11 @@ public ResponseEntity<String> submit(JCurlRequestOptions requestOptions) throws
}

System.out.println("\nSending 'GET' request to URL : " + requestOptions.getUrl());
long startTime = System.currentTimeMillis();
con.connect();

int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);
System.out.println("Response Code : " + responseCode + " Total Time: " + (System.currentTimeMillis() - startTime));

final InputStream is = con.getInputStream();
String response = IOUtils.toString(is);
Expand All @@ -63,4 +97,12 @@ public ResponseEntity<String> submit(JCurlRequestOptions requestOptions) throws
return responseEntity;
}

private String generateNonce() {
String nonce = null;
if (this.random != null) {
nonce = Long.toString(this.random.nextLong());
}
return nonce;
}

}