use of org.springframework.security.core.Authentication in project spring-security-oauth by spring-projects.
the class OAuth2WebSecurityExpressionHandlerTests method testOauthClient.
@Test
public void testOauthClient() throws Exception {
AuthorizationRequest request = new AuthorizationRequest("foo", Collections.singleton("read"));
request.setResourceIdsAndAuthoritiesFromClientDetails(new BaseClientDetails("foo", "", "", "client_credentials", "ROLE_CLIENT"));
OAuth2Request clientAuthentication = RequestTokenFactory.createOAuth2Request(request.getRequestParameters(), request.getClientId(), request.getAuthorities(), request.isApproved(), request.getScope(), request.getResourceIds(), request.getRedirectUri(), request.getResponseTypes(), request.getExtensions());
Authentication userAuthentication = null;
OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(clientAuthentication, userAuthentication);
FilterInvocation invocation = new FilterInvocation("/foo", "GET");
Expression expression = handler.getExpressionParser().parseExpression("#oauth2.clientHasAnyRole('ROLE_CLIENT')");
assertTrue((Boolean) expression.getValue(handler.createEvaluationContext(oAuth2Authentication, invocation)));
}
use of org.springframework.security.core.Authentication in project spring-security-oauth by spring-projects.
the class OAuth2WebSecurityExpressionHandlerTests method testInsufficientScope.
@Test(expected = AccessDeniedException.class)
public void testInsufficientScope() throws Exception {
AuthorizationRequest request = new AuthorizationRequest("foo", Collections.singleton("read"));
request.setResourceIdsAndAuthoritiesFromClientDetails(new BaseClientDetails("foo", "bar", "", "client_credentials", "ROLE_USER"));
OAuth2Request clientAuthentication = request.createOAuth2Request();
Authentication userAuthentication = null;
OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(clientAuthentication, userAuthentication);
OAuth2SecurityExpressionMethods root = new OAuth2SecurityExpressionMethods(oAuth2Authentication);
boolean hasAnyScope = root.hasAnyScope("foo");
root.throwOnError(hasAnyScope);
}
use of org.springframework.security.core.Authentication in project spring-security-oauth by spring-projects.
the class ResourceOwnerPasswordTokenGranterTests method testBadCredentials.
@Test(expected = InvalidGrantException.class)
public void testBadCredentials() {
ResourceOwnerPasswordTokenGranter granter = new ResourceOwnerPasswordTokenGranter(new AuthenticationManager() {
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
throw new BadCredentialsException("test");
}
}, providerTokenServices, clientDetailsService, requestFactory);
granter.grant("password", tokenRequest);
}
use of org.springframework.security.core.Authentication in project camel by apache.
the class SpringSecurityAuthorizationPolicyTest method createAuthenticationToken.
private Authentication createAuthenticationToken(String username, String password, String... roles) {
Authentication authToken;
if (roles != null && roles.length > 0) {
List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(roles.length);
for (String role : roles) {
authorities.add(new SimpleGrantedAuthority(role));
}
authToken = new UsernamePasswordAuthenticationToken(username, password, authorities);
} else {
authToken = new UsernamePasswordAuthenticationToken(username, password);
}
return authToken;
}
use of org.springframework.security.core.Authentication in project camel by apache.
the class SpringSecurityAuthorizationPolicyTest method sendMessageWithAuthentication.
private void sendMessageWithAuthentication(String username, String password, String... roles) {
Authentication authToken = createAuthenticationToken(username, password, roles);
Subject subject = new Subject();
subject.getPrincipals().add(authToken);
template.sendBodyAndHeader("direct:start", "hello world", Exchange.AUTHENTICATION, subject);
}
Aggregations