Skip to content

Commit

Permalink
Fixed UriBuilder handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
dlazerka committed May 17, 2016
1 parent 84071f0 commit 34f761e
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 67 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ Extensible as much as possible, so you can swap any class with your own implemen
<dependency>
<groupId>me.lazerka.gae-jersey-oauth2</groupId>
<artifactId>gae-jersey-oauth2</artifactId>
<version>2.1</version>
<version>2.2</version>
</dependency>
```
* Gradle:
```groovy
compile 'me.lazerka.gae-jersey-oauth2:gae-jersey-oauth2:2.1'
compile 'me.lazerka.gae-jersey-oauth2:gae-jersey-oauth2:2.2'
```
2. Install Guice module:

Expand Down
16 changes: 8 additions & 8 deletions gae-jersey-oauth2.iml
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,30 @@
<orderEntry type="library" name="Maven: javax.annotation:jsr250-api:1.0" level="project" />
<orderEntry type="library" name="Maven: com.google.api-client:google-api-client-appengine:1.21.0" level="project" />
<orderEntry type="library" name="Maven: com.google.oauth-client:google-oauth-client-appengine:1.21.0" level="project" />
<orderEntry type="library" name="Maven: com.google.http-client:google-http-client-appengine:1.21.0" level="project" />
<orderEntry type="library" name="Maven: com.google.oauth-client:google-oauth-client:1.21.0" level="project" />
<orderEntry type="library" name="Maven: com.google.http-client:google-http-client:1.21.0" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:3.0.1" level="project" />
<orderEntry type="library" name="Maven: com.google.oauth-client:google-oauth-client-servlet:1.21.0" level="project" />
<orderEntry type="library" name="Maven: com.google.http-client:google-http-client-jdo:1.21.0" level="project" />
<orderEntry type="library" name="Maven: javax.jdo:jdo2-api:2.3-eb" level="project" />
<orderEntry type="library" name="Maven: javax.servlet:servlet-api:2.5" level="project" />
<orderEntry type="library" name="Maven: com.google.api-client:google-api-client:1.21.0" level="project" />
<orderEntry type="library" name="Maven: com.google.http-client:google-http-client-jackson2:1.21.0" level="project" />
<orderEntry type="library" name="Maven: com.google.api-client:google-api-client-servlet:1.21.0" level="project" />
<orderEntry type="library" name="Maven: javax.jdo:jdo2-api:2.3-eb" level="project" />
<orderEntry type="library" name="Maven: javax.transaction:transaction-api:1.1" level="project" />
<orderEntry type="library" name="Maven: com.google.http-client:google-http-client-appengine:1.21.0" level="project" />
<orderEntry type="library" name="Maven: com.google.http-client:google-http-client:1.21.0" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.0.1" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.0.1" level="project" />
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.1.1" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.3" level="project" />
<orderEntry type="library" name="Maven: com.google.apis:google-api-services-oauth2:v2-rev98-1.21.0" level="project" />
<orderEntry type="library" name="Maven: com.google.inject:guice:4.0" level="project" />
<orderEntry type="library" name="Maven: javax.inject:javax.inject:1" level="project" />
<orderEntry type="library" name="Maven: aopalliance:aopalliance:1.0" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:19.0" level="project" />
<orderEntry type="library" name="Maven: com.google.inject.extensions:guice-multibindings:4.0" level="project" />
<orderEntry type="library" name="Maven: joda-time:joda-time:2.9.1" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.13" level="project" />
<orderEntry type="library" name="Maven: com.google.code.findbugs:jsr305:3.0.1" level="project" />
<orderEntry type="library" name="Maven: com.google.guava:guava:19.0" level="project" />
<orderEntry type="library" name="Maven: javax.inject:javax.inject:1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.slf4j:slf4j-jdk14:1.7.13" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.testng:testng:6.9.10" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: com.beust:jcommander:1.48" level="project" />
Expand All @@ -74,12 +74,12 @@
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-all:1.10.19" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.powermock:powermock-api-mockito:1.6.4" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:1.10.19" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:2.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:2.1" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.powermock:powermock-api-support:1.6.4" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.powermock:powermock-core:1.6.4" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.powermock:powermock-reflect:1.6.4" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.powermock:powermock-module-testng:1.6.4" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.powermock:powermock-core:1.6.4" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.javassist:javassist:3.20.0-GA" level="project" />
<orderEntry type="library" scope="TEST" name="Maven: org.powermock:powermock-module-testng-common:1.6.4" level="project" />
</component>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<groupId>me.lazerka.gae-jersey-oauth2</groupId>
<artifactId>gae-jersey-oauth2</artifactId>
<version>2.1</version>
<version>2.2</version>
<packaging>jar</packaging>

<name>GAE Jersey Oauth2.0</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.api.client.auth.oauth2.TokenErrorResponse;
import com.google.appengine.api.urlfetch.HTTPRequest;
import com.google.appengine.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.URLFetchService;
import com.google.common.base.Stopwatch;
import com.google.common.collect.ImmutableMap;
import me.lazerka.gae.jersey.oauth2.TokenVerifier;
import org.joda.time.DateTime;
import org.slf4j.Logger;
Expand All @@ -33,9 +31,9 @@
import javax.inject.Provider;
import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.security.InvalidKeyException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.google.appengine.api.urlfetch.FetchOptions.Builder.validateCertificate;
Expand All @@ -58,26 +56,7 @@ public class TokenVerifierFacebookInspectToken implements TokenVerifier {

public static final String AUTH_SCHEME = "Facebook/InspectToken";

private static final UriBuilder accessTokenEndpoint =
UriBuilder.fromUri("https://graph.facebook.com/oauth/access_token")
.queryParam("client_id", "{appId}")
.queryParam("client_secret", "{clientSecret}")
.queryParam("grant_type", "client_credentials");
// .queryParam("redirect_uri={redirect-uri}");
// .queryParam("code={code-parameter}");

protected static final UriBuilder accessTokenEndpoint2 =
UriBuilder.fromUri("https://graph.facebook.com//oauth/access_token")
.queryParam("client_id", "{appId}")
.queryParam("client_secret", "{clientSecret}")
.queryParam("grant_type", "fb_exchange_token")
.queryParam("fb_exchange_token", "{short-lived-token}");


private static final UriBuilder debugTokenEndpoint =
UriBuilder.fromUri("https://graph.facebook.com/v2.6/debug_token")
.queryParam("input_token", "{inputToken}")
.queryParam("access_token", "{appId}|{appSecret}");
private static final URI GRAPH_API = URI.create("https://graph.facebook.com/v2.6/");

final URLFetchService urlFetchService;
final ObjectMapper jackson;
Expand Down Expand Up @@ -108,13 +87,28 @@ public boolean canHandle(@Nullable String authProvider) {
public FacebookUserPrincipal verify(String userAccessToken) throws IOException, InvalidKeyException {
logger.trace("Requesting endpoint to validate token");

Map<String, String> params = ImmutableMap.of(
"inputToken", userAccessToken,
"appId", appId,
"appSecret", appSecret
);
URL url = UriBuilder.fromUri(GRAPH_API)
.path("debug_token")
.queryParam("input_token", userAccessToken)
.queryParam("access_token", "{appId}|{appSecret}")
.build(appId, appSecret)
.toURL();

// UriBuilder.fromUri(GRAPH_API)
// .path("oauth/access_token")
// .queryParam("client_id", "{appId}")
// .queryParam("client_secret", "{clientSecret}")
// .queryParam("grant_type", "client_credentials")
// // also can be used
// .queryParam("redirect_uri={redirect-uri}")
// .queryParam("code={code-parameter}");
//
// UriBuilder.fromUri(GRAPH_API).path("oauth/access_token")
// .queryParam("client_id", "{appId}")
// .queryParam("client_secret", "{clientSecret}")
// .queryParam("grant_type", "fb_exchange_token")
// .queryParam("fb_exchange_token", "{short-lived-token}");

URL url = debugTokenEndpoint.buildFromMap(params).toURL();

HTTPRequest httpRequest = new HTTPRequest(url, GET, validateCertificate());

Expand All @@ -140,7 +134,7 @@ public FacebookUserPrincipal verify(String userAccessToken) throws IOException,
msg += ": " + error.findPath("message").textValue();
}
} catch (IOException e) {
logger.warn("Cannot parse response as " + TokenErrorResponse.class.getSimpleName());
logger.warn("Cannot parse response as error");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.google.common.base.Splitter;
import com.google.common.base.Stopwatch;
import com.google.common.base.Throwables;
import com.google.common.collect.ImmutableMap;
import me.lazerka.gae.jersey.oauth2.TokenVerifier;
import org.joda.time.DateTime;
import org.slf4j.Logger;
Expand All @@ -38,12 +37,12 @@
import javax.inject.Provider;
import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static com.google.appengine.api.urlfetch.FetchOptions.Builder.validateCertificate;
Expand All @@ -67,19 +66,7 @@ public class TokenVerifierFacebookSignedRequest implements TokenVerifier {

public static final String AUTH_SCHEME = "Facebook/SignedRequest";

protected static final UriBuilder accessTokenEndpoint =
UriBuilder.fromUri("https://graph.facebook.com/v2.6/oauth/access_token")
.queryParam("client_id", "{appId}")
.queryParam("client_secret", "{appSecret}")
.queryParam("code", "{code}")
.queryParam("grant_type", "client_credentials");
// .queryParam("redirect_uri={redirect-uri}");

// protected static final UriBuilder userEndpoint =
// UriBuilder.fromUri("https://graph.facebook.com/v2.6/user")
// .segment("{userId}")
// .queryParam("access_token", "{appId}");

private static final URI GRAPH_API = URI.create("https://graph.facebook.com/v2.6/");

private final Mac hmac;

Expand Down Expand Up @@ -150,13 +137,18 @@ public FacebookUserPrincipal verify(String signedRequestToken) throws IOExceptio
* Exchange `code` for long-lived access token. This serves as verification for `code` expiration too.
*/
protected String exchangeCodeForAppAccessToken(String code) throws IOException, InvalidKeyException {
Map<String, String> params = ImmutableMap.of(
"appId", appId,
"appSecret", appSecret,
"code", code
);

URL url = accessTokenEndpoint.buildFromMap(params).toURL();
URL url = UriBuilder.fromUri(GRAPH_API).path("/oauth/access_token")
.queryParam("client_id", appId)
.queryParam("client_secret", appSecret)
.queryParam("code", code)
.queryParam("grant_type", "client_credentials")
// .queryParam("redirect_uri={redirect-uri}");
.build()
.toURL();

// UriBuilder.fromUri("https://graph.facebook.com/v2.6/user")
// .segment("{userId}")
// .queryParam("access_token", "{appId}");

HTTPRequest httpRequest = new HTTPRequest(url, GET, validateCertificate());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import javax.inject.Singleton;
import javax.ws.rs.core.UriBuilder;
import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.security.InvalidKeyException;
import java.util.Collections;
Expand All @@ -56,9 +57,7 @@ public class TokenVerifierGoogleRemote implements TokenVerifier {

public static final String AUTH_SCHEME = "GoogleSignIn/Remote";

protected static final UriBuilder endpoint =
UriBuilder.fromUri("https://www.googleapis.com/oauth2/v3/tokeninfo")
.queryParam("id_token", "{token}");
private static final URI TOKEN_INFO = URI.create("https://www.googleapis.com/oauth2/v3/tokeninfo");

final URLFetchService urlFetchService;
final JsonFactory jsonFactory;
Expand All @@ -79,7 +78,10 @@ public boolean canHandle(String authProvider) {
public UserPrincipal verify(String authToken) throws IOException, InvalidKeyException {
logger.trace("Requesting endpoint to validate token");

URL url = endpoint.build(authToken).toURL();
URL url = UriBuilder.fromUri(TOKEN_INFO)
.queryParam("id_token", authToken)
.build()
.toURL();

HTTPRequest httpRequest = new HTTPRequest(url, GET, validateCertificate());

Expand Down

0 comments on commit 34f761e

Please sign in to comment.