use of org.springframework.security.oauth.consumer.OAuthConsumerToken in project spring-security-oauth by spring-projects.
the class HttpSessionOAuthRememberMeServicesTests method testEmptySession.
@Test
public void testEmptySession() {
MockHttpSession mockHttpSession = new MockHttpSession();
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setSession(mockHttpSession);
HttpSessionOAuthRememberMeServices oAuthRememberMeService = new HttpSessionOAuthRememberMeServices();
Map<String, OAuthConsumerToken> tokens = oAuthRememberMeService.loadRememberedTokens(request, response);
Assert.assertNull(tokens);
}
use of org.springframework.security.oauth.consumer.OAuthConsumerToken in project spring-security-oauth by spring-projects.
the class HttpSessionOAuthRememberMeServicesTests method testStoreEverything.
@Test
public void testStoreEverything() {
MockHttpSession mockHttpSession = new MockHttpSession();
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setSession(mockHttpSession);
HttpSessionOAuthRememberMeServices oAuthRememberMeService = new HttpSessionOAuthRememberMeServices();
Map<String, OAuthConsumerToken> tokens = new HashMap<String, OAuthConsumerToken>();
{
OAuthConsumerToken token = new OAuthConsumerToken();
token.setAccessToken(false);
tokens.put("resourceID1", token);
}
{
OAuthConsumerToken token = new OAuthConsumerToken();
token.setAccessToken(true);
tokens.put("resourceID2", token);
}
oAuthRememberMeService.rememberTokens(tokens, request, response);
Assert.assertEquals(1, oAuthRememberMeService.loadRememberedTokens(request, response).size());
}
use of org.springframework.security.oauth.consumer.OAuthConsumerToken in project spring-security-oauth by spring-projects.
the class HttpSessionOAuthRememberMeServicesTests method testStoreRequestTokensOnly.
@Test
public void testStoreRequestTokensOnly() {
MockHttpSession mockHttpSession = new MockHttpSession();
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
request.setSession(mockHttpSession);
HttpSessionOAuthRememberMeServices oAuthRememberMeService = new HttpSessionOAuthRememberMeServices();
Map<String, OAuthConsumerToken> tokens = new HashMap<String, OAuthConsumerToken>();
{
OAuthConsumerToken token = new OAuthConsumerToken();
token.setAccessToken(false);
tokens.put("resourceID1", token);
}
{
OAuthConsumerToken token = new OAuthConsumerToken();
token.setAccessToken(true);
tokens.put("resourceID2", token);
}
oAuthRememberMeService.rememberTokens(tokens, request, response);
Map<String, OAuthConsumerToken> storedTokens = oAuthRememberMeService.loadRememberedTokens(request, response);
Assert.assertEquals(1, storedTokens.size());
Assert.assertNotNull(storedTokens.get("resourceID1"));
}
use of org.springframework.security.oauth.consumer.OAuthConsumerToken in project spring-security-oauth by spring-projects.
the class CoreOAuthConsumerSupport method getTokenFromProvider.
/**
* Get the consumer token with the given parameters and URL. The determination of whether the retrieved token
* is an access token depends on whether a request token is provided.
*
* @param details The resource details.
* @param tokenURL The token URL.
* @param httpMethod The http method.
* @param requestToken The request token, or null if none.
* @param additionalParameters The additional request parameter.
* @return The token.
*/
protected OAuthConsumerToken getTokenFromProvider(ProtectedResourceDetails details, URL tokenURL, String httpMethod, OAuthConsumerToken requestToken, Map<String, String> additionalParameters) {
boolean isAccessToken = requestToken != null;
if (!isAccessToken) {
// create an empty token to make a request for a new unauthorized request token.
requestToken = new OAuthConsumerToken();
}
TreeMap<String, String> requestHeaders = new TreeMap<String, String>();
if ("POST".equalsIgnoreCase(httpMethod)) {
requestHeaders.put("Content-Type", "application/x-www-form-urlencoded");
}
InputStream inputStream = readResource(details, tokenURL, httpMethod, requestToken, additionalParameters, requestHeaders);
String tokenInfo;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = inputStream.read(buffer);
while (len >= 0) {
out.write(buffer, 0, len);
len = inputStream.read(buffer);
}
tokenInfo = new String(out.toByteArray(), "UTF-8");
} catch (IOException e) {
throw new OAuthRequestFailedException("Unable to read the token.", e);
}
StringTokenizer tokenProperties = new StringTokenizer(tokenInfo, "&");
Map<String, String> tokenPropertyValues = new TreeMap<String, String>();
while (tokenProperties.hasMoreElements()) {
try {
String tokenProperty = (String) tokenProperties.nextElement();
int equalsIndex = tokenProperty.indexOf('=');
if (equalsIndex > 0) {
String propertyName = OAuthCodec.oauthDecode(tokenProperty.substring(0, equalsIndex));
String propertyValue = OAuthCodec.oauthDecode(tokenProperty.substring(equalsIndex + 1));
tokenPropertyValues.put(propertyName, propertyValue);
} else {
tokenProperty = OAuthCodec.oauthDecode(tokenProperty);
tokenPropertyValues.put(tokenProperty, null);
}
} catch (DecoderException e) {
throw new OAuthRequestFailedException("Unable to decode token parameters.");
}
}
String tokenValue = tokenPropertyValues.remove(OAuthProviderParameter.oauth_token.toString());
if (tokenValue == null) {
throw new OAuthRequestFailedException("OAuth provider failed to return a token.");
}
String tokenSecret = tokenPropertyValues.remove(OAuthProviderParameter.oauth_token_secret.toString());
if (tokenSecret == null) {
throw new OAuthRequestFailedException("OAuth provider failed to return a token secret.");
}
OAuthConsumerToken consumerToken = new OAuthConsumerToken();
consumerToken.setValue(tokenValue);
consumerToken.setSecret(tokenSecret);
consumerToken.setResourceId(details.getId());
consumerToken.setAccessToken(isAccessToken);
if (!tokenPropertyValues.isEmpty()) {
consumerToken.setAdditionalParameters(tokenPropertyValues);
}
return consumerToken;
}
use of org.springframework.security.oauth.consumer.OAuthConsumerToken in project spring-security-oauth by spring-projects.
the class OAuthClientHttpRequestFactory method createRequest.
public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IOException {
OAuthSecurityContext context = OAuthSecurityContextHolder.getContext();
if (context == null) {
context = new OAuthSecurityContextImpl();
}
Map<String, OAuthConsumerToken> accessTokens = context.getAccessTokens();
OAuthConsumerToken accessToken = accessTokens == null ? null : accessTokens.get(this.resource.getId());
boolean useAuthHeader = this.resource.isAcceptsAuthorizationHeader();
if (!useAuthHeader) {
String queryString = this.support.getOAuthQueryString(this.resource, accessToken, uri.toURL(), httpMethod.name(), this.additionalOAuthParameters);
String uriValue = String.valueOf(uri);
uri = URI.create((uriValue.contains("?") ? uriValue.substring(0, uriValue.indexOf('?')) : uriValue) + "?" + queryString);
}
ClientHttpRequest req = delegate.createRequest(uri, httpMethod);
if (useAuthHeader) {
String authHeader = this.support.getAuthorizationHeader(this.resource, accessToken, uri.toURL(), httpMethod.name(), this.additionalOAuthParameters);
req.getHeaders().add("Authorization", authHeader);
}
Map<String, String> additionalHeaders = this.resource.getAdditionalRequestHeaders();
if (additionalHeaders != null) {
for (Map.Entry<String, String> header : additionalHeaders.entrySet()) {
req.getHeaders().add(header.getKey(), header.getValue());
}
}
return req;
}
Aggregations