Example usage for java.util Base64 getDecoder

List of usage examples for java.util Base64 getDecoder

Introduction

In this page you can find the example usage for java.util Base64 getDecoder.

Prototype

public static Decoder getDecoder() 

Source Link

Document

Returns a Decoder that decodes using the Basic type base64 encoding scheme.

Usage

From source file:org.adeptnet.atlassian.common.Common.java

public String getKrb5UserName(final HttpServletRequest request) {
    if (!krb5Enabled) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("!krb5Enabled");
        }//  www  . ja  v  a  2 s .c  o m
        return null;
    }
    final String _ticket = request.getHeader(Krb5.AUTHORIZATION);
    if (_ticket == null) {
        return null;
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("Found Kerberos Ticket");
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace(_ticket);
    }

    final String ticket = Krb5.extractTicket(_ticket);
    if (ticket == null) {
        return null;
    }

    final String serverName = Krb5.resolveServerName(request.getServerName());

    krb5Cfg.init(getFileName(request.getServletContext()));
    final String realm = String.format("@%s", krb5Cfg.getRealm());
    final String spn = String.format("HTTP/%s%s", serverName, realm);
    if (LOG.isDebugEnabled()) {
        LOG.debug(String.format("SPN: %s", spn));
    }
    final String username = new Krb5(krb5Cfg).isTicketValid(spn, Base64.getDecoder().decode(ticket));

    if (username == null || !username.endsWith(realm)) {
        LOG.error(String.format("Invalid username: %s", username));
        return null;
    }

    return username.split("@")[0];
}

From source file:org.springframework.security.crypto.scrypt.SCryptPasswordEncoder.java

private byte[] decodePart(String part) {
    return Base64.getDecoder().decode(Utf8.encode(part));
}

From source file:org.wso2.identity.scenarios.commons.util.IdentityScenarioUtil.java

/**
 * Decode base64 encoded String/*www . j ava2 s.  c  om*/
 *
 * @param encodedString string tobe Base64 decoded
 * @return Base64 decoded string
 */
public static String bese64Decode(String encodedString) {
    byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
    return new String(decodedBytes);
}

From source file:com.ibasco.agql.examples.base.BaseExample.java

/**
 * @see <a href="https://gist.github.com/bricef/2436364">https://gist.github.com/bricef/2436364</a>
 *//*from w  ww  .j a  v  a2 s  .  c o m*/
public static String decrypt(String cipherText) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
    SecretKeySpec key = new SecretKeySpec(worldsMostSecureUnhackableKey.getBytes("UTF-8"), "AES");
    cipher.init(Cipher.DECRYPT_MODE, key,
            new IvParameterSpec(worldsMostSecureUnhackableIvKey.getBytes("UTF-8")));
    byte[] cipherBytes = Base64.getDecoder().decode(cipherText);
    return new String(cipher.doFinal(cipherBytes), "UTF-8");
}

From source file:org.wso2.carbon.apimgt.authenticator.LoginTokenService.java

private String getUsernameFromJWT(String jwt) throws KeyManagementException {
    if (jwt != null && jwt.contains(".")) {
        String[] jwtParts = jwt.split("\\.");
        JWTTokenPayload jwtHeader = new Gson().fromJson(
                new String(Base64.getDecoder().decode(jwtParts[1]), StandardCharsets.UTF_8),
                JWTTokenPayload.class);

        //removing "@carbon.super" part explicitly (until IS side is fixed to drop it)
        String username = jwtHeader.getSub();
        username = username.replace("@carbon.super", "");
        return username;
    } else {/* w ww  . j a va 2  s.c  om*/
        log.error("JWT Parsing failed. Invalid JWT: " + jwt);
        throw new KeyManagementException("JWT Parsing failed. Invalid JWT.");
    }
}

From source file:com.github.horrorho.inflatabledonkey.cloud.escrow.EscrowOperationsRecover.java

static BlobA4 blobA4(String respBlob) {
    byte[] data = Base64.getDecoder().decode(respBlob);
    ByteBuffer buffer = ByteBuffer.wrap(data);
    return new BlobA4(buffer);
}

From source file:es.tid.keyserver.controllers.db.DataBaseTest.java

/**
 * Test of getPrivateForHash method, of class DataBase.
 *///w  ww . j  a  va  2  s .c  om
@Test
public void testGetPrivateForHash1() {
    System.out.println("getPrivateForHash1");
    Assume.assumeTrue(this.dbAvailable); // If the Redis DB is not available, skip the test.
    // Insert the test register inside Redis DB.
    String certHash = "TestHashKey1";
    String value = "MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQC+OF7Ecq9cUnAwS0nafyhek4fmScf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RknnRp8gXjkBEAbB4AgC8ntukdcx4KD6s+ZuP/M5pWKqfapfSIQv8SjHsuMFwEtyAJUkoGrdBocSJE8+ROepmGEFb9MuckXNiEnrhX6C0Y0mxqFaTccX0PiqdUeuVjNVlLGnBL/EQnGG5X2JW0UeW0B0LXD3sTlQ4GGd0Ph3i9YHuyodYjTRABtYLwnyFTZSi6CoZyV+3uqnmedEz9Q+6w6GoR+sK5kQNmrieG8CggEAQnO4B9lh6g4/J8zFvhlIWyGhqOpnZOYyIPHPyufhrwuwq625Ws2LBN1AJxXQ3Ixz7numFUqMfgROpCX7bs30jY9oy7SjYZMxTZBDlp9Rm5Szemp1th3vN0kUiuskm7KeGObHMr5Ou9j7Qxd9GpXmZylchUZIAXf74mD+9N8CXbCWsAZv50S4SB+xjUmuYC7JyyIFGcTCCtYKbYvJspc5nJiMyVHUJX5CK47q/Udc02UefjJgH9bTEnnPQ8TVbdIzGe6IYDUril1usehaJaJV2/AbbIe4iCW7HqMZapL8YDv0bTsWjIdvHtaJAPVlurT1aj4hNDvJjBVPil0uO3TduQKCAQEA0ffYzzIJQpPuEqyPF5pr9HdQnDywlcvss9+uMgFZX0efr/NHh9ai+WITTi9pgCD3LUP77QtiqzM/H++p4d1mObc3frZu2o4hLAPIsZrsYYerQl8YexmTVD8XN9RcJ6fgfvQ80DZukkvMQNRDg4pCZ4/X5bKajzVmv+QChwofp7FQuw3E8AVMU9kOl8koB6IYrRbWaVyYDsr1RPKTfQNHBp0y3uUOuRGDWbNmrWNywmqORXDoGeLu1PaCWDMMTuV2tCqvym0Y6BkFaoDxi0VUd/tKvP2hSEIPM8b0fTKp2P6QXDdpfaD/pzBFiddV82j0jPWgaTQgJ9+J3Ft1rYas/A==";
    this.dataBaseObj.set(certHash, value);
    // Test code.
    DataBase instance = new DataBase(dbAddress, dbPort, dbPassword, dbIndex);
    byte[] expResult = Base64.getDecoder().decode(value);
    byte[] result = instance.getPrivateForHash(certHash);
    // Remove test register from Redis DB.
    this.dataBaseObj.del(certHash);
    assertArrayEquals(expResult, result);
}

From source file:org.apache.nifi.registry.web.api.SecureKerberosIT.java

@Test
public void testTokenGenerationAndAccessStatus() throws Exception {

    // Note: this test intentionally does not use the token generated
    // for nifiadmin by the @Before method

    // Given: the client and server have been configured correctly for Kerberos SPNEGO authentication
    String expectedJwtPayloadJson = "{" + "\"sub\":\"kerberosUser@LOCALHOST\","
            + "\"preferred_username\":\"kerberosUser@LOCALHOST\","
            + "\"iss\":\"KerberosSpnegoIdentityProvider\"" + "}";
    String expectedAccessStatusJson = "{" + "\"identity\":\"kerberosUser@LOCALHOST\"," + "\"anonymous\":false}";

    // When: the /access/token/kerberos endpoint is accessed with no credentials
    final Response tokenResponse1 = client.target(createURL("/access/token/kerberos")).request().post(null,
            Response.class);

    // Then: the server returns 401 Unauthorized with an authenticate challenge header
    assertEquals(401, tokenResponse1.getStatus());
    assertNotNull(tokenResponse1.getHeaders().get("www-authenticate"));
    assertEquals(1, tokenResponse1.getHeaders().get("www-authenticate").size());
    assertEquals("Negotiate", tokenResponse1.getHeaders().get("www-authenticate").get(0));

    // When: the /access/token/kerberos endpoint is accessed again with an invalid ticket
    String invalidTicket = new String(
            java.util.Base64.getEncoder().encode(invalidKerberosTicket.getBytes(Charset.forName("UTF-8"))));
    final Response tokenResponse2 = client.target(createURL("/access/token/kerberos")).request()
            .header("Authorization", "Negotiate " + invalidTicket).post(null, Response.class);

    // Then: the server returns 401 Unauthorized
    assertEquals(401, tokenResponse2.getStatus());

    // When: the /access/token/kerberos endpoint is accessed with a valid ticket
    String validTicket = new String(
            Base64.getEncoder().encode(validKerberosTicket.getBytes(Charset.forName("UTF-8"))));
    final Response tokenResponse3 = client.target(createURL("/access/token/kerberos")).request()
            .header("Authorization", "Negotiate " + validTicket).post(null, Response.class);

    // Then: the server returns 200 OK with a JWT in the body
    assertEquals(201, tokenResponse3.getStatus());
    String token = tokenResponse3.readEntity(String.class);
    assertTrue(StringUtils.isNotEmpty(token));
    String[] jwtParts = token.split("\\.");
    assertEquals(3, jwtParts.length);/*from w w  w. j a v a 2  s  .co m*/
    String jwtPayload = new String(Base64.getDecoder().decode(jwtParts[1]), "UTF-8");
    JSONAssert.assertEquals(expectedJwtPayloadJson, jwtPayload, false);

    // When: the token is returned in the Authorization header
    final Response accessResponse = client.target(createURL("access")).request()
            .header("Authorization", "Bearer " + token).get(Response.class);

    // Then: the server acknowledges the client has access
    assertEquals(200, accessResponse.getStatus());
    String accessStatus = accessResponse.readEntity(String.class);
    JSONAssert.assertEquals(expectedAccessStatusJson, accessStatus, false);

}

From source file:software.coolstuff.springframework.owncloud.service.impl.rest.AbstractOwncloudRestServiceImpl.java

protected String getAuthorizationUserFromHeaders(HttpHeaders headers) {
    Validate.notNull(headers);//from  w ww. j a  va  2s .c o m

    List<String> authorizations = headers.get(HttpHeaders.AUTHORIZATION);
    if (CollectionUtils.isEmpty(authorizations)) {
        return null;
    }

    String encodedCredentials = authorizations.get(0);
    if (StringUtils.startsWith(encodedCredentials, AUTHORIZATION_METHOD_PREFIX)) {
        encodedCredentials = StringUtils.substring(encodedCredentials, AUTHORIZATION_METHOD_PREFIX.length());
    }
    final byte[] rawDecodedCredentials = Base64.getDecoder().decode(encodedCredentials.getBytes());
    final String decodedCredentials = new String(rawDecodedCredentials);
    if (!StringUtils.contains(decodedCredentials, ':')) {
        return null;
    }
    return StringUtils.split(decodedCredentials, ':')[0];
}