Skip to content

Commit

Permalink
Added TokenPair to return instead of plain strings
Browse files Browse the repository at this point in the history
Also for this added provider extension points for custom responses
  • Loading branch information
fernandezpablo85 committed Mar 27, 2010
1 parent 664e94f commit b0130f2
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 22 deletions.
20 changes: 9 additions & 11 deletions src/main/java/org/scribe/oauth/Scribe.java
Expand Up @@ -79,15 +79,15 @@ private boolean isEmpty(String string){
/**
* Obtains the request token and token secret.
*
* @return the request token and token secret.
* @return the request token
*/
public String getRequestToken(){
public Token getRequestToken(){
Request request = getRTRequest();
OAuthSigner signer = getOAuthSigner();
signer.signForRequestToken(request, config.getProperty(CALLBACK_URL));
provider.tuneRequest(request, CallType.REQUEST_TOKEN);
Response response = request.send();
return response.getBody();
return provider.parseRequestTokens(response.getBody());
}

private Request getRTRequest() {
Expand All @@ -102,17 +102,16 @@ private OAuthSigner getOAuthSigner() {
* Obtains the access token and access token secret.
*
* @param request token
* @param request token secret
* @param verifier
* @return access token and access token secret
* @return access token
*/
public String getAccessToken(String requestToken, String tokenSecret, String verifier){
public Token getAccessToken(Token token, String verifier){
Request request = getATRequest();
OAuthSigner signer = getOAuthSigner();
signer.signForAccessToken(request, requestToken, tokenSecret, verifier);
signer.signForAccessToken(request, token.getToken(), token.getSecret(), verifier);
provider.tuneRequest(request, CallType.ACCESS_TOKEN);
Response response = request.send();
return response.getBody();
return provider.parseAccessTokens(response.getBody());
}

private Request getATRequest() {
Expand All @@ -124,11 +123,10 @@ private Request getATRequest() {
*
* @param Request to sign
* @param access token
* @param access token secret
*/
public void signRequest(Request request, String token, String tokenSecret){
public void signRequest(Request request, Token tokens){
OAuthSigner signer = getOAuthSigner();
signer.sign(request, token, tokenSecret);
signer.sign(request, tokens.getToken(), tokens.getSecret());
provider.tuneRequest(request, CallType.RESOURCE);
}
}
39 changes: 39 additions & 0 deletions src/main/java/org/scribe/oauth/Token.java
@@ -0,0 +1,39 @@
/*
Copyright 2010 Pablo Fernandez
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.scribe.oauth;

public class Token {

private String token;
private String secret;

public Token(String token, String secret){
this.token = token;
this.secret = secret;
}

public String getToken() {
return token;
}

public String getSecret() {
return secret;
}

public String toString(){
return String.format("oauth_token -> %s oauth_token_secret -> %s",token,secret);
}
}
22 changes: 22 additions & 0 deletions src/main/java/org/scribe/providers/DefaultProvider.java
Expand Up @@ -15,9 +15,12 @@
*/
package org.scribe.providers;

import java.util.regex.*;

import org.scribe.http.*;
import org.scribe.oauth.*;


/**
* Default OAuth provider.
*
Expand All @@ -27,6 +30,8 @@
*/
public class DefaultProvider {

private static final String TOKEN_REGEX = "oauth_token=(\\S*)&oauth_token_secret=(\\S*?)(&(.*))?";

/**
* Hook that lets you modify the string to sign before the HMAC-SHA1 signature is calculated.
*
Expand Down Expand Up @@ -58,4 +63,21 @@ public String tuneOAuthHeader(Request request, String oAuthHeader, CallType type
* @param the call type
*/
public void tuneRequest(Request request, CallType type){}

public Token parseRequestTokens(String response){
return parseTokens(response);
}

public Token parseAccessTokens(String response){
return parseTokens(response);
}

private Token parseTokens(String response){
Matcher matcher = Pattern.compile(TOKEN_REGEX).matcher(response);
if(matcher.matches()){
return new Token(matcher.group(1), matcher.group(2));
}else{
throw new RuntimeException("Could not find request token or secret in response: " + response);
}
}
}
Expand Up @@ -13,17 +13,16 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.scribe;
package org.scribe.oauth;

import static org.junit.Assert.*;

import java.io.*;
import java.util.*;

import org.junit.*;
import org.scribe.http.*;
import org.scribe.http.Request.*;
import org.scribe.oauth.*;

import static org.junit.Assert.*;

public class ScribeTest {

Expand All @@ -32,31 +31,33 @@ public class ScribeTest {
@Before
public void setup() throws Exception{
Properties props = new Properties();
FileInputStream fis = new FileInputStream("src/main/resources/org/scribe/providers/linkedin.properties");
FileInputStream fis = new FileInputStream("src/main/resources/org/scribe/providers/twitter.properties");
props.load(fis);
scribe = Scribe.getInstance(props);
}

@Test
public void shouldRetrieveRequestToken(){
String requestToken = scribe.getRequestToken();
System.out.println(requestToken);
assertTrue(requestToken.length() > 0);
Token token = scribe.getRequestToken();
System.out.println(token);
assertTrue(token.getSecret().length() > 0);
}

@Test @Ignore
public void shouldRetrieveAccessToken(){
//MAKE SURE YOU VALIDATE THE TOKEN IN THE PREVIOUS TEST, AND COPY THE VERIFIER
String accessToken = scribe.getAccessToken("RequestToken", "RequestTokenSecret", "Verifier");
Token tokens = new Token("requestToken", "requestTokenSecret");
Token accessToken = scribe.getAccessToken(tokens, "Verifier");
System.out.println(accessToken);
assertTrue(accessToken.length() > 0);
assertTrue(accessToken.toString().length() > 0);
}

@Test @Ignore
public void shouldAccessProtectedResource(){
//MAKE SURE YOU GET THE ACCESS TOKEN IN THE PREVIOUS TEST
Request request = new Request(Verb.GET, "https://api.linkedin.com/v1/people/~/network?count=50");
scribe.signRequest(request, "AccessToken", "AccessTokenSecret");
Token token = new Token("accessToken","accessTokenSecret");
scribe.signRequest(request, token);
Response response = request.send();
System.out.println(response.getBody());
assertTrue(response.getCode() == 200);
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/org/scribe/providers/DefaultProviderTest.java
@@ -0,0 +1,40 @@
/*
Copyright 2010 Pablo Fernandez
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.scribe.providers;

import org.junit.*;
import org.scribe.oauth.*;

public class DefaultProviderTest {

private DefaultProvider provider = new DefaultProvider();

@Test
public void shouldProcessDefaultRequestTokenResponse(){
String response = "oauth_token=f30d5067-ba09-4706-86b1-5bec36d9ffb7&oauth_token_secret=c5e26ad6-c1a9-4c25-8650-b4b098323321&oauth_callback_confirmed=true&xoauth_request_auth_url=https%3A%2F%2Fapi.linkedin.com%2Fuas%2Foauth%2Fauthorize";
Token pair = provider.parseRequestTokens(response);
Assert.assertEquals("f30d5067-ba09-4706-86b1-5bec36d9ffb7", pair.getToken());
Assert.assertEquals("c5e26ad6-c1a9-4c25-8650-b4b098323321", pair.getSecret());
}

@Test
public void shouldProcessDefaultAccessTokenResponse(){
String response = "oauth_token=a0637ba7-2e07-4be1-8fcc-87eee817350e&oauth_token_secret=eb88ed1e-ce87-4b69-9e62-6f57deca8105";
Token pair = provider.parseAccessTokens(response);
Assert.assertEquals("a0637ba7-2e07-4be1-8fcc-87eee817350e", pair.getToken());
Assert.assertEquals("eb88ed1e-ce87-4b69-9e62-6f57deca8105", pair.getSecret());
}
}

0 comments on commit b0130f2

Please sign in to comment.