Newer
Older
ups-api / src / main / java / io / ljunggren / ups / api / service / OauthService.java
package io.ljunggren.ups.api.service;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;

import io.ljunggren.json.utils.JsonUtils;
import io.ljunggren.ups.api.model.AuthToken;
import io.ljunggren.ups.api.model.Debug;

public class OauthService extends BaseService {

    public OauthService(String domain) {
        super(domain);
    }
    private final String URI = domain + "/security/v1/oauth/token";

    public AuthToken getAuthToken(String clientId, String clientSecret, String accountNumber) throws Exception {
        return getAuthToken(clientId, clientSecret, accountNumber, getHttpClient());
    }
    
    public AuthToken getAuthToken(String clientId, String clientSecret, String accountNumber, CloseableHttpClient httpClient) throws Exception {
        HttpPost post = new HttpPost(URI);
        Header[] headers = generateHeaders(clientId, clientSecret, accountNumber);
        try {
            post.setHeaders(headers);
            post.setEntity(generateAuthEntity());
            CloseableHttpResponse response = httpClient.execute(post);
            String json = getResult(response);
            return JsonUtils.jsonToObject(json, AuthToken.class);
        } finally {
            httpClient.close();
        }
    }
    
    public Debug debug(String clientId, String clientSecret, String accountNumber) throws Exception {
    	return debug(clientId, clientSecret, accountNumber, getHttpClient());
    }
    
    public Debug debug(String clientId, String clientSecret, String accountNumber, CloseableHttpClient httpClient) throws Exception {
        HttpPost post = new HttpPost(URI);
        post.setHeaders(generateHeaders(clientId, clientSecret, accountNumber));
        post.setEntity(generateAuthEntity());
    	return debug(post, httpClient);
    }
    
    private Header[] generateHeaders(String clientId, String clientSecret, String accountNumber) throws UnsupportedEncodingException {
        String base64String = Base64.getEncoder().encodeToString((clientId + ":" + clientSecret).getBytes("UTF-8"));
        return new Header[] { 
                new BasicHeader("x-merchant-id", accountNumber),
                new BasicHeader("content-type", "application/x-www-form-urlencoded"),
                new BasicHeader("authorization", "Basic " + base64String) 
        };
    }
    
    private StringEntity generateAuthEntity() throws UnsupportedEncodingException {
        List<BasicNameValuePair> urlParameters = Arrays.asList(
                new BasicNameValuePair("grant_type", "client_credentials")
        );
        return new UrlEncodedFormEntity(urlParameters);
    }
    
}