Search in sources :

Example 26 with AuthenticationState

use of org.zaproxy.zap.users.AuthenticationState in project zaproxy by zaproxy.

the class AuthenticationMethodPollUrlUnitTest method shouldPollOnSpecifiedNumberOfRequestsPerUser.

@Test
void shouldPollOnSpecifiedNumberOfRequestsPerUser() throws NullPointerException, IOException {
    // Given
    String test = "/shouldPollOnFirstRequest/test";
    String pollUrl = "/shouldPollOnFirstRequest/pollUrl";
    final List<String> orderedReqs = new ArrayList<>();
    this.nano.addHandler(new NanoServerHandler(pollUrl) {

        @Override
        protected Response serve(IHTTPSession session) {
            orderedReqs.add(session.getUri());
            return newFixedLengthResponse(LOGGED_IN_BODY);
        }
    });
    HttpMessage testMsg = this.getHttpMessage(test);
    HttpMessage pollMsg = this.getHttpMessage(pollUrl);
    method.setAuthCheckingStrategy(AuthCheckingStrategy.POLL_URL);
    method.setPollUrl(pollMsg.getRequestHeader().getURI().toString() + "?");
    method.setPollFrequencyUnits(AuthPollFrequencyUnits.REQUESTS);
    method.setPollFrequency(5);
    method.setLoggedInIndicatorPattern(LOGGED_IN_INDICATOR);
    User user1 = mock(User.class);
    given(user1.getAuthenticationState()).willReturn(new AuthenticationState());
    User user2 = mock(User.class);
    given(user2.getAuthenticationState()).willReturn(new AuthenticationState());
    // When/Then
    assertThat(method.isAuthenticated(testMsg, user1), is(true));
    // First poll for user1
    assertThat(orderedReqs.size(), is(1));
    assertThat(method.isAuthenticated(testMsg, user1), is(true));
    assertThat(method.isAuthenticated(testMsg, user1), is(true));
    assertThat(method.isAuthenticated(testMsg, user2), is(true));
    // First poll for user2
    assertThat(orderedReqs.size(), is(2));
    assertThat(method.isAuthenticated(testMsg, user2), is(true));
    assertThat(method.isAuthenticated(testMsg, user1), is(true));
    assertThat(method.isAuthenticated(testMsg, user1), is(true));
    assertThat(method.isAuthenticated(testMsg, user1), is(true));
    // Should not have changed yet
    assertThat(orderedReqs.size(), is(2));
    assertThat(method.isAuthenticated(testMsg, user1), is(true));
    // Second poll for user1
    assertThat(orderedReqs.size(), is(3));
    assertThat(method.isAuthenticated(testMsg, user1), is(true));
    assertThat(method.isAuthenticated(testMsg, user1), is(true));
    assertThat(method.isAuthenticated(testMsg, user1), is(true));
    assertThat(method.isAuthenticated(testMsg, user2), is(true));
    assertThat(method.isAuthenticated(testMsg, user2), is(true));
    assertThat(method.isAuthenticated(testMsg, user2), is(true));
    assertThat(method.isAuthenticated(testMsg, user2), is(true));
    // Should not have changed yet
    assertThat(orderedReqs.size(), is(3));
    assertThat(method.isAuthenticated(testMsg, user2), is(true));
    // Second poll for user2
    assertThat(orderedReqs.size(), is(4));
}
Also used : Response(fi.iki.elonen.NanoHTTPD.Response) NanoHTTPD.newFixedLengthResponse(fi.iki.elonen.NanoHTTPD.newFixedLengthResponse) User(org.zaproxy.zap.users.User) NanoServerHandler(org.zaproxy.zap.testutils.NanoServerHandler) ArrayList(java.util.ArrayList) IHTTPSession(fi.iki.elonen.NanoHTTPD.IHTTPSession) HttpMessage(org.parosproxy.paros.network.HttpMessage) AuthenticationState(org.zaproxy.zap.users.AuthenticationState) Test(org.junit.jupiter.api.Test)

Example 27 with AuthenticationState

use of org.zaproxy.zap.users.AuthenticationState in project zaproxy by zaproxy.

the class AuthenticationMethodIndicatorsUnitTest method shouldIdentifyLoggedInRequestBodyWhenLoggedInIndicatorIsSet.

@Test
void shouldIdentifyLoggedInRequestBodyWhenLoggedInIndicatorIsSet() {
    // Given
    method.setLoggedInIndicatorPattern(LOGGED_IN_INDICATOR);
    method.setAuthCheckingStrategy(AuthCheckingStrategy.EACH_REQ);
    loginMessage.setRequestBody(LOGGED_IN_BODY);
    User user = mock(User.class);
    given(user.getAuthenticationState()).willReturn(new AuthenticationState());
    // When/Then
    assertThat(method.isAuthenticated(loginMessage, user), is(true));
}
Also used : User(org.zaproxy.zap.users.User) AuthenticationState(org.zaproxy.zap.users.AuthenticationState) Test(org.junit.jupiter.api.Test)

Example 28 with AuthenticationState

use of org.zaproxy.zap.users.AuthenticationState in project zaproxy by zaproxy.

the class AuthenticationMethodIndicatorsUnitTest method shouldIdentifyResponseAsLoggedInWhenNoIndicatorIsSet.

@Test
void shouldIdentifyResponseAsLoggedInWhenNoIndicatorIsSet() {
    // Given
    loginMessage.setResponseBody(LOGGED_OUT_BODY);
    User user = mock(User.class);
    given(user.getAuthenticationState()).willReturn(new AuthenticationState());
    // When/Then
    assertThat(method.isAuthenticated(loginMessage, user), is(true));
}
Also used : User(org.zaproxy.zap.users.User) AuthenticationState(org.zaproxy.zap.users.AuthenticationState) Test(org.junit.jupiter.api.Test)

Example 29 with AuthenticationState

use of org.zaproxy.zap.users.AuthenticationState in project zaproxy by zaproxy.

the class AuthenticationMethodIndicatorsUnitTest method shouldIdentifyLoggedInRequestWithComplexRegex.

@Test
void shouldIdentifyLoggedInRequestWithComplexRegex() {
    // Given
    method.setLoggedOutIndicatorPattern(LOGGED_OUT_COMPLEX_INDICATOR);
    method.setAuthCheckingStrategy(AuthCheckingStrategy.EACH_REQ);
    loginMessage.setRequestBody(LOGGED_OUT_BODY);
    User user = mock(User.class);
    given(user.getAuthenticationState()).willReturn(new AuthenticationState());
    // When/Then
    assertThat(method.isAuthenticated(loginMessage, user), is(true));
}
Also used : User(org.zaproxy.zap.users.User) AuthenticationState(org.zaproxy.zap.users.AuthenticationState) Test(org.junit.jupiter.api.Test)

Example 30 with AuthenticationState

use of org.zaproxy.zap.users.AuthenticationState in project zaproxy by zaproxy.

the class AuthenticationMethodIndicatorsUnitTest method shouldIdentifyLoggedInRequestBodyWhenLoggedOutIndicatorIsSet.

@Test
void shouldIdentifyLoggedInRequestBodyWhenLoggedOutIndicatorIsSet() {
    // Given
    method.setLoggedOutIndicatorPattern(LOGGED_OUT_INDICATOR);
    method.setAuthCheckingStrategy(AuthCheckingStrategy.EACH_REQ);
    loginMessage.setRequestBody(LOGGED_IN_BODY);
    User user = mock(User.class);
    given(user.getAuthenticationState()).willReturn(new AuthenticationState());
    // When/Then
    assertThat(method.isAuthenticated(loginMessage, user), is(true));
}
Also used : User(org.zaproxy.zap.users.User) AuthenticationState(org.zaproxy.zap.users.AuthenticationState) Test(org.junit.jupiter.api.Test)

Aggregations

AuthenticationState (org.zaproxy.zap.users.AuthenticationState)36 User (org.zaproxy.zap.users.User)34 Test (org.junit.jupiter.api.Test)33 HttpMessage (org.parosproxy.paros.network.HttpMessage)14 IHTTPSession (fi.iki.elonen.NanoHTTPD.IHTTPSession)11 Response (fi.iki.elonen.NanoHTTPD.Response)11 NanoHTTPD.newFixedLengthResponse (fi.iki.elonen.NanoHTTPD.newFixedLengthResponse)11 ArrayList (java.util.ArrayList)11 NanoServerHandler (org.zaproxy.zap.testutils.NanoServerHandler)11 IOException (java.io.IOException)8 HashMap (java.util.HashMap)6 WithConfigsTest (org.zaproxy.zap.WithConfigsTest)6 JSONException (net.sf.json.JSONException)1 JSONObject (net.sf.json.JSONObject)1 Cookie (org.apache.commons.httpclient.Cookie)1 URI (org.apache.commons.httpclient.URI)1 URIException (org.apache.commons.httpclient.URIException)1 ExtensionHistory (org.parosproxy.paros.extension.history.ExtensionHistory)1 HistoryReference (org.parosproxy.paros.model.HistoryReference)1 ApiDynamicActionImplementor (org.zaproxy.zap.extension.api.ApiDynamicActionImplementor)1