use of com.thoughtworks.go.server.security.OauthAuthenticationToken in project gocd by gocd.
the class OauthAuthenticationProvider method authenticate.
public OauthAuthenticationToken authenticate(Authentication authentication) throws AuthenticationException {
OauthAuthenticationToken authenticationToken = (OauthAuthenticationToken) authentication;
String token = authenticationToken.getCredentials();
OauthDataSource.OauthTokenDTO oauthToken = oauthDataSource.findOauthTokenByAccessToken(token);
if (oauthToken == null) {
throw new BadCredentialsException("No match for OAuth token: " + token);
}
String username = oauthToken.getUserId();
UserDetails user = new User(username, token, true, true, true, true, oauthAuthority());
return new OauthAuthenticationToken(user);
}
use of com.thoughtworks.go.server.security.OauthAuthenticationToken in project gocd by gocd.
the class OauthAuthenticationProviderTest method shouldReturnOAUTH_USERAsTheGrantedAuthority.
@Test
public void shouldReturnOAUTH_USERAsTheGrantedAuthority() {
when(dataSource.findOauthTokenByAccessToken("token-string")).thenReturn(oauthTokenDto("user-id"));
GrantedAuthority[] grantedAuthorities = { GoAuthority.ROLE_OAUTH_USER.asAuthority() };
OauthAuthenticationToken authentication = provider.authenticate(new OauthAuthenticationToken("token-string"));
assertThat(authentication.isAuthenticated(), is(true));
UserDetails userDetails = authentication.getPrincipal();
assertThat(userDetails.getUsername(), is("user-id"));
assertThat(userDetails.getAuthorities(), is(grantedAuthorities));
assertThat(authentication.getAuthorities(), is(grantedAuthorities));
}
use of com.thoughtworks.go.server.security.OauthAuthenticationToken in project gocd by gocd.
the class OauthAuthenticationProviderTest method shouldRaiseAuthenticationExceptionWhenNoMatchForTokenExists.
@Test
public void shouldRaiseAuthenticationExceptionWhenNoMatchForTokenExists() {
when(dataSource.findOauthTokenByAccessToken("token-string")).thenReturn(null);
try {
provider.authenticate(new OauthAuthenticationToken("token-string"));
fail("should have thrown an AuthenticationException");
} catch (AuthenticationException e) {
assertThat(e.getMessage(), is("No match for OAuth token: token-string"));
}
}
Aggregations