use of org.springframework.security.oauth2.client.OAuth2RestTemplate in project spring-security-oauth by spring-projects.
the class OAuth2RestTemplateTests method open.
@Before
public void open() throws Exception {
resource = new BaseOAuth2ProtectedResourceDetails();
// Facebook and older specs:
resource.setTokenName("bearer_token");
restTemplate = new OAuth2RestTemplate(resource);
restTemplate.setAccessTokenProvider(accessTokenProvider);
request = Mockito.mock(ClientHttpRequest.class);
headers = new HttpHeaders();
Mockito.when(request.getHeaders()).thenReturn(headers);
ClientHttpResponse response = Mockito.mock(ClientHttpResponse.class);
HttpStatus statusCode = HttpStatus.OK;
Mockito.when(response.getStatusCode()).thenReturn(statusCode);
Mockito.when(request.execute()).thenReturn(response);
}
use of org.springframework.security.oauth2.client.OAuth2RestTemplate in project spring-security-oauth by spring-projects.
the class AbstractEmptyAuthorizationCodeProviderTests method verifyAuthorizationPage.
private void verifyAuthorizationPage(OAuth2RestTemplate restTemplate, String location) {
final AtomicReference<String> confirmationPage = new AtomicReference<String>();
AuthorizationCodeAccessTokenProvider provider = new AuthorizationCodeAccessTokenProvider() {
@Override
protected ResponseExtractor<ResponseEntity<Void>> getAuthorizationResponseExtractor() {
return new ResponseExtractor<ResponseEntity<Void>>() {
public ResponseEntity<Void> extractData(ClientHttpResponse response) throws IOException {
confirmationPage.set(StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8")));
return new ResponseEntity<Void>(response.getHeaders(), response.getStatusCode());
}
};
}
};
try {
provider.obtainAuthorizationCode(restTemplate.getResource(), restTemplate.getOAuth2ClientContext().getAccessTokenRequest());
} catch (UserApprovalRequiredException e) {
// ignore
}
String page = confirmationPage.get();
verifyAuthorizationPage(page);
}
use of org.springframework.security.oauth2.client.OAuth2RestTemplate in project spring-security-oauth by spring-projects.
the class AbstractAuthorizationCodeProviderTests method verifyAuthorizationPage.
private void verifyAuthorizationPage(OAuth2RestTemplate restTemplate, String location) {
final AtomicReference<String> confirmationPage = new AtomicReference<String>();
AuthorizationCodeAccessTokenProvider provider = new AuthorizationCodeAccessTokenProvider() {
@Override
protected ResponseExtractor<ResponseEntity<Void>> getAuthorizationResponseExtractor() {
return new ResponseExtractor<ResponseEntity<Void>>() {
public ResponseEntity<Void> extractData(ClientHttpResponse response) throws IOException {
confirmationPage.set(StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8")));
return new ResponseEntity<Void>(response.getHeaders(), response.getStatusCode());
}
};
}
};
try {
provider.obtainAuthorizationCode(restTemplate.getResource(), restTemplate.getOAuth2ClientContext().getAccessTokenRequest());
} catch (UserApprovalRequiredException e) {
// ignore
}
String page = confirmationPage.get();
verifyAuthorizationPage(page);
}
use of org.springframework.security.oauth2.client.OAuth2RestTemplate in project spring-boot by spring-projects.
the class UserInfoTokenServicesRefreshTokenTests method withRestTemplate.
@Test
public void withRestTemplate() {
OAuth2ProtectedResourceDetails resource = new AuthorizationCodeResourceDetails();
OAuth2ClientContext context = new DefaultOAuth2ClientContext();
DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("FOO");
token.setRefreshToken(new DefaultExpiringOAuth2RefreshToken("BAR", new Date(0L)));
context.setAccessToken(token);
this.services.setRestTemplate(new OAuth2RestTemplate(resource, context));
assertThat(this.services.loadAuthentication("FOO").getName()).isEqualTo("me");
assertThat(context.getAccessToken().getValue()).isEqualTo("FOO");
// The refresh token is still intact
assertThat(context.getAccessToken().getRefreshToken()).isEqualTo(token.getRefreshToken());
}
use of org.springframework.security.oauth2.client.OAuth2RestTemplate in project spring-boot by spring-projects.
the class UserInfoTokenServices method getMap.
@SuppressWarnings({ "unchecked" })
private Map<String, Object> getMap(String path, String accessToken) {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Getting user info from: " + path);
}
try {
OAuth2RestOperations restTemplate = this.restTemplate;
if (restTemplate == null) {
BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails();
resource.setClientId(this.clientId);
restTemplate = new OAuth2RestTemplate(resource);
}
OAuth2AccessToken existingToken = restTemplate.getOAuth2ClientContext().getAccessToken();
if (existingToken == null || !accessToken.equals(existingToken.getValue())) {
DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken(accessToken);
token.setTokenType(this.tokenType);
restTemplate.getOAuth2ClientContext().setAccessToken(token);
}
return restTemplate.getForEntity(path, Map.class).getBody();
} catch (Exception ex) {
this.logger.warn("Could not fetch user details: " + ex.getClass() + ", " + ex.getMessage());
return Collections.<String, Object>singletonMap("error", "Could not fetch user details");
}
}
Aggregations