OAuth 1.0a service refactor to remove many properties
This commit is contained in:
1 parent
d0fa8689cb
commit
0be82d6832
8 files changed
+39
-62
No files matched your search
@@ -18,42 +18,42 @@ import org.scribe.services.*;
|
||||
public abstract class DefaultApi10a implements Api
|
||||
{
|
||||
|
||||
protected AccessTokenExtractor getAccessTokenExtractor()
|
||||
public AccessTokenExtractor getAccessTokenExtractor()
|
||||
{
|
||||
return new TokenExtractorImpl();
|
||||
}
|
||||
|
||||
protected BaseStringExtractor getBaseStringExtractor()
|
||||
public BaseStringExtractor getBaseStringExtractor()
|
||||
{
|
||||
return new BaseStringExtractorImpl();
|
||||
}
|
||||
|
||||
protected HeaderExtractor getHeaderExtractor()
|
||||
public HeaderExtractor getHeaderExtractor()
|
||||
{
|
||||
return new HeaderExtractorImpl();
|
||||
}
|
||||
|
||||
protected RequestTokenExtractor getRequestTokenExtractor()
|
||||
public RequestTokenExtractor getRequestTokenExtractor()
|
||||
{
|
||||
return new TokenExtractorImpl();
|
||||
}
|
||||
|
||||
protected SignatureService getSignatureService()
|
||||
public SignatureService getSignatureService()
|
||||
{
|
||||
return new HMACSha1SignatureService();
|
||||
}
|
||||
|
||||
protected TimestampService getTimestampService()
|
||||
public TimestampService getTimestampService()
|
||||
{
|
||||
return new TimestampServiceImpl();
|
||||
}
|
||||
|
||||
protected Verb getAccessTokenVerb()
|
||||
public Verb getAccessTokenVerb()
|
||||
{
|
||||
return Verb.POST;
|
||||
}
|
||||
|
||||
protected Verb getRequestTokenVerb()
|
||||
public Verb getRequestTokenVerb()
|
||||
{
|
||||
return Verb.POST;
|
||||
}
|
||||
@@ -63,14 +63,14 @@ public abstract class DefaultApi10a implements Api
|
||||
*
|
||||
* @return request token URL
|
||||
*/
|
||||
protected abstract String getRequestTokenEndpoint();
|
||||
public abstract String getRequestTokenEndpoint();
|
||||
|
||||
/**
|
||||
* Returns the URL that receives the access token requests.
|
||||
*
|
||||
* @return access token URL
|
||||
*/
|
||||
protected abstract String getAccessTokenEndpoint();
|
||||
public abstract String getAccessTokenEndpoint();
|
||||
|
||||
/**
|
||||
* Returns the {@link OAuthService} for this Api
|
||||
@@ -89,13 +89,7 @@ public abstract class DefaultApi10a implements Api
|
||||
|
||||
private OAuthService createService(String apiKey, String apiSecret, String callback)
|
||||
{
|
||||
return new OAuth10aServiceImpl( getSignatureService(),
|
||||
getTimestampService(),
|
||||
getBaseStringExtractor(),
|
||||
getHeaderExtractor(),
|
||||
getRequestTokenExtractor(),
|
||||
getAccessTokenExtractor(),
|
||||
createConfig(apiKey, apiSecret, callback));
|
||||
return new OAuth10aServiceImpl(this, createConfig(apiKey, apiSecret, callback));
|
||||
}
|
||||
|
||||
private OAuthConfig createConfig(String apiKey, String apiSecret, String callback)
|
||||
|
||||
@@ -5,7 +5,7 @@ import org.scribe.model.Verb;
|
||||
public class EvernoteApi extends DefaultApi10a
|
||||
{
|
||||
@Override
|
||||
protected Verb getRequestTokenVerb()
|
||||
public Verb getRequestTokenVerb()
|
||||
{
|
||||
return Verb.GET;
|
||||
}
|
||||
@@ -17,7 +17,7 @@ public class EvernoteApi extends DefaultApi10a
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Verb getAccessTokenVerb()
|
||||
public Verb getAccessTokenVerb()
|
||||
{
|
||||
return Verb.GET;
|
||||
}
|
||||
|
||||
@@ -3,13 +3,13 @@ package org.scribe.builder.api;
|
||||
public class FoursquareApi extends DefaultApi10a
|
||||
{
|
||||
@Override
|
||||
protected String getAccessTokenEndpoint()
|
||||
public String getAccessTokenEndpoint()
|
||||
{
|
||||
return "http://foursquare.com/oauth/access_token";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getRequestTokenEndpoint()
|
||||
public String getRequestTokenEndpoint()
|
||||
{
|
||||
return "http://foursquare.com/oauth/request_token";
|
||||
}
|
||||
|
||||
@@ -5,13 +5,13 @@ import org.scribe.model.*;
|
||||
public class GoogleApi extends DefaultApi10a
|
||||
{
|
||||
@Override
|
||||
protected String getAccessTokenEndpoint()
|
||||
public String getAccessTokenEndpoint()
|
||||
{
|
||||
return "https://www.google.com/accounts/OAuthGetAccessToken";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getRequestTokenEndpoint()
|
||||
public String getRequestTokenEndpoint()
|
||||
{
|
||||
return "https://www.google.com/accounts/OAuthGetRequestToken";
|
||||
}
|
||||
|
||||
@@ -3,13 +3,13 @@ package org.scribe.builder.api;
|
||||
public class TwitterApi extends DefaultApi10a
|
||||
{
|
||||
@Override
|
||||
protected String getAccessTokenEndpoint()
|
||||
public String getAccessTokenEndpoint()
|
||||
{
|
||||
return "http://api.twitter.com/oauth/access_token";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getRequestTokenEndpoint()
|
||||
public String getRequestTokenEndpoint()
|
||||
{
|
||||
return "http://api.twitter.com/oauth/request_token";
|
||||
}
|
||||
|
||||
@@ -3,13 +3,13 @@ package org.scribe.builder.api;
|
||||
public class VimeoApi extends DefaultApi10a
|
||||
{
|
||||
@Override
|
||||
protected String getAccessTokenEndpoint()
|
||||
public String getAccessTokenEndpoint()
|
||||
{
|
||||
return "http://vimeo.com/oauth/access_token";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getRequestTokenEndpoint()
|
||||
public String getRequestTokenEndpoint()
|
||||
{
|
||||
return "http://vimeo.com/oauth/request_token";
|
||||
}
|
||||
|
||||
@@ -3,13 +3,13 @@ package org.scribe.builder.api;
|
||||
public class YahooApi extends DefaultApi10a
|
||||
{
|
||||
@Override
|
||||
protected String getAccessTokenEndpoint()
|
||||
public String getAccessTokenEndpoint()
|
||||
{
|
||||
return "https://api.login.yahoo.com/oauth/v2/get_token";
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getRequestTokenEndpoint()
|
||||
public String getRequestTokenEndpoint()
|
||||
{
|
||||
return "https://api.login.yahoo.com/oauth/v2/get_request_token";
|
||||
}
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
package org.scribe.oauth;
|
||||
|
||||
import org.scribe.extractors.*;
|
||||
import org.scribe.builder.api.*;
|
||||
import org.scribe.model.*;
|
||||
import org.scribe.services.*;
|
||||
|
||||
/**
|
||||
* OAuth 1.0a implementation of {@link OAuthService}
|
||||
@@ -15,34 +14,18 @@ public class OAuth10aServiceImpl implements OAuthService
|
||||
private static final String VERSION = "1.0";
|
||||
|
||||
private OAuthConfig config;
|
||||
private SignatureService signatureService;
|
||||
private TimestampService timestampService;
|
||||
private BaseStringExtractor baseStringExtractor;
|
||||
private HeaderExtractor headerExtractor;
|
||||
private RequestTokenExtractor rtExtractor;
|
||||
private AccessTokenExtractor atExtractor;
|
||||
private DefaultApi10a api;
|
||||
private String scope;
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
*
|
||||
* @param signatureService OAuth 1.0a signature service
|
||||
* @param timestampService OAuth 1.0a timestamp service
|
||||
* @param baseStringExtractor OAuth 1.0a base string extractor
|
||||
* @param headerExtractor OAuth 1.0a http header extractor
|
||||
* @param rtExtractor OAuth 1.0a request token extractor
|
||||
* @param atExtractor OAuth 1.0a access token extractor
|
||||
* @param api OAuth1.0a api information
|
||||
* @param config OAuth 1.0a configuration param object
|
||||
*/
|
||||
public OAuth10aServiceImpl(SignatureService signatureService, TimestampService timestampService, BaseStringExtractor baseStringExtractor,
|
||||
HeaderExtractor headerExtractor, RequestTokenExtractor rtExtractor, AccessTokenExtractor atExtractor, OAuthConfig config)
|
||||
public OAuth10aServiceImpl(DefaultApi10a api, OAuthConfig config)
|
||||
{
|
||||
this.signatureService = signatureService;
|
||||
this.timestampService = timestampService;
|
||||
this.baseStringExtractor = baseStringExtractor;
|
||||
this.headerExtractor = headerExtractor;
|
||||
this.rtExtractor = rtExtractor;
|
||||
this.atExtractor = atExtractor;
|
||||
this.api = api;
|
||||
this.config = config;
|
||||
this.scope = NO_SCOPE;
|
||||
}
|
||||
@@ -52,22 +35,22 @@ public class OAuth10aServiceImpl implements OAuthService
|
||||
*/
|
||||
public Token getRequestToken()
|
||||
{
|
||||
OAuthRequest request = new OAuthRequest(config.getRequestTokenVerb(), config.getRequestTokenEndpoint());
|
||||
request.addOAuthParameter(OAuthConstants.CALLBACK, config.getCallback());
|
||||
if(scope != NO_SCOPE) request.addOAuthParameter(OAuthConstants.SCOPE, scope);
|
||||
OAuthRequest request = new OAuthRequest(api.getRequestTokenVerb(), api.getRequestTokenEndpoint());
|
||||
addOAuthParams(request, OAuthConstants.EMPTY_TOKEN);
|
||||
addOAuthHeader(request);
|
||||
Response response = request.send();
|
||||
return rtExtractor.extract(response.getBody());
|
||||
return api.getRequestTokenExtractor().extract(response.getBody());
|
||||
}
|
||||
|
||||
private void addOAuthParams(OAuthRequest request, Token token)
|
||||
{
|
||||
request.addOAuthParameter(OAuthConstants.TIMESTAMP, timestampService.getTimestampInSeconds());
|
||||
request.addOAuthParameter(OAuthConstants.NONCE, timestampService.getNonce());
|
||||
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, signatureService.getSignatureMethod());
|
||||
request.addOAuthParameter(OAuthConstants.SIGN_METHOD, api.getSignatureService().getSignatureMethod());
|
||||
request.addOAuthParameter(OAuthConstants.VERSION, getVersion());
|
||||
request.addOAuthParameter(OAuthConstants.CALLBACK, config.getCallback());
|
||||
if(scope != NO_SCOPE) request.addOAuthParameter(OAuthConstants.SCOPE, scope);
|
||||
request.addOAuthParameter(OAuthConstants.SIGNATURE, getSignature(request, token));
|
||||
}
|
||||
|
||||
@@ -82,7 +65,7 @@ public class OAuth10aServiceImpl implements OAuthService
|
||||
addOAuthParams(request, requestToken);
|
||||
addOAuthHeader(request);
|
||||
Response response = request.send();
|
||||
return atExtractor.extract(response.getBody());
|
||||
return api.getAccessTokenExtractor().extract(response.getBody());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -113,13 +96,13 @@ public class OAuth10aServiceImpl implements OAuthService
|
||||
|
||||
private String getSignature(OAuthRequest request, Token token)
|
||||
{
|
||||
String baseString = baseStringExtractor.extract(request);
|
||||
return signatureService.getSignature(baseString, config.getApiSecret(), token.getSecret());
|
||||
String baseString = api.getBaseStringExtractor().extract(request);
|
||||
return api.getSignatureService().getSignature(baseString, config.getApiSecret(), token.getSecret());
|
||||
}
|
||||
|
||||
private void addOAuthHeader(OAuthRequest request)
|
||||
{
|
||||
String oauthHeader = headerExtractor.extract(request);
|
||||
String oauthHeader = api.getHeaderExtractor().extract(request);
|
||||
request.addHeader(OAuthConstants.HEADER, oauthHeader);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user