Search in sources :

Example 26 with SecurityAuthConfig

use of com.thoughtworks.go.config.SecurityAuthConfig in project gocd by gocd.

the class SecurityAuthConfigServiceTest method verifyConnection_shouldSendConnectionFailedResponseOnUnSuccessfulVerification.

@Test
public void verifyConnection_shouldSendConnectionFailedResponseOnUnSuccessfulVerification() throws Exception {
    VerifyConnectionResponse success = new VerifyConnectionResponse("failure", "Connection check failed", new ValidationResult());
    SecurityAuthConfig ldap = new SecurityAuthConfig("ldap", "cd.go.ldap");
    when(extension.verifyConnection("cd.go.ldap", ldap.getConfigurationAsMap(true))).thenReturn(success);
    VerifyConnectionResponse response = securityAuthConfigService.verifyConnection(ldap);
    assertThat(response, is(success));
}
Also used : VerifyConnectionResponse(com.thoughtworks.go.plugin.domain.common.VerifyConnectionResponse) SecurityAuthConfig(com.thoughtworks.go.config.SecurityAuthConfig) ValidationResult(com.thoughtworks.go.plugin.domain.common.ValidationResult) Test(org.junit.jupiter.api.Test)

Example 27 with SecurityAuthConfig

use of com.thoughtworks.go.config.SecurityAuthConfig in project gocd by gocd.

the class RevokeStaleAccessTokenServiceTest method setUp.

@BeforeEach
void setUp() {
    service = new RevokeStaleAccessTokenService(goConfigService, accessTokenService);
    authConfig1 = new SecurityAuthConfig("authConfig1", "ldap");
    authConfig2 = new SecurityAuthConfig("authConfig2", "ldap");
    authConfig1_token1 = AccessToken.create(null, null, "authConfig1", new TestingClock());
    authConfig1_token1.setId(0);
    authConfig1_token2 = AccessToken.create(null, null, "authConfig1", new TestingClock());
    authConfig1_token2.setId(1);
    authConfig2_token1 = AccessToken.create(null, null, "authConfig2", new TestingClock());
    authConfig2_token1.setId(2);
}
Also used : SecurityAuthConfig(com.thoughtworks.go.config.SecurityAuthConfig) TestingClock(com.thoughtworks.go.util.TestingClock) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 28 with SecurityAuthConfig

use of com.thoughtworks.go.config.SecurityAuthConfig in project gocd by gocd.

the class AccessTokenAuthenticationFilterTest method setUp.

@BeforeEach
void setUp() throws Exception {
    clock = new TestingClock();
    securityService = mock(SecurityService.class);
    accessTokenService = mock(AccessTokenService.class);
    authenticationProvider = mock(AccessTokenBasedPluginAuthenticationProvider.class);
    securityAuthConfigService = mock(SecurityAuthConfigService.class);
    response = new MockHttpServletResponse();
    filterChain = mock(FilterChain.class);
    filter = new AccessTokenAuthenticationFilter(securityService, accessTokenService, securityAuthConfigService, authenticationProvider);
    accessToken = randomAccessTokenForUser(BOB);
    when(accessTokenService.findByAccessToken(TOKEN)).thenReturn(accessToken);
    authConfig = new SecurityAuthConfig(accessToken.getAuthConfigId(), PLUGIN_ID);
    when(securityAuthConfigService.findProfile(accessToken.getAuthConfigId())).thenReturn(authConfig);
}
Also used : SecurityAuthConfigService(com.thoughtworks.go.server.service.SecurityAuthConfigService) AccessTokenBasedPluginAuthenticationProvider(com.thoughtworks.go.server.newsecurity.providers.AccessTokenBasedPluginAuthenticationProvider) SecurityAuthConfig(com.thoughtworks.go.config.SecurityAuthConfig) AccessTokenService(com.thoughtworks.go.server.service.AccessTokenService) SecurityService(com.thoughtworks.go.server.service.SecurityService) FilterChain(javax.servlet.FilterChain) TestingClock(com.thoughtworks.go.util.TestingClock) MockHttpServletResponse(com.thoughtworks.go.http.mocks.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 29 with SecurityAuthConfig

use of com.thoughtworks.go.config.SecurityAuthConfig in project gocd by gocd.

the class AccessTokenAuthenticationFilter method filterWhenSecurityEnabled.

private void filterWhenSecurityEnabled(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain, AccessTokenCredential accessTokenCredential) throws IOException, ServletException {
    if (accessTokenCredential == null) {
        LOGGER.debug("Bearer auth credentials are not provided in request.");
        filterChain.doFilter(request, response);
    } else {
        accessTokenService.updateLastUsedCacheWith(accessTokenCredential.getAccessToken());
        ACCESS_TOKEN_LOGGER.debug("[Bearer Token Authentication] Authenticating bearer token for: " + "GoCD User: '{}'. " + "GoCD API endpoint: '{}', " + "API Client: '{}', " + "Is Admin Scoped Token: '{}', " + "Current Time: '{}'.", accessTokenCredential.getAccessToken().getUsername(), request.getRequestURI(), request.getHeader("User-Agent"), securityService.isUserAdmin(new Username(accessTokenCredential.getAccessToken().getUsername())), new Timestamp(System.currentTimeMillis()));
        try {
            String authConfigId = accessTokenCredential.getAccessToken().getAuthConfigId();
            SecurityAuthConfig authConfig = securityAuthConfigService.findProfile(authConfigId);
            if (authConfig == null) {
                String errorMessage = String.format("Can not find authorization configuration \"%s\" to which the requested personal access token belongs. Authorization Configuration \"%s\" might have been renamed or deleted. Please revoke the existing token and create a new one for the same.", authConfigId, authConfigId);
                onAuthenticationFailure(request, response, errorMessage);
                return;
            }
            final AuthenticationToken<AccessTokenCredential> authenticationToken = authenticationProvider.authenticateUser(accessTokenCredential, authConfig);
            if (authenticationToken == null) {
                onAuthenticationFailure(request, response, BAD_CREDENTIALS_MSG);
            } else {
                SessionUtils.setAuthenticationTokenAfterRecreatingSession(authenticationToken, request);
                filterChain.doFilter(request, response);
            }
        } catch (AuthenticationException e) {
            LOGGER.debug("Failed to authenticate user.", e);
            onAuthenticationFailure(request, response, e.getMessage());
        }
    }
}
Also used : SecurityAuthConfig(com.thoughtworks.go.config.SecurityAuthConfig) Username(com.thoughtworks.go.server.domain.Username) AuthenticationException(org.springframework.security.core.AuthenticationException) Timestamp(java.sql.Timestamp) AccessTokenCredential(com.thoughtworks.go.server.newsecurity.models.AccessTokenCredential)

Example 30 with SecurityAuthConfig

use of com.thoughtworks.go.config.SecurityAuthConfig in project gocd by gocd.

the class AbstractPluginAuthenticationProvider method reauthenticate.

@Override
public AuthenticationToken<T> reauthenticate(AuthenticationToken<T> authenticationToken) {
    final String authConfigId = authenticationToken.getAuthConfigId();
    final T credentials = authenticationToken.getCredentials();
    final SecurityAuthConfig authConfig = goConfigService.security().securityAuthConfigs().find(authConfigId);
    AuthenticationToken<T> reAuthenticatedToken;
    if (authConfig == null) {
        reAuthenticatedToken = authenticate(credentials, authenticationToken.getPluginId());
    } else {
        reAuthenticatedToken = authenticateUser(credentials, authConfig);
    }
    if (reAuthenticatedToken == null) {
        removeAnyAssociatedPluginRolesFor(getUsername(authenticationToken));
    }
    return reAuthenticatedToken;
}
Also used : SecurityAuthConfig(com.thoughtworks.go.config.SecurityAuthConfig) CaseInsensitiveString(com.thoughtworks.go.config.CaseInsensitiveString)

Aggregations

SecurityAuthConfig (com.thoughtworks.go.config.SecurityAuthConfig)81 Test (org.junit.jupiter.api.Test)46 HttpLocalizedOperationResult (com.thoughtworks.go.server.service.result.HttpLocalizedOperationResult)28 Test (org.junit.Test)16 CaseInsensitiveString (com.thoughtworks.go.config.CaseInsensitiveString)14 AuthenticationResponse (com.thoughtworks.go.plugin.access.authorization.models.AuthenticationResponse)14 User (com.thoughtworks.go.plugin.access.authorization.models.User)11 PluginRoleConfig (com.thoughtworks.go.config.PluginRoleConfig)9 Username (com.thoughtworks.go.server.domain.Username)9 UserDetails (org.springframework.security.userdetails.UserDetails)8 ConfigurationProperty (com.thoughtworks.go.domain.config.ConfigurationProperty)7 SecurityConfig (com.thoughtworks.go.config.SecurityConfig)5 VerifyConnectionResponse (com.thoughtworks.go.plugin.domain.common.VerifyConnectionResponse)5 UsernamePasswordAuthenticationToken (org.springframework.security.providers.UsernamePasswordAuthenticationToken)5 BasicCruiseConfig (com.thoughtworks.go.config.BasicCruiseConfig)4 SecurityAuthConfigs (com.thoughtworks.go.config.SecurityAuthConfigs)4 RecordNotFoundException (com.thoughtworks.go.config.exceptions.RecordNotFoundException)4 DefaultGoPluginApiResponse (com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse)4 ValidationResult (com.thoughtworks.go.plugin.domain.common.ValidationResult)4 GoUserPrinciple (com.thoughtworks.go.server.security.userdetail.GoUserPrinciple)4