Search in sources :

Example 51 with Credentials

use of org.apache.commons.httpclient.Credentials in project sling by apache.

the class RemoveAcesTest method createFolderWithAces.

private String createFolderWithAces(boolean addGroupAce) throws IOException, JsonException {
    testUserId = createTestUser();
    testFolderUrl = createTestFolder();
    String postUrl = testFolderUrl + ".modifyAce.html";
    List<NameValuePair> postParams = new ArrayList<NameValuePair>();
    postParams.add(new NameValuePair("principalId", testUserId));
    postParams.add(new NameValuePair("privilege@jcr:read", "granted"));
    postParams.add(new NameValuePair("privilege@jcr:write", "denied"));
    Credentials creds = new UsernamePasswordCredentials("admin", "admin");
    assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    if (addGroupAce) {
        testGroupId = createTestGroup();
        postParams = new ArrayList<NameValuePair>();
        postParams.add(new NameValuePair("principalId", testGroupId));
        postParams.add(new NameValuePair("privilege@jcr:read", "granted"));
        assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    }
    //fetch the JSON for the acl to verify the settings.
    String getUrl = testFolderUrl + ".acl.json";
    String json = getAuthenticatedContent(creds, getUrl, CONTENT_TYPE_JSON, null, HttpServletResponse.SC_OK);
    assertNotNull(json);
    JsonObject jsonObject = JsonUtil.parseObject(json);
    if (addGroupAce) {
        assertEquals(2, jsonObject.size());
    } else {
        assertEquals(1, jsonObject.size());
    }
    JsonObject aceObject = jsonObject.getJsonObject(testUserId);
    assertNotNull(aceObject);
    assertEquals(0, aceObject.getInt("order"));
    String principalString = aceObject.getString("principal");
    assertEquals(testUserId, principalString);
    JsonArray grantedArray = aceObject.getJsonArray("granted");
    assertNotNull(grantedArray);
    assertEquals("jcr:read", grantedArray.getString(0));
    JsonArray deniedArray = aceObject.getJsonArray("denied");
    assertNotNull(deniedArray);
    assertEquals("jcr:write", deniedArray.getString(0));
    if (addGroupAce) {
        aceObject = jsonObject.getJsonObject(testGroupId);
        assertNotNull(aceObject);
        principalString = aceObject.getString("principal");
        assertEquals(testGroupId, principalString);
        assertEquals(1, aceObject.getInt("order"));
        grantedArray = aceObject.getJsonArray("granted");
        assertNotNull(grantedArray);
        assertEquals("jcr:read", grantedArray.getString(0));
    }
    return testFolderUrl;
}
Also used : JsonArray(javax.json.JsonArray) NameValuePair(org.apache.commons.httpclient.NameValuePair) ArrayList(java.util.ArrayList) JsonObject(javax.json.JsonObject) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 52 with Credentials

use of org.apache.commons.httpclient.Credentials in project sling by apache.

the class RemoveAcesTest method tearDown.

@Override
public void tearDown() throws Exception {
    super.tearDown();
    Credentials creds = new UsernamePasswordCredentials("admin", "admin");
    if (testFolderUrl != null) {
        //remove the test user if it exists.
        String postUrl = testFolderUrl;
        List<NameValuePair> postParams = new ArrayList<NameValuePair>();
        postParams.add(new NameValuePair(":operation", "delete"));
        assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    }
    if (testGroupId != null) {
        //remove the test user if it exists.
        String postUrl = HTTP_BASE_URL + "/system/userManager/group/" + testGroupId + ".delete.html";
        List<NameValuePair> postParams = new ArrayList<NameValuePair>();
        assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    }
    if (testUserId != null) {
        //remove the test user if it exists.
        String postUrl = HTTP_BASE_URL + "/system/userManager/user/" + testUserId + ".delete.html";
        List<NameValuePair> postParams = new ArrayList<NameValuePair>();
        assertAuthenticatedPostStatus(creds, postUrl, HttpServletResponse.SC_OK, postParams, null);
    }
//todo delete test folder
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) ArrayList(java.util.ArrayList) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 53 with Credentials

use of org.apache.commons.httpclient.Credentials in project sling by apache.

the class AuthenticationResponseCodeTest method testPreventLoopIncorrectHttpBasicCredentials.

@Test
public void testPreventLoopIncorrectHttpBasicCredentials() throws Exception {
    // assume http and webdav are on the same host + port
    URL url = new URL(HttpTest.HTTP_BASE_URL);
    Credentials defaultcreds = new UsernamePasswordCredentials("garbage", "garbage");
    H.getHttpClient().getState().setCredentials(new AuthScope(url.getHost(), url.getPort(), AuthScope.ANY_REALM), defaultcreds);
    final String requestUrl = HttpTest.HTTP_BASE_URL + "/junk?param1=1";
    HttpMethod get = new GetMethod(requestUrl);
    get.setRequestHeader("Referer", requestUrl);
    get.setRequestHeader("User-Agent", "Mozilla/5.0 Sling Integration Test");
    int status = H.getHttpClient().executeMethod(get);
    assertEquals(HttpServletResponse.SC_UNAUTHORIZED, status);
}
Also used : AuthScope(org.apache.commons.httpclient.auth.AuthScope) GetMethod(org.apache.commons.httpclient.methods.GetMethod) URL(java.net.URL) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) HttpMethod(org.apache.commons.httpclient.HttpMethod) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) HttpTest(org.apache.sling.commons.testing.integration.HttpTest) Test(org.junit.Test)

Example 54 with Credentials

use of org.apache.commons.httpclient.Credentials in project sling by apache.

the class ExternalSlingLaunchpad method before.

@Override
protected void before() throws Throwable {
    Credentials creds = new UsernamePasswordCredentials(config.getUsername(), config.getPassword());
    HttpClient client = new HttpClient();
    client.getState().setCredentials(new AuthScope(config.getHostname(), config.getPort()), creds);
    long cutoff = System.currentTimeMillis() + MAX_WAIT_TIME_MS;
    List<SlingReadyRule> rules = new ArrayList<>();
    rules.add(new StartLevelSlingReadyRule(client));
    rules.add(new ActiveBundlesSlingReadyRule(client));
    for (SlingReadyRule rule : rules) {
        while (true) {
            if (rule.evaluate()) {
                break;
            }
            assertTimeout(cutoff);
            Thread.sleep(100);
        }
    }
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) AuthScope(org.apache.commons.httpclient.auth.AuthScope) ArrayList(java.util.ArrayList) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 55 with Credentials

use of org.apache.commons.httpclient.Credentials in project sling by apache.

the class GetNodeCommand method execute.

@Override
public Result<byte[]> execute() {
    GetMethod get = new GetMethod(getPath());
    try {
        httpClient.getParams().setAuthenticationPreemptive(true);
        Credentials defaultcreds = new UsernamePasswordCredentials(repositoryInfo.getUsername(), repositoryInfo.getPassword());
        //TODO
        httpClient.getState().setCredentials(new AuthScope(repositoryInfo.getHost(), repositoryInfo.getPort(), AuthScope.ANY_REALM), defaultcreds);
        int responseStatus = httpClient.executeMethod(get);
        if (isSuccessStatus(responseStatus))
            return AbstractResult.success(get.getResponseBody());
        return failureResultForStatusCode(responseStatus);
    } catch (Exception e) {
        return AbstractResult.failure(new RepositoryException(e));
    } finally {
        get.releaseConnection();
    }
}
Also used : GetMethod(org.apache.commons.httpclient.methods.GetMethod) AuthScope(org.apache.commons.httpclient.auth.AuthScope) RepositoryException(org.apache.sling.ide.transport.RepositoryException) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) RepositoryException(org.apache.sling.ide.transport.RepositoryException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Aggregations

Credentials (org.apache.commons.httpclient.Credentials)102 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)102 ArrayList (java.util.ArrayList)64 NameValuePair (org.apache.commons.httpclient.NameValuePair)64 JsonObject (javax.json.JsonObject)52 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)51 Test (org.junit.Test)51 JsonArray (javax.json.JsonArray)19 AuthScope (org.apache.commons.httpclient.auth.AuthScope)17 HashSet (java.util.HashSet)14 GetMethod (org.apache.commons.httpclient.methods.GetMethod)12 HttpClient (org.apache.commons.httpclient.HttpClient)11 URL (java.net.URL)9 HttpMethod (org.apache.commons.httpclient.HttpMethod)5 IOException (java.io.IOException)4 HttpState (org.apache.commons.httpclient.HttpState)4 After (org.junit.After)4 InputStream (java.io.InputStream)3 Header (org.apache.commons.httpclient.Header)3 HttpException (org.apache.commons.httpclient.HttpException)3