Search in sources :

Example 1 with Base64.encodeBase64String

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

the class RMWebServices method createCredentials.

/**
   * Generate a Credentials object from the information in the CredentialsInfo
   * object.
   * 
   * @param credentials
   *          the CredentialsInfo provided by the user.
   * @return
   */
private Credentials createCredentials(CredentialsInfo credentials) {
    Credentials ret = new Credentials();
    try {
        for (Map.Entry<String, String> entry : credentials.getTokens().entrySet()) {
            Text alias = new Text(entry.getKey());
            Token<TokenIdentifier> token = new Token<TokenIdentifier>();
            token.decodeFromUrlString(entry.getValue());
            ret.addToken(alias, token);
        }
        for (Map.Entry<String, String> entry : credentials.getSecrets().entrySet()) {
            Text alias = new Text(entry.getKey());
            Base64 decoder = new Base64(0, null, true);
            byte[] secret = decoder.decode(entry.getValue());
            ret.addSecretKey(alias, secret);
        }
    } catch (IOException ie) {
        throw new BadRequestException("Could not parse credentials data; exception message = " + ie.getMessage());
    }
    return ret;
}
Also used : TokenIdentifier(org.apache.hadoop.security.token.TokenIdentifier) RMDelegationTokenIdentifier(org.apache.hadoop.yarn.security.client.RMDelegationTokenIdentifier) Base64(org.apache.commons.codec.binary.Base64) BadRequestException(org.apache.hadoop.yarn.webapp.BadRequestException) Text(org.apache.hadoop.io.Text) InvalidToken(org.apache.hadoop.security.token.SecretManager.InvalidToken) Token(org.apache.hadoop.security.token.Token) DelegationToken(org.apache.hadoop.yarn.server.resourcemanager.webapp.dao.DelegationToken) IOException(java.io.IOException) Map(java.util.Map) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Credentials(org.apache.hadoop.security.Credentials)

Example 2 with Base64.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String 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.encodeBase64String

use of org.apache.commons.codec.binary.Base64.encodeBase64String in project azure-iot-sdk-java by Azure.

the class IotHubServiceSasTokenTest method constructor_good_case_flow_check.

// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_002: [The constructor shall create a target uri from the url encoded host name)]
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_003: [The constructor shall create a string to sign by concatenating the target uri and the expiry time string]
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_004: [The constructor shall create a key from the shared access key signing with HmacSHA256]
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_005: [The constructor shall compute the final signature by url encoding the signed key]
// Tests_SRS_SERVICE_SDK_JAVA_IOTHUBSERVICESASTOKEN_12_006: [The constructor shall concatenate the target uri, the signature, the expiry time and the key name using the format: "SharedAccessSignature sr=%s&sig=%s&se=%s&skn=%s"]
@Test
public void constructor_good_case_flow_check() throws Exception {
    // Arrange
    String cryptoProvider = "HmacSHA256";
    String charset = "UTF-8";
    String iotHubName = "b.c.d";
    String hostName = "HOSTNAME." + iotHubName;
    String sharedAccessKeyName = "ACCESSKEYNAME";
    String policyName = "SharedAccessKey";
    String sharedAccessKey = "1234567890abcdefghijklmnopqrstvwxyz=";
    String connectionString = "HostName=" + hostName + ";SharedAccessKeyName=" + sharedAccessKeyName + ";" + policyName + "=" + sharedAccessKey;
    String expectedToken = "SharedAccessSignature sr=hostname.b.c.d&sig=M%2FT5oCM8WWs%2B%2FMv7okAVmfrzVM%2FGUyA7EIp%2FfKo8BeQ%3D&se=1474065852&skn=ACCESSKEYNAME";
    IotHubConnectionString iotHubConnectionString = IotHubConnectionStringBuilder.createConnectionString(connectionString);
    // Assert
    new Expectations() {

        URLEncoder urlEncoder;

        Base64 base64;

        System system;

        SecretKeySpec secretKeySpec;

        Mac mac;

        {
            urlEncoder.encode(hostName.toLowerCase(), String.valueOf(StandardCharsets.UTF_8));
            system.currentTimeMillis();
            Base64.decodeBase64(sharedAccessKey.getBytes(charset));
            byte[] body = { 1 };
            secretKeySpec = new SecretKeySpec(body, cryptoProvider);
            mac.getInstance(cryptoProvider);
        }
    };
    // Act
    IotHubServiceSasToken iotHubServiceSasToken = new IotHubServiceSasToken(iotHubConnectionString);
}
Also used : Expectations(mockit.Expectations) Base64(org.apache.commons.codec.binary.Base64) IotHubServiceSasToken(com.microsoft.azure.sdk.iot.service.auth.IotHubServiceSasToken) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) IotHubConnectionString(com.microsoft.azure.sdk.iot.service.IotHubConnectionString) URLEncoder(java.net.URLEncoder) Mac(javax.crypto.Mac) Test(org.junit.Test)

Example 4 with Base64.encodeBase64String

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

the class DigestOfFile method digestAsBase64.

/**
     * @param input
     * @return hash (base64 encoded)
     * @throws Exception
     */
public synchronized String digestAsBase64(byte[] input) throws Exception {
    byte[] digest = digestAsByteArray(input);
    Base64 encoder = new Base64();
    String digestAsBase64 = new String(encoder.encode(digest), "ASCII");
    return digestAsBase64;
}
Also used : Base64(org.apache.commons.codec.binary.Base64)

Example 5 with Base64.encodeBase64String

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

the class AdempiereMonitorFilter method checkAuthorization.

//	doFilter
/**
	 * 	Check Authorization
	 *	@param authorization authorization
	 *	@return true if authenticated
	 */
private boolean checkAuthorization(String authorization) {
    if (authorization == null)
        return false;
    try {
        String userInfo = authorization.substring(6).trim();
        Base64 decoder = new Base64();
        String namePassword = new String(decoder.decode(userInfo.getBytes()));
        //	log.fine("checkAuthorization - Name:Password=" + namePassword);
        int index = namePassword.indexOf(':');
        String name = namePassword.substring(0, index);
        String password = namePassword.substring(index + 1);
        Login login = new Login(Env.getCtx());
        KeyNamePair[] rolesKNPairs = login.getRoles(name, password);
        if (rolesKNPairs == null || rolesKNPairs.length == 0)
            throw new AdempiereException("@UserPwdError@");
        for (KeyNamePair keyNamePair : rolesKNPairs) {
            if ("System Administrator".equals(keyNamePair.getName())) {
                log.info("Name=" + name);
                return true;
            }
        }
        log.warning("Not a Sys Admin = " + name);
        return false;
    } catch (Exception e) {
        log.log(Level.SEVERE, "check", e);
    }
    return false;
}
Also used : Base64(org.apache.commons.codec.binary.Base64) AdempiereException(org.adempiere.exceptions.AdempiereException) Login(org.compiere.util.Login) KeyNamePair(org.compiere.util.KeyNamePair) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) AdempiereException(org.adempiere.exceptions.AdempiereException)

Aggregations

Base64 (org.apache.commons.codec.binary.Base64)135 IOException (java.io.IOException)30 Test (org.junit.Test)29 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)12 InputStream (java.io.InputStream)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 HttpServletResponse (javax.servlet.http.HttpServletResponse)11 Base64.encodeBase64String (org.apache.commons.codec.binary.Base64.encodeBase64String)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 HashMap (java.util.HashMap)10 SecretKeySpec (javax.crypto.spec.SecretKeySpec)9 MessageDigest (java.security.MessageDigest)8 File (java.io.File)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)7 URL (java.net.URL)7 Mac (javax.crypto.Mac)7 ServletException (javax.servlet.ServletException)7 X509Certificate (java.security.cert.X509Certificate)6 FileNotFoundException (java.io.FileNotFoundException)5 Signature (java.security.Signature)5