org.wso2.carbon.identity.oauth2.client.authentication.ClientAuthUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.wso2.carbon.identity.oauth2.client.authentication.ClientAuthUtil.java

Source

/*
 * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.wso2.carbon.identity.oauth2.client.authentication;

import org.apache.axiom.util.base64.Base64Utils;
import org.apache.commons.lang.StringUtils;
import org.apache.oltu.oauth2.common.OAuth;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClientAuthUtil {

    protected String test = "test";

    public static Map<String, List<String>> getBodyContentWithClientAndSecret(String clientId, String secret) {

        Map<String, String> content = new HashMap<>();
        if (StringUtils.isNotEmpty(clientId)) {
            content.put(OAuth.OAUTH_CLIENT_ID, clientId);
        }
        if (StringUtils.isNotEmpty(secret)) {
            content.put(OAuth.OAUTH_CLIENT_SECRET, secret);
        }
        return getBodyContent(content);
    }

    public static Map<String, List<String>> getBodyContent(Map<String, String> contentStrings) {

        Map<String, List<String>> bodyContent = new HashMap<>();
        contentStrings.forEach((key, value) -> {
            List<String> valueList = new ArrayList<String>();
            valueList.add(value);
            bodyContent.put(key, valueList);
        });
        return bodyContent;
    }

    public static String getBase64EncodedBasicAuthHeader(String clientId, String secret, String extraParam) {

        if (StringUtils.isEmpty(clientId)) {
            return "Basic " + Base64Utils.encode((secret).getBytes());
        } else if (StringUtils.isEmpty(secret)) {
            return "Basic " + Base64Utils.encode((clientId).getBytes());
        }
        if (StringUtils.isNotEmpty(extraParam)) {
            return "Basic " + Base64Utils.encode((clientId + ":" + secret + ":" + extraParam).getBytes());
        }
        return "Basic " + Base64Utils.encode((clientId + ":" + secret).getBytes());
    }
}