use of org.springframework.security.oauth2.common.DefaultOAuth2AccessToken in project spring-security-oauth by spring-projects.
the class AuthorizationEndpointTests method testImplicitPreApproved.
@Test
public void testImplicitPreApproved() throws Exception {
endpoint.setTokenGranter(new TokenGranter() {
public OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest) {
DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("FOO");
token.setAdditionalInformation(Collections.singletonMap("foo", (Object) "bar"));
return token;
}
});
endpoint.setUserApprovalHandler(new DefaultUserApprovalHandler() {
public AuthorizationRequest checkForPreApproval(AuthorizationRequest authorizationRequest, Authentication userAuthentication) {
return authorizationRequest;
}
public AuthorizationRequest updateAfterApproval(AuthorizationRequest authorizationRequest, Authentication userAuthentication) {
return authorizationRequest;
}
public boolean isApproved(AuthorizationRequest authorizationRequest, Authentication userAuthentication) {
return true;
}
});
AuthorizationRequest authorizationRequest = getAuthorizationRequest("foo", "http://anywhere.com", "mystate", "myscope", Collections.singleton("token"));
ModelAndView result = endpoint.authorize(model, authorizationRequest.getRequestParameters(), sessionStatus, principal);
String url = ((RedirectView) result.getView()).getUrl();
assertTrue("Wrong view: " + result, url.startsWith("http://anywhere.com"));
assertTrue("Wrong state: " + result, url.contains("&state=mystate"));
assertTrue("Wrong token: " + result, url.contains("access_token="));
assertTrue("Wrong token: " + result, url.contains("foo=bar"));
}
use of org.springframework.security.oauth2.common.DefaultOAuth2AccessToken in project spring-security-oauth by spring-projects.
the class AuthorizationEndpointTests method testImplicitAppendsScope.
@Test
public void testImplicitAppendsScope() throws Exception {
endpoint.setTokenGranter(new TokenGranter() {
public OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest) {
DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("FOO");
token.setScope(Collections.singleton("read"));
return token;
}
});
endpoint.setUserApprovalHandler(new DefaultUserApprovalHandler() {
public AuthorizationRequest checkForPreApproval(AuthorizationRequest authorizationRequest, Authentication userAuthentication) {
return authorizationRequest;
}
public AuthorizationRequest updateAfterApproval(AuthorizationRequest authorizationRequest, Authentication userAuthentication) {
return authorizationRequest;
}
public boolean isApproved(AuthorizationRequest authorizationRequest, Authentication userAuthentication) {
return true;
}
});
AuthorizationRequest authorizationRequest = getAuthorizationRequest("foo", "http://anywhere.com", "mystate", "myscope", Collections.singleton("token"));
ModelAndView result = endpoint.authorize(model, authorizationRequest.getRequestParameters(), sessionStatus, principal);
String url = ((RedirectView) result.getView()).getUrl();
assertTrue("Wrong scope: " + result, url.contains("&scope=read"));
}
use of org.springframework.security.oauth2.common.DefaultOAuth2AccessToken in project spring-security-oauth by spring-projects.
the class AbstractPersistentDefaultTokenServicesTests method testRefreshedTokenIsEnhanced.
@Test
public void testRefreshedTokenIsEnhanced() throws Exception {
getTokenServices().setTokenEnhancer(new TokenEnhancer() {
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {
DefaultOAuth2AccessToken result = new DefaultOAuth2AccessToken(accessToken);
result.setValue("I'mEnhanced");
return result;
}
});
OAuth2AccessToken accessToken = getTokenServices().createAccessToken(createAuthentication());
assertTrue(accessToken.getValue().startsWith("I'mEnhanced"));
TokenRequest tokenRequest = new TokenRequest(Collections.singletonMap("client_id", "id"), "id", null, null);
OAuth2AccessToken refreshedAccessToken = getTokenServices().refreshAccessToken(accessToken.getRefreshToken().getValue(), tokenRequest);
assertTrue(refreshedAccessToken.getValue().startsWith("I'mEnhanced"));
}
use of org.springframework.security.oauth2.common.DefaultOAuth2AccessToken in project spring-security-oauth by spring-projects.
the class DefaultAccessTokenConverterTests method extractAuthenticationFromClientToken.
@Test
public void extractAuthenticationFromClientToken() {
DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("FOO");
OAuth2Authentication authentication = new OAuth2Authentication(request, null);
token.setScope(authentication.getOAuth2Request().getScope());
Map<String, ?> map = converter.convertAccessToken(token, authentication);
assertTrue(map.containsKey(AccessTokenConverter.AUD));
assertTrue(map.containsKey(AccessTokenConverter.SCOPE));
assertTrue(map.containsKey(AccessTokenConverter.AUTHORITIES));
assertEquals(singleton(ROLE_CLIENT), map.get(AccessTokenConverter.AUTHORITIES));
OAuth2Authentication extracted = converter.extractAuthentication(map);
assertTrue(extracted.getOAuth2Request().getResourceIds().contains("resource"));
assertEquals("[ROLE_CLIENT]", extracted.getAuthorities().toString());
}
use of org.springframework.security.oauth2.common.DefaultOAuth2AccessToken in project spring-security-oauth by spring-projects.
the class DefaultAccessTokenConverterTests method extractAuthentication.
@Test
public void extractAuthentication() {
DefaultOAuth2AccessToken token = new DefaultOAuth2AccessToken("FOO");
OAuth2Authentication authentication = new OAuth2Authentication(request, userAuthentication);
token.setScope(authentication.getOAuth2Request().getScope());
Map<String, ?> map = converter.convertAccessToken(token, authentication);
assertTrue(map.containsKey(AccessTokenConverter.AUD));
assertTrue(map.containsKey(AccessTokenConverter.SCOPE));
assertTrue(map.containsKey(AccessTokenConverter.AUTHORITIES));
assertEquals(singleton(ROLE_USER), map.get(AccessTokenConverter.AUTHORITIES));
OAuth2Authentication extracted = converter.extractAuthentication(map);
assertTrue(extracted.getOAuth2Request().getResourceIds().contains("resource"));
assertEquals("[ROLE_USER]", extracted.getAuthorities().toString());
}
Aggregations