Java Code Examples for org.apache.axiom.util.base64.Base64Utils#encode()

The following examples show how to use org.apache.axiom.util.base64.Base64Utils#encode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MicroIntegratorBaseUtils.java    From micro-integrator with Apache License 2.0 6 votes vote down vote up
/**
 * This is a utility method which can be used to set security headers in a service client. This method
 * will create authorization header according to basic security protocol. i.e. encodeBase64(username:password)
 * and put it in a HTTP header with name "Authorization".
 *
 * @param userName      User calling the service.
 * @param password      Password of the user.
 * @param rememberMe    <code>true</code> if UI asks to persist remember me cookie.
 * @param serviceClient The service client used in the communication.
 */
public static void setBasicAccessSecurityHeaders(String userName, String password, boolean rememberMe,
                                                 ServiceClient serviceClient) {

    String userNamePassword = userName + ":" + password;
    String encodedString = Base64Utils.encode(userNamePassword.getBytes());

    String authorizationHeader = "Basic " + encodedString;

    List<Header> headers = new ArrayList<Header>();

    Header authHeader = new Header("Authorization", authorizationHeader);
    headers.add(authHeader);

    if (rememberMe) {
        Header rememberMeHeader = new Header("RememberMe", TRUE);
        headers.add(rememberMeHeader);
    }

    serviceClient.getOptions().setProperty(HTTPConstants.HTTP_HEADERS, headers);
}
 
Example 2
Source File: IdentityApplicationManagementUtil.java    From carbon-identity with Apache License 2.0 6 votes vote down vote up
/**
 * @param key
 * @param value
 * @return
 * @throws SignatureException
 */
public static String calculateHmacSha1(String key, String value) throws SignatureException {
    String result;
    try {
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);
        byte[] rawHmac = mac.doFinal(value.getBytes());
        result = Base64Utils.encode(rawHmac);
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.debug("Failed to create the HMAC Signature", e);
        }
        throw new SignatureException("Failed to calculate HMAC : " + e.getMessage());
    }
    return result;
}
 
Example 3
Source File: IdentityApplicationManagementUtil.java    From carbon-identity-framework with Apache License 2.0 4 votes vote down vote up
/**
 * @param jsonObj
 * @return Base64 encoded JWT
 */
public static String getSignedJWT(String jsonObj, ServiceProvider serviceProvider) {

    String oauthConsumerSecret = null;

    if (serviceProvider.getInboundAuthenticationConfig() != null
            && serviceProvider.getInboundAuthenticationConfig()
            .getInboundAuthenticationRequestConfigs() != null
            && serviceProvider.getInboundAuthenticationConfig()
            .getInboundAuthenticationRequestConfigs().length > 0) {

        InboundAuthenticationRequestConfig[] authReqConfigs = serviceProvider
                .getInboundAuthenticationConfig().getInboundAuthenticationRequestConfigs();

        for (InboundAuthenticationRequestConfig authReqConfig : authReqConfigs) {
            if ((IdentityApplicationConstants.OAuth2.NAME).equals(authReqConfig.getInboundAuthType())) {
                if (authReqConfig.getProperties() != null) {
                    for (Property property : authReqConfig.getProperties()) {
                        if ((IdentityApplicationConstants.OAuth2.OAUTH_CONSUMER_SECRET)
                                .equalsIgnoreCase(property.getName())) {
                            oauthConsumerSecret = property.getValue();
                            break;
                        }
                    }
                }
            }
        }

    }

    String jwtBody = "{\"iss\":\"wso2\",\"exp\":" + new Date().getTime() + 3000 + ",\"iat\":"
            + new Date().getTime() + "," + jsonObj + "}";
    String jwtHeader = "{\"typ\":\"JWT\", \"alg\":\"HS256\"}";

    if (oauthConsumerSecret == null) {
        jwtHeader = "{\"typ\":\"JWT\", \"alg\":\"none\"}";
    }

    String base64EncodedHeader = Base64Utils.encode(jwtHeader.getBytes(StandardCharsets.UTF_8));
    String base64EncodedBody = Base64Utils.encode(jwtBody.getBytes(StandardCharsets.UTF_8));

    if (log.isDebugEnabled()) {
        log.debug("JWT Header :" + jwtHeader);
        log.debug("JWT Body :" + jwtBody);
    }

    String assertion = base64EncodedHeader + "." + base64EncodedBody;

    if (oauthConsumerSecret == null) {
        return assertion + ".";
    } else {
        String signedAssertion;
        try {
            signedAssertion = calculateHmacSha1(oauthConsumerSecret, assertion);
            return assertion + "." + signedAssertion;
        } catch (SignatureException e) {
            log.error("Error while signing the assertion", e);
            return assertion + ".";
        }
    }
}
 
Example 4
Source File: IdentityApplicationManagementUtil.java    From carbon-identity with Apache License 2.0 4 votes vote down vote up
/**
 * @param jsonObj
 * @return Base64 encoded JWT
 */
public static String getSignedJWT(String jsonObj, ServiceProvider serviceProvider) {

    String oauthConsumerSecret = null;

    if (serviceProvider.getInboundAuthenticationConfig() != null
            && serviceProvider.getInboundAuthenticationConfig()
            .getInboundAuthenticationRequestConfigs() != null
            && serviceProvider.getInboundAuthenticationConfig()
            .getInboundAuthenticationRequestConfigs().length > 0) {

        InboundAuthenticationRequestConfig[] authReqConfigs = serviceProvider
                .getInboundAuthenticationConfig().getInboundAuthenticationRequestConfigs();

        for (InboundAuthenticationRequestConfig authReqConfig : authReqConfigs) {
            if ((IdentityApplicationConstants.OAuth2.NAME).equals(authReqConfig.getInboundAuthType())) {
                if (authReqConfig.getProperties() != null) {
                    for (Property property : authReqConfig.getProperties()) {
                        if ((IdentityApplicationConstants.OAuth2.OAUTH_CONSUMER_SECRET)
                                .equalsIgnoreCase(property.getName())) {
                            oauthConsumerSecret = property.getValue();
                            break;
                        }
                    }
                }
            }
        }

    }

    String jwtBody = "{\"iss\":\"wso2\",\"exp\":" + new Date().getTime() + 3000 + ",\"iat\":"
            + new Date().getTime() + "," + jsonObj + "}";
    String jwtHeader = "{\"typ\":\"JWT\", \"alg\":\"HS256\"}";

    if (oauthConsumerSecret == null) {
        jwtHeader = "{\"typ\":\"JWT\", \"alg\":\"none\"}";
    }

    String base64EncodedHeader = Base64Utils.encode(jwtHeader.getBytes());
    String base64EncodedBody = Base64Utils.encode(jwtBody.getBytes());

    if (log.isDebugEnabled()) {
        log.debug("JWT Header :" + jwtHeader);
        log.debug("JWT Body :" + jwtBody);
    }

    String assertion = base64EncodedHeader + "." + base64EncodedBody;

    if (oauthConsumerSecret == null) {
        return assertion + ".";
    } else {
        String signedAssertion;
        try {
            signedAssertion = calculateHmacSha1(oauthConsumerSecret, assertion);
            return assertion + "." + signedAssertion;
        } catch (SignatureException e) {
            log.error("Error while siging the assertion", e);
            return assertion + ".";
        }
    }
}