Example usage for org.apache.commons.ssl Base64 decodeBase64

List of usage examples for org.apache.commons.ssl Base64 decodeBase64

Introduction

In this page you can find the example usage for org.apache.commons.ssl Base64 decodeBase64.

Prototype

public static byte[] decodeBase64(byte[] base64Data) 

Source Link

Document

Decodes Base64 data into octets

Usage

From source file:nl.surfnet.coin.selfservice.service.impl.ssl.KeyStore.java

/**
 * Add a private key (plus its certificate chain) to the given key store.
 * /*  w  w w.  j  a va 2 s. co m*/
 * @param alias
 *          alias of the key
 * @param privateKey
 *          the private key in Base64 encoded BER format.
 * @param certificate
 *          the certificate in PEM format, without ---BEGIN CER.... wrapper
 * @param password
 *          password to protect key with
 */
private void addPrivateKey(String alias, String privateKey, String certificate, String password) {
    String wrappedCert = "-----BEGIN CERTIFICATE-----\n" + certificate + "\n-----END CERTIFICATE-----";
    byte[] decodedKey = Base64.decodeBase64(privateKey.getBytes());

    try {
        KeyStoreUtil.appendKeyToKeyStore(keyStore, alias, new ByteArrayInputStream(wrappedCert.getBytes()),
                new ByteArrayInputStream(decodedKey), password.toCharArray());
        passwords.put(alias, password);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:nl.surfnet.spring.security.opensaml.KeyStore.java

/**
 * Add a private key (plus its certificate chain) to the given key store.
 * @param alias alias of the key/*from  ww  w.j av a2s .c  o m*/
 * @param privateKey the private key in Base64 encoded BER format.
 * @param certificate the certificate in PEM format, without ---BEGIN CER.... wrapper
 * @param password password to protect key with
 */
public void addPrivateKey(String alias, String privateKey, String certificate, String password) {
    String wrappedCert = "-----BEGIN CERTIFICATE-----\n" + certificate + "\n-----END CERTIFICATE-----";
    byte[] decodedKey = Base64.decodeBase64(privateKey.getBytes());

    try {
        KeyStoreUtil.appendKeyToKeyStore(keyStore, alias, new ByteArrayInputStream(wrappedCert.getBytes()),
                new ByteArrayInputStream(decodedKey), password.toCharArray());
        passwords.put(alias, password);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:nl.surfnet.spring.security.opensaml.util.KeyStoreUtilTest.java

@Test
public void testAppendKeyToKeyStore() throws Exception {
    String wrappedCert = "-----BEGIN CERTIFICATE-----\n" + CERT + "\n-----END CERTIFICATE-----";
    byte[] wrappedKey = Base64.decodeBase64(KEY.getBytes());

    KeyStoreUtil.appendKeyToKeyStore(keyStore, "alias", new ByteArrayInputStream(wrappedCert.getBytes()),
            new ByteArrayInputStream(wrappedKey), "pass".toCharArray());
    try {//from   www.  j  av  a 2 s.  co  m
        keyStore.getKey("alias", "wrongpass".toCharArray());
        fail("Should throw exception when requesting key with wrong password");
    } catch (UnrecoverableKeyException e) {
        // success
    }

    assertNotNull(keyStore.getKey("alias", "pass".toCharArray()));
}

From source file:nl.surfnet.spring.security.opensaml.util.KeyStoreUtilTest.java

@Test
public void testAppendKeyToKeyStoreWithPassword() throws Exception {
    String wrappedCert = "-----BEGIN CERTIFICATE-----\n" + CERT + "\n-----END CERTIFICATE-----";
    byte[] wrappedKey = Base64.decodeBase64(KEY.getBytes());

    KeyStoreUtil.appendKeyToKeyStore(keyStore, "alias", new ByteArrayInputStream(wrappedCert.getBytes()),
            new ByteArrayInputStream(wrappedKey), "".toCharArray());
}

From source file:org.wso2.carbon.appmgt.services.api.v1.apps.mobile.MobileAppService.java

private Registry doAuthorizeAndGetRegistry(String tenantDomain, HttpHeaders headers)
        throws UnauthorizedUserException, UserStoreException {
    List<String> authorization = headers.getRequestHeader("Authorization");
    if (authorization != null && authorization.size() != 0) {
        String basicHeader = authorization.get(0);
        String base64Credentials = basicHeader.substring("Basic".length()).trim();
        String credentialsString = new String(Base64.decodeBase64(base64Credentials.getBytes()));
        final String[] credentials = credentialsString.split(":", 2);
        if (credentials.length < 2) {
            throw new UnauthorizedUserException();
        }/*  w ww. ja  va  2s  .c  o m*/

        RealmService realmService = (RealmService) PrivilegedCarbonContext.getThreadLocalCarbonContext()
                .getOSGiService(RealmService.class);
        RegistryService registryService = (RegistryService) PrivilegedCarbonContext
                .getThreadLocalCarbonContext().getOSGiService(RegistryService.class);
        UserStoreManager userStoreManager = (UserStoreManager) realmService
                .getTenantUserRealm(SUPER_USER_TENANT_ID).getUserStoreManager();

        String[] userList = userStoreManager.getRoleListOfUser(credentials[0]);
        String authorizedRole = ServicesApiConfigurations.getInstance().getAuthorizedRole();
        if (!Arrays.asList(userList).contains(authorizedRole)) {
            throw new UnauthorizedUserException();
        }

        boolean isAuthenticated = userStoreManager
                .authenticate(MultitenantUtils.getTenantAwareUsername(credentials[0]), credentials[1]);

        if (!isAuthenticated) {
            throw new UnauthorizedUserException();
        }
    } else {
        throw new UnauthorizedUserException();
    }

    PrivilegedCarbonContext.startTenantFlow();
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain);
    PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(PrivilegedCarbonContext
            .getThreadLocalCarbonContext().getUserRealm().getRealmConfiguration().getAdminUserName());

    return CarbonContext.getThreadLocalCarbonContext().getRegistry(RegistryType.USER_GOVERNANCE);
}