Search in sources :

Example 1 with Base64

use of org.apache.commons.codec.binary.Base64 in project camel by apache.

the class SigningProcessor method process.

public void process(Exchange exchange) throws Exception {
    Signature service = initSignatureService(exchange);
    calculateSignature(exchange, service);
    byte[] signature = service.sign();
    Message in = exchange.getIn();
    clearMessageHeaders(in);
    Message out = exchange.getOut();
    out.copyFrom(in);
    out.setHeader(config.getSignatureHeaderName(), new Base64().encode(signature));
}
Also used : Base64(org.apache.commons.codec.binary.Base64) Message(org.apache.camel.Message) Signature(java.security.Signature)

Example 2 with Base64

use of org.apache.commons.codec.binary.Base64 in project hadoop by apache.

the class Token method encodeWritable.

/**
   * Generate a string with the url-quoted base64 encoded serialized form
   * of the Writable.
   * @param obj the object to serialize
   * @return the encoded string
   * @throws IOException
   */
private static String encodeWritable(Writable obj) throws IOException {
    DataOutputBuffer buf = new DataOutputBuffer();
    obj.write(buf);
    Base64 encoder = new Base64(0, null, true);
    byte[] raw = new byte[buf.getLength()];
    System.arraycopy(buf.getData(), 0, raw, 0, buf.getLength());
    return encoder.encodeToString(raw);
}
Also used : Base64(org.apache.commons.codec.binary.Base64)

Example 3 with Base64

use of org.apache.commons.codec.binary.Base64 in project hadoop by apache.

the class LdapAuthenticationHandler method authenticate.

@Override
public AuthenticationToken authenticate(HttpServletRequest request, HttpServletResponse response) throws IOException, AuthenticationException {
    AuthenticationToken token = null;
    String authorization = request.getHeader(HttpConstants.AUTHORIZATION_HEADER);
    if (authorization == null || !AuthenticationHandlerUtil.matchAuthScheme(HttpConstants.BASIC, authorization)) {
        response.setHeader(WWW_AUTHENTICATE, HttpConstants.BASIC);
        response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        if (authorization == null) {
            logger.trace("Basic auth starting");
        } else {
            logger.warn("'" + HttpConstants.AUTHORIZATION_HEADER + "' does not start with '" + HttpConstants.BASIC + "' :  {}", authorization);
        }
    } else {
        authorization = authorization.substring(HttpConstants.BASIC.length()).trim();
        final Base64 base64 = new Base64(0);
        // As per RFC7617, UTF-8 charset should be used for decoding.
        String[] credentials = new String(base64.decode(authorization), StandardCharsets.UTF_8).split(":", 2);
        if (credentials.length == 2) {
            token = authenticateUser(credentials[0], credentials[1]);
            response.setStatus(HttpServletResponse.SC_OK);
        }
    }
    return token;
}
Also used : Base64(org.apache.commons.codec.binary.Base64)

Example 4 with Base64

use of org.apache.commons.codec.binary.Base64 in project hadoop by apache.

the class TestLdapAuthenticationHandler method testRequestWithAuthorization.

@Test(timeout = 60000)
public void testRequestWithAuthorization() throws Exception {
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
    final Base64 base64 = new Base64(0);
    String credentials = base64.encodeToString("bjones:p@ssw0rd".getBytes());
    String authHeader = HttpConstants.BASIC + " " + credentials;
    Mockito.when(request.getHeader(HttpConstants.AUTHORIZATION_HEADER)).thenReturn(authHeader);
    AuthenticationToken token = handler.authenticate(request, response);
    Assert.assertNotNull(token);
    Mockito.verify(response).setStatus(HttpServletResponse.SC_OK);
    Assert.assertEquals(TYPE, token.getType());
    Assert.assertEquals("bjones", token.getUserName());
    Assert.assertEquals("bjones", token.getName());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Base64(org.apache.commons.codec.binary.Base64) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.Test)

Example 5 with Base64

use of org.apache.commons.codec.binary.Base64 in project hadoop by apache.

the class TestLdapAuthenticationHandler method testRequestWithWrongCredentials.

@Test(timeout = 60000)
public void testRequestWithWrongCredentials() throws Exception {
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
    final Base64 base64 = new Base64(0);
    String credentials = base64.encodeToString("bjones:foo123".getBytes());
    String authHeader = HttpConstants.BASIC + " " + credentials;
    Mockito.when(request.getHeader(HttpConstants.AUTHORIZATION_HEADER)).thenReturn(authHeader);
    try {
        handler.authenticate(request, response);
        Assert.fail();
    } catch (AuthenticationException ex) {
    // Expected
    } catch (Exception ex) {
        Assert.fail();
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Base64(org.apache.commons.codec.binary.Base64) AuthenticationException(org.apache.hadoop.security.authentication.client.AuthenticationException) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthenticationException(org.apache.hadoop.security.authentication.client.AuthenticationException) Test(org.junit.Test)

Aggregations

Base64 (org.apache.commons.codec.binary.Base64)50 IOException (java.io.IOException)13 Test (org.junit.Test)9 HttpServletRequest (javax.servlet.http.HttpServletRequest)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)8 InputStream (java.io.InputStream)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)6 Entry (org.apache.directory.shared.ldap.ldif.Entry)5 AuthenticationException (org.apache.hadoop.security.authentication.client.AuthenticationException)5 File (java.io.File)4 ServletException (javax.servlet.ServletException)4 GeneralException (com.microsoft.azure.oidc.exception.GeneralException)3 PreconditionException (com.microsoft.azure.oidc.exception.PreconditionException)3 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 URL (java.net.URL)3 MessageDigest (java.security.MessageDigest)3 CertificateException (java.security.cert.CertificateException)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2