Moved scope inside OAuthConfig
This commit is contained in:
1 parent
41b6da1a44
commit
109c93b4e4
11 files changed
+32
-76
No files matched your search
@@ -44,3 +44,6 @@
|
||||
* FEATURE: Added Foursquare OAuth 2.0 Api (thanks Tom Tasche)
|
||||
* FEATURE: Added support for OAuth1.0a signatures via querystring parameters
|
||||
* FEATURE: Added ConstantContact Api (thanks Matthew Laudato)
|
||||
|
||||
[1.2]
|
||||
* REFACTOR: Moved scope inside OAuthConfig (breaks backwards compatibility)
|
||||
@@ -147,6 +147,6 @@ public class ServiceBuilder
|
||||
Preconditions.checkNotNull(api, "You must specify a valid api through the provider() method");
|
||||
Preconditions.checkEmptyString(apiKey, "You must provide an api key");
|
||||
Preconditions.checkEmptyString(apiSecret, "You must provide an api secret");
|
||||
return api.createService(new OAuthConfig(apiKey, apiSecret, callback, signatureType), scope);
|
||||
return api.createService(new OAuthConfig(apiKey, apiSecret, callback, signatureType, scope));
|
||||
}
|
||||
}
|
||||
@@ -21,5 +21,5 @@ public interface Api
|
||||
*
|
||||
* @return fully configured {@link OAuthService}
|
||||
*/
|
||||
OAuthService createService(OAuthConfig config, String scope);
|
||||
OAuthService createService(OAuthConfig config);
|
||||
}
|
||||
@@ -134,14 +134,7 @@ public abstract class DefaultApi10a implements Api
|
||||
* @param callback OAuth callback (either URL or 'oob')
|
||||
* @param scope OAuth scope (optional)
|
||||
*/
|
||||
public OAuthService createService(OAuthConfig config, String scope)
|
||||
{
|
||||
OAuthService service = doCreateService(config);
|
||||
service.addScope(scope);
|
||||
return service;
|
||||
}
|
||||
|
||||
private OAuthService doCreateService(OAuthConfig config)
|
||||
public OAuthService createService(OAuthConfig config)
|
||||
{
|
||||
return new OAuth10aServiceImpl(this, config);
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ public abstract class DefaultApi20 implements Api
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public OAuthService createService(OAuthConfig config, String scope)
|
||||
public OAuthService createService(OAuthConfig config)
|
||||
{
|
||||
return new OAuth20ServiceImpl(this, config);
|
||||
}
|
||||
|
||||
@@ -7,28 +7,24 @@ package org.scribe.model;
|
||||
*/
|
||||
public class OAuthConfig
|
||||
{
|
||||
private String apiKey;
|
||||
private String apiSecret;
|
||||
private String callback;
|
||||
private SignatureType signatureType;
|
||||
private final String apiKey;
|
||||
private final String apiSecret;
|
||||
private final String callback;
|
||||
private final SignatureType signatureType;
|
||||
private final String scope;
|
||||
|
||||
public OAuthConfig(String key, String secret)
|
||||
{
|
||||
this(key,secret,null);
|
||||
this(key, secret, null, null, null);
|
||||
}
|
||||
|
||||
//Kept for backwards compatibility
|
||||
public OAuthConfig(String key, String secret, String callback)
|
||||
{
|
||||
this(key,secret,callback,null);
|
||||
}
|
||||
|
||||
public OAuthConfig(String key, String secret, String callback, SignatureType type)
|
||||
public OAuthConfig(String key, String secret, String callback, SignatureType type, String scope)
|
||||
{
|
||||
this.apiKey = key;
|
||||
this.apiSecret = secret;
|
||||
this.callback = callback;
|
||||
this.signatureType = type;
|
||||
this.callback = callback != null ? callback : OAuthConstants.OUT_OF_BAND;
|
||||
this.signatureType = (type != null) ? type : SignatureType.Header;
|
||||
this.scope = scope;
|
||||
}
|
||||
|
||||
public String getApiKey()
|
||||
@@ -36,38 +32,28 @@ public class OAuthConfig
|
||||
return apiKey;
|
||||
}
|
||||
|
||||
public void setApiKey(String apiKey)
|
||||
{
|
||||
this.apiKey = apiKey;
|
||||
}
|
||||
|
||||
public String getApiSecret()
|
||||
{
|
||||
return apiSecret;
|
||||
}
|
||||
|
||||
public void setApiSecret(String apiSecret)
|
||||
{
|
||||
this.apiSecret = apiSecret;
|
||||
}
|
||||
|
||||
public String getCallback()
|
||||
{
|
||||
return callback != null ? callback : OAuthConstants.OUT_OF_BAND;
|
||||
}
|
||||
|
||||
public void setCallback(String callback)
|
||||
{
|
||||
this.callback = callback;
|
||||
return callback;
|
||||
}
|
||||
|
||||
public SignatureType getSignatureType()
|
||||
{
|
||||
return signatureType != null ? signatureType : SignatureType.Header;
|
||||
return signatureType;
|
||||
}
|
||||
|
||||
public void setSignatureType(SignatureType type)
|
||||
public String getScope()
|
||||
{
|
||||
this.signatureType = type;
|
||||
return scope;
|
||||
}
|
||||
|
||||
public boolean hasScope()
|
||||
{
|
||||
return scope != null;
|
||||
}
|
||||
}
|
||||
@@ -12,12 +12,10 @@ import org.scribe.model.*;
|
||||
*/
|
||||
public class OAuth10aServiceImpl implements OAuthService
|
||||
{
|
||||
private static final String NO_SCOPE = null;
|
||||
private static final String VERSION = "1.0";
|
||||
|
||||
private OAuthConfig config;
|
||||
private DefaultApi10a api;
|
||||
private String scope;
|
||||
|
||||
/**
|
||||
* Default constructor
|
||||
@@ -29,7 +27,6 @@ public class OAuth10aServiceImpl implements OAuthService
|
||||
{
|
||||
this.api = api;
|
||||
this.config = config;
|
||||
this.scope = NO_SCOPE;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,7 +49,7 @@ public class OAuth10aServiceImpl implements OAuthService
|
||||
request.addOAuthParameter(OAuthConstants.CONSUMER_KEY, config.getApiKey());
|
||||
request.addOAuthParameter(OAuthConstants.SIGN_METHOD, api.getSignatureService().getSignatureMethod());
|
||||
request.addOAuthParameter(OAuthConstants.VERSION, getVersion());
|
||||
if(scope != NO_SCOPE) request.addOAuthParameter(OAuthConstants.SCOPE, scope);
|
||||
if(config.hasScope()) request.addOAuthParameter(OAuthConstants.SCOPE, config.getScope());
|
||||
request.addOAuthParameter(OAuthConstants.SIGNATURE, getSignature(request, token));
|
||||
}
|
||||
|
||||
@@ -88,14 +85,6 @@ public class OAuth10aServiceImpl implements OAuthService
|
||||
return VERSION;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public void addScope(String scope)
|
||||
{
|
||||
this.scope = scope;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
||||
@@ -22,14 +22,6 @@ public class OAuth20ServiceImpl implements OAuthService
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public void addScope(String scope)
|
||||
{
|
||||
throw new UnsupportedOperationException("OAuth 2 does not use scopes");
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
|
||||
@@ -41,13 +41,6 @@ public interface OAuthService
|
||||
* @return oauth version as string
|
||||
*/
|
||||
public String getVersion();
|
||||
|
||||
/**
|
||||
* Adds the 'scope' parameter. This is **not** a default OAuth parameter and the setting is optional.
|
||||
*
|
||||
* @param scope OAuth Api scope (optional)
|
||||
*/
|
||||
public void addScope(String scope);
|
||||
|
||||
/**
|
||||
* Returns the URL where you should redirect your users to authenticate
|
||||
|
||||
@@ -57,19 +57,17 @@ public class ServiceBuilderTest
|
||||
builder.provider(ApiMock.class).apiKey("key").apiSecret("secret").scope("rss-api").build();
|
||||
assertEquals(ApiMock.config.getApiKey(), "key");
|
||||
assertEquals(ApiMock.config.getApiSecret(), "secret");
|
||||
assertEquals(ApiMock.scope, "rss-api");
|
||||
assertEquals(ApiMock.config.getScope(), "rss-api");
|
||||
}
|
||||
|
||||
public static class ApiMock implements Api
|
||||
{
|
||||
public static OAuthConfig config;
|
||||
public static String scope;
|
||||
|
||||
@Override
|
||||
public OAuthService createService(OAuthConfig config, String scope)
|
||||
public OAuthService createService(OAuthConfig config)
|
||||
{
|
||||
ApiMock.config = config;
|
||||
ApiMock.scope = scope;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,12 +12,14 @@ public class OAuthConfigTest
|
||||
{
|
||||
OAuthConfig config = new OAuthConfig("key", "secret");
|
||||
assertEquals(OAuthConstants.OUT_OF_BAND, config.getCallback());
|
||||
assertEquals(SignatureType.Header, config.getSignatureType());
|
||||
assertFalse(config.hasScope());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldOverrideDefaultsIfSet()
|
||||
{
|
||||
OAuthConfig config = new OAuthConfig("key", "secret", "http://callback");
|
||||
OAuthConfig config = new OAuthConfig("key", "secret", "http://callback", SignatureType.Header, "scope");
|
||||
assertEquals("http://callback", config.getCallback());
|
||||
assertEquals("key", config.getApiKey());
|
||||
assertEquals("secret", config.getApiSecret());
|
||||
|
||||
Reference in new issue
Block a user