Skip to content
Closed
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
141 changes: 141 additions & 0 deletions src/main/java/org/scribe/builder/api/DefaultApi10.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package org.scribe.builder.api;

import org.scribe.extractors.*;
import org.scribe.model.*;
import org.scribe.oauth.*;
import org.scribe.services.*;

/**
* Default implementation of the OAuth protocol, version 1.0
*
* This class is meant to be extended by concrete implementations of the API,
* providing the endpoints and endpoint-http-verbs.
*
* If your Api adheres to the 1.0 protocol correctly, you just need to extend
* this class and define the getters for your endpoints.
*
* If your Api does something a bit different, you can override the different
* extractors or services, in order to fine-tune the process. Please read the
* javadocs of the interfaces to get an idea of what to do.
*
* @author Pablo Fernandez (copy of OAuth 1.0a), patched by Martin Vlcek (OAuth 1.0)
*
*/
public abstract class DefaultApi10 implements Api
{
/**
* Returns the access token extractor.
*
* @return access token extractor
*/
public AccessTokenExtractor getAccessTokenExtractor()
{
return new TokenExtractorImpl();
}

/**
* Returns the base string extractor.
*
* @return base string extractor
*/
public BaseStringExtractor getBaseStringExtractor()
{
return new BaseStringExtractorImpl();
}

/**
* Returns the header extractor.
*
* @return header extractor
*/
public HeaderExtractor getHeaderExtractor()
{
return new HeaderExtractorImpl();
}

/**
* Returns the request token extractor.
*
* @return request token extractor
*/
public RequestTokenExtractor getRequestTokenExtractor()
{
return new TokenExtractorImpl();
}

/**
* Returns the signature service.
*
* @return signature service
*/
public SignatureService getSignatureService()
{
return new HMACSha1SignatureService();
}

/**
* Returns the timestamp service.
*
* @return timestamp service
*/
public TimestampService getTimestampService()
{
return new TimestampServiceImpl();
}

/**
* Returns the verb for the access token endpoint (defaults to POST)
*
* @return access token endpoint verb
*/
public Verb getAccessTokenVerb()
{
return Verb.POST;
}

/**
* Returns the verb for the request token endpoint (defaults to POST)
*
* @return request token endpoint verb
*/
public Verb getRequestTokenVerb()
{
return Verb.POST;
}

/**
* Returns the URL that receives the request token requests.
*
* @return request token URL
*/
public abstract String getRequestTokenEndpoint();

/**
* Returns the URL that receives the access token requests.
*
* @return access token URL
*/
public abstract String getAccessTokenEndpoint();

/**
* Returns the URL where you should redirect your users to authenticate
* your application.
*
* @param requestToken the request token you need to authorize
* @return the URL where you should redirect your users
*/
public abstract String getAuthorizationUrl(Token requestToken, String callback);

/**
* Returns the {@link OAuthService} for this Api
*
* @param apiKey Key
* @param apiSecret Api Secret
* @param callback OAuth callback (either URL or 'oob')
* @param scope OAuth scope (optional)
*/
public OAuthService createService(OAuthConfig config)
{
return new OAuth10ServiceImpl(this, config);
}
}
14 changes: 11 additions & 3 deletions src/main/java/org/scribe/builder/api/DropBoxApi.java
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package org.scribe.builder.api;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.scribe.model.*;

public class DropBoxApi extends DefaultApi10a
public class DropBoxApi extends DefaultApi10
{
@Override
public String getAccessTokenEndpoint()
Expand All @@ -11,9 +14,14 @@ public String getAccessTokenEndpoint()
}

@Override
public String getAuthorizationUrl(Token requestToken)
public String getAuthorizationUrl(Token requestToken, String callback)
{
return "https://www.dropbox.com/1/oauth/authorize?oauth_token="+requestToken.getToken();
String url = "https://www.dropbox.com/1/oauth/authorize?oauth_token="+requestToken.getToken();
try {
return url+"&oauth_callback="+URLEncoder.encode(callback, "UTF-8");
} catch (UnsupportedEncodingException e) {
return url;
}
}

@Override
Expand Down
147 changes: 147 additions & 0 deletions src/main/java/org/scribe/oauth/OAuth10ServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package org.scribe.oauth;

import java.util.*;

import org.scribe.builder.api.*;
import org.scribe.model.*;
import org.scribe.utils.*;

/**
* OAuth 1.0 implementation of {@link OAuthService}
*
* @author Pablo Fernandez (copy of OAuth 1.0a), patched by Martin Vlcek (API 1.0)
*/
public class OAuth10ServiceImpl implements OAuthService
{
private static final String VERSION = "1.0";

private OAuthConfig config;
private DefaultApi10 api;

/**
* Default constructor
*
* @param api OAuth1.0 api information
* @param config OAuth 1.0 configuration param object
*/
public OAuth10ServiceImpl(DefaultApi10 api, OAuthConfig config)
{
this.api = api;
this.config = config;
}

/**
* {@inheritDoc}
*/
public Token getRequestToken()
{
config.log("obtaining request token from " + api.getRequestTokenEndpoint());
OAuthRequest request = new OAuthRequest(api.getRequestTokenVerb(), api.getRequestTokenEndpoint());

addOAuthParams(request, OAuthConstants.EMPTY_TOKEN);
appendSignature(request);

config.log("sending request...");
Response response = request.send();
String body = response.getBody();

config.log("response status code: " + response.getCode());
config.log("response body: " + body);
return api.getRequestTokenExtractor().extract(body);
}

private void addOAuthParams(OAuthRequest request, Token token)
{
request.addOAuthParameter(OAuthConstants.TIMESTAMP, api.getTimestampService().getTimestampInSeconds());
request.addOAuthParameter(OAuthConstants.NONCE, api.getTimestampService().getNonce());
request.addOAuthParameter(OAuthConstants.CONSUMER_KEY, config.getApiKey());
request.addOAuthParameter(OAuthConstants.SIGN_METHOD, api.getSignatureService().getSignatureMethod());
request.addOAuthParameter(OAuthConstants.VERSION, getVersion());
if(config.hasScope()) request.addOAuthParameter(OAuthConstants.SCOPE, config.getScope());
request.addOAuthParameter(OAuthConstants.SIGNATURE, getSignature(request, token));

config.log("appended additional OAuth parameters: " + MapUtils.toString(request.getOauthParameters()));
}

/**
* {@inheritDoc}
*/
public Token getAccessToken(Token requestToken, Verifier verifier)
{
config.log("obtaining access token from " + api.getAccessTokenEndpoint());
OAuthRequest request = new OAuthRequest(api.getAccessTokenVerb(), api.getAccessTokenEndpoint());
request.addOAuthParameter(OAuthConstants.TOKEN, requestToken.getToken());
request.addOAuthParameter(OAuthConstants.VERIFIER, verifier.getValue());

config.log("setting token to: " + requestToken + " and verifier to: " + verifier);
addOAuthParams(request, requestToken);
appendSignature(request);
Response response = request.send();
return api.getAccessTokenExtractor().extract(response.getBody());
}

/**
* {@inheritDoc}
*/
public void signRequest(Token token, OAuthRequest request)
{
config.log("signing request: " + request.getCompleteUrl());

// Do not append the token if empty. This is for two legged OAuth calls.
if (!token.isEmpty())
{
request.addOAuthParameter(OAuthConstants.TOKEN, token.getToken());
}
config.log("setting token to: " + token);
addOAuthParams(request, token);
appendSignature(request);
}

/**
* {@inheritDoc}
*/
public String getVersion()
{
return VERSION;
}

/**
* {@inheritDoc}
*/
public String getAuthorizationUrl(Token requestToken)
{
return api.getAuthorizationUrl(requestToken, config.getCallback());
}

private String getSignature(OAuthRequest request, Token token)
{
config.log("generating signature...");
String baseString = api.getBaseStringExtractor().extract(request);
String signature = api.getSignatureService().getSignature(baseString, config.getApiSecret(), token.getSecret());

config.log("base string is: " + baseString);
config.log("signature is: " + signature);
return signature;
}

private void appendSignature(OAuthRequest request)
{
switch (config.getSignatureType())
{
case Header:
config.log("using Http Header signature");

String oauthHeader = api.getHeaderExtractor().extract(request);
request.addHeader(OAuthConstants.HEADER, oauthHeader);
break;
case QueryString:
config.log("using Querystring signature");

for (Map.Entry<String, String> entry : request.getOauthParameters().entrySet())
{
request.addQuerystringParameter(entry.getKey(), entry.getValue());
}
break;
}
}
}