Search in sources :

Example 21 with AuthenticationState

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

the class AuthenticationMethod method pollAsUser.

public HttpMessage pollAsUser(User user) throws IOException {
    if (!this.authCheckingStrategy.equals(AuthCheckingStrategy.POLL_URL)) {
        throw new IllegalArgumentException("Authentication checking strategy is not POLL_URL");
    }
    HttpMessage pollMsg = new HttpMessage(new URI(this.getPollUrl(), true));
    if (this.getPollData() != null && this.getPollData().length() > 0) {
        pollMsg.getRequestHeader().setMethod(HttpRequestHeader.POST);
        pollMsg.getRequestBody().setBody(this.getPollData());
        pollMsg.getRequestHeader().setContentLength(pollMsg.getRequestBody().length());
    }
    if (this.getPollHeaders() != null && this.getPollHeaders().length() > 0) {
        for (String header : this.getPollHeaders().split("\n")) {
            String[] headerValue = header.split(":");
            if (headerValue.length == 2) {
                pollMsg.getRequestHeader().addHeader(headerValue[0].trim(), headerValue[1].trim());
            } else {
                LOGGER.error("Invalid header '" + header + "' for poll request to " + this.getPollUrl());
            }
        }
    }
    pollMsg.setRequestingUser(user);
    replaceUserDataInPollRequest(pollMsg, user);
    getHttpSender().sendAndReceive(pollMsg);
    AuthenticationHelper.addAuthMessageToHistory(pollMsg);
    AuthenticationState authState = user.getAuthenticationState();
    authState.setLastPollTime(System.currentTimeMillis());
    authState.setRequestsSincePoll(0);
    return pollMsg;
}
Also used : HttpMessage(org.parosproxy.paros.network.HttpMessage) URI(org.apache.commons.httpclient.URI) AuthenticationState(org.zaproxy.zap.users.AuthenticationState)

Example 22 with AuthenticationState

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

the class JsonBasedAuthenticationMethodTypeUnitTest method shouldNotReplacePasswordInPollRequest.

@Test
void shouldNotReplacePasswordInPollRequest() throws NullPointerException, IOException {
    // Given
    String test = "/shouldNotReplacePasswordInPollRequest/test";
    String pollUrl = "/shouldNotReplacePasswordInPollRequest/pollUrl";
    String pollData = "pwd=" + PostBasedAuthenticationMethod.MSG_PASS_PATTERN;
    String password = "password123!";
    final List<String> orderedReqData = new ArrayList<>();
    this.nano.addHandler(new NanoServerHandler(pollUrl) {

        @Override
        protected Response serve(IHTTPSession session) {
            HashMap<String, String> map = new HashMap<>();
            try {
                session.parseBody(map);
                orderedReqData.add(map.get("postData"));
            } catch (Exception e) {
            }
            return newFixedLengthResponse(LOGGED_IN_BODY);
        }
    });
    HttpMessage testMsg = this.getHttpMessage(test);
    HttpMessage pollMsg = this.getHttpMessage(pollUrl);
    method.setPollUrl(pollMsg.getRequestHeader().getURI().toString());
    method.setPollData(pollData);
    User user = mock(User.class);
    given(user.getAuthenticationState()).willReturn(new AuthenticationState());
    given(user.getAuthenticationCredentials()).willReturn(new UsernamePasswordAuthenticationCredentials("", password));
    // When/Then
    assertThat(method.isAuthenticated(testMsg, user), is(true));
    assertThat(orderedReqData.size(), is(1));
    assertThat(orderedReqData.get(0), is(pollData));
}
Also used : User(org.zaproxy.zap.users.User) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IHTTPSession(fi.iki.elonen.NanoHTTPD.IHTTPSession) IOException(java.io.IOException) AuthenticationState(org.zaproxy.zap.users.AuthenticationState) Response(fi.iki.elonen.NanoHTTPD.Response) NanoHTTPD.newFixedLengthResponse(fi.iki.elonen.NanoHTTPD.newFixedLengthResponse) NanoServerHandler(org.zaproxy.zap.testutils.NanoServerHandler) HttpMessage(org.parosproxy.paros.network.HttpMessage) Test(org.junit.jupiter.api.Test) WithConfigsTest(org.zaproxy.zap.WithConfigsTest)

Example 23 with AuthenticationState

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

the class FormBasedAuthenticationMethodTypeUnitTest method shouldNotReplacePasswordInPollRequest.

@Test
void shouldNotReplacePasswordInPollRequest() throws NullPointerException, IOException {
    // Given
    String test = "/shouldNotReplacePasswordInPollRequest/test";
    String pollUrl = "/shouldNotReplacePasswordInPollRequest/pollUrl";
    String pollData = "pwd=" + PostBasedAuthenticationMethod.MSG_PASS_PATTERN;
    String password = "password123!";
    final List<String> orderedReqData = new ArrayList<>();
    this.nano.addHandler(new NanoServerHandler(pollUrl) {

        @Override
        protected Response serve(IHTTPSession session) {
            HashMap<String, String> map = new HashMap<>();
            try {
                session.parseBody(map);
                orderedReqData.add(map.get("postData"));
            } catch (Exception e) {
            }
            return newFixedLengthResponse(LOGGED_IN_BODY);
        }
    });
    HttpMessage testMsg = this.getHttpMessage(test);
    HttpMessage pollMsg = this.getHttpMessage(pollUrl);
    method.setPollUrl(pollMsg.getRequestHeader().getURI().toString());
    method.setPollData(pollData);
    User user = mock(User.class);
    given(user.getAuthenticationState()).willReturn(new AuthenticationState());
    given(user.getAuthenticationCredentials()).willReturn(new UsernamePasswordAuthenticationCredentials("", password));
    // When/Then
    assertThat(method.isAuthenticated(testMsg, user), is(true));
    assertThat(orderedReqData.size(), is(1));
    assertThat(orderedReqData.get(0), is(pollData));
}
Also used : User(org.zaproxy.zap.users.User) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IHTTPSession(fi.iki.elonen.NanoHTTPD.IHTTPSession) IOException(java.io.IOException) AuthenticationState(org.zaproxy.zap.users.AuthenticationState) Response(fi.iki.elonen.NanoHTTPD.Response) NanoHTTPD.newFixedLengthResponse(fi.iki.elonen.NanoHTTPD.newFixedLengthResponse) NanoServerHandler(org.zaproxy.zap.testutils.NanoServerHandler) HttpMessage(org.parosproxy.paros.network.HttpMessage) Test(org.junit.jupiter.api.Test) WithConfigsTest(org.zaproxy.zap.WithConfigsTest)

Example 24 with AuthenticationState

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

the class FormBasedAuthenticationMethodTypeUnitTest method shouldReplaceUsernameInPollRequest.

@Test
void shouldReplaceUsernameInPollRequest() throws NullPointerException, IOException {
    // Given
    String test = "/shouldReplaceUsernameInPollRequest/test";
    String encodedPattern = URLEncoder.encode(PostBasedAuthenticationMethod.MSG_USER_PATTERN, StandardCharsets.UTF_8.name());
    String pollUrl = "/shouldReplaceUsernameInPollRequest/pollUrl";
    String pollData = "user=" + PostBasedAuthenticationMethod.MSG_USER_PATTERN;
    String username = "user";
    final List<String> orderedReqUrls = new ArrayList<>();
    final List<String> orderedReqData = new ArrayList<>();
    this.nano.addHandler(new NanoServerHandler(pollUrl.replace(encodedPattern, username)) {

        @Override
        protected Response serve(IHTTPSession session) {
            orderedReqUrls.add(session.getUri() + "?" + session.getQueryParameterString());
            HashMap<String, String> map = new HashMap<>();
            try {
                session.parseBody(map);
                orderedReqData.add(map.get("postData"));
            } catch (Exception e) {
            }
            return newFixedLengthResponse(LOGGED_IN_BODY);
        }
    });
    HttpMessage testMsg = this.getHttpMessage(test);
    HttpMessage pollMsg = this.getHttpMessage(pollUrl + "?" + encodedPattern);
    method.setPollUrl(pollMsg.getRequestHeader().getURI().toString());
    method.setPollData(pollData);
    User user = mock(User.class);
    given(user.getAuthenticationState()).willReturn(new AuthenticationState());
    given(user.getAuthenticationCredentials()).willReturn(new UsernamePasswordAuthenticationCredentials(username, ""));
    // When/Then
    assertThat(method.isAuthenticated(testMsg, user), is(true));
    assertThat(orderedReqUrls.size(), is(1));
    assertThat(orderedReqUrls.get(0), is(pollUrl + "?" + username));
    assertThat(orderedReqData.size(), is(1));
    assertThat(orderedReqData.get(0), is(pollData.replace(PostBasedAuthenticationMethod.MSG_USER_PATTERN, username)));
}
Also used : User(org.zaproxy.zap.users.User) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) IHTTPSession(fi.iki.elonen.NanoHTTPD.IHTTPSession) IOException(java.io.IOException) AuthenticationState(org.zaproxy.zap.users.AuthenticationState) Response(fi.iki.elonen.NanoHTTPD.Response) NanoHTTPD.newFixedLengthResponse(fi.iki.elonen.NanoHTTPD.newFixedLengthResponse) NanoServerHandler(org.zaproxy.zap.testutils.NanoServerHandler) HttpMessage(org.parosproxy.paros.network.HttpMessage) Test(org.junit.jupiter.api.Test) WithConfigsTest(org.zaproxy.zap.WithConfigsTest)

Example 25 with AuthenticationState

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

the class AuthenticationMethodPollUrlUnitTest method shouldPollOnSpecifiedNumberOfRequests.

@Test
void shouldPollOnSpecifiedNumberOfRequests() 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 user = mock(User.class);
    given(user.getAuthenticationState()).willReturn(new AuthenticationState());
    // When/Then
    assertThat(method.isAuthenticated(testMsg, user), is(true));
    assertThat(orderedReqs.size(), is(1));
    assertThat(method.isAuthenticated(testMsg, user), is(true));
    assertThat(method.isAuthenticated(testMsg, user), is(true));
    assertThat(method.isAuthenticated(testMsg, user), is(true));
    assertThat(method.isAuthenticated(testMsg, user), is(true));
    assertThat(method.isAuthenticated(testMsg, user), is(true));
    assertThat(orderedReqs.size(), is(1));
    assertThat(method.isAuthenticated(testMsg, user), is(true));
    assertThat(orderedReqs.size(), is(2));
    assertThat(orderedReqs.get(0), is(pollUrl));
    assertThat(orderedReqs.get(1), is(pollUrl));
}
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)

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