use of com.thoughtworks.go.server.security.userdetail.GoUserPrinciple in project gocd by gocd.
the class UserHelperTest method shouldGetDisplayNameForAPasswordFileUser.
@Test
public void shouldGetDisplayNameForAPasswordFileUser() {
GrantedAuthority[] authorities = { new GrantedAuthorityImpl("anything") };
TestingAuthenticationToken authentication = new TestingAuthenticationToken(new GoUserPrinciple("user", "Full Name", "password", true, true, true, true, authorities), null, authorities);
assertThat(UserHelper.getUserName(authentication), is(new Username(new CaseInsensitiveString("user"), "Full Name")));
}
use of com.thoughtworks.go.server.security.userdetail.GoUserPrinciple in project gocd by gocd.
the class AuthenticationRequestProcessor method process.
@Override
public GoApiResponse process(GoPluginDescriptor pluginDescriptor, GoApiRequest goPluginApiRequest) {
try {
String version = goPluginApiRequest.apiVersion();
if (!goSupportedVersions.contains(version)) {
throw new RuntimeException(String.format("Unsupported '%s' API version: %s. Supported versions: %s", AUTHENTICATE_USER_REQUEST, version, goSupportedVersions));
}
User user = messageHandlerMap.get(version).responseMessageForAuthenticateUser(goPluginApiRequest.requestBody());
if (user == null) {
throw new RuntimeException(String.format("Could not parse User details. Request Body: %s", goPluginApiRequest.requestBody()));
}
GoUserPrinciple goUserPrincipal = getGoUserPrincipal(user);
Authentication authentication = getAuthenticationToken(goUserPrincipal);
userService.addUserIfDoesNotExist(UserHelper.getUser(authentication));
getSecurityContext().setAuthentication(authentication);
return new DefaultGoApiResponse(200);
} catch (Exception e) {
LOGGER.error("Error occurred while authenticating user", e);
}
return new DefaultGoApiResponse(500);
}
use of com.thoughtworks.go.server.security.userdetail.GoUserPrinciple in project gocd by gocd.
the class PluginAuthenticationProvider method retrieveUser.
@Override
protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) throws AuthenticationException {
User user = getUserDetailsFromAuthorizationPlugins(username, authentication);
if (user == null) {
user = getUserDetailsFromAuthenticationPlugins(username, authentication);
}
if (user == null) {
removeAnyAssociatedPluginRolesFor(username);
throw new UsernameNotFoundException("Unable to authenticate user: " + username);
}
userService.addUserIfDoesNotExist(toDomainUser(user));
GoUserPrinciple goUserPrinciple = getGoUserPrinciple(user);
return goUserPrinciple;
}
use of com.thoughtworks.go.server.security.userdetail.GoUserPrinciple in project gocd by gocd.
the class AuthenticationRequestProcessorTest method shouldAuthenticateUser.
@Test
public void shouldAuthenticateUser() {
String responseBody = "expected-response-body";
User user = new User("username", "display name", "test@test.com");
when(jsonMessageHandler.responseMessageForAuthenticateUser(responseBody)).thenReturn(user);
AuthenticationRequestProcessor processorSpy = spy(processor);
doReturn(securityContext).when(processorSpy).getSecurityContext();
GoApiResponse response = processorSpy.process(pluginDescriptor, getGoPluginApiRequest("1.0", responseBody));
assertThat(response.responseCode(), is(200));
verify(userService).addUserIfDoesNotExist(new com.thoughtworks.go.domain.User("username", "", ""));
GoUserPrinciple goUserPrincipal = processorSpy.getGoUserPrincipal(user);
assertThat(goUserPrincipal.getUsername(), is("username"));
assertThat(goUserPrincipal.getDisplayName(), is("display name"));
verifyGrantAuthorities(goUserPrincipal.getAuthorities());
PreAuthenticatedAuthenticationToken authenticationToken = processorSpy.getAuthenticationToken(goUserPrincipal);
assertThat(authenticationToken.getPrincipal(), is(goUserPrincipal));
verifyGrantAuthorities(authenticationToken.getAuthorities());
verify(securityContext).setAuthentication(authenticationToken);
}
use of com.thoughtworks.go.server.security.userdetail.GoUserPrinciple in project gocd by gocd.
the class FileAuthenticationProviderTest method shouldReturnUserPrincipleWithTheRightDisplayName.
@Test
public void shouldReturnUserPrincipleWithTheRightDisplayName() throws Exception {
setupFile(String.format("jez=%s\ncharan=%s\nbabe=%s", SHA1_BADGER, SHA1_BADGER, SHA1_BADGER));
when(userService.findUserByName("jez")).thenReturn(new com.thoughtworks.go.domain.User("jez", "Jezz Humbles", "jez@humble.com"));
when(userService.findUserByName("charan")).thenReturn(new com.thoughtworks.go.domain.User("charan", "", "ch@ar.an"));
FileAuthenticationProvider provider = new FileAuthenticationProvider(goConfigService, new AuthorityGranter(securityService), userService, securityService);
GoUserPrinciple details = (GoUserPrinciple) provider.retrieveUser("jez", null);
assertThat(details.getUsername(), is("jez"));
assertThat(details.getDisplayName(), is("Jezz Humbles"));
details = (GoUserPrinciple) provider.retrieveUser("charan", null);
assertThat(details.getUsername(), is("charan"));
assertThat(details.getDisplayName(), is("charan"));
details = (GoUserPrinciple) provider.retrieveUser("babe", null);
assertThat(details.getUsername(), is("babe"));
assertThat(details.getDisplayName(), is("babe"));
}
Aggregations