All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mastercard.developer.signers.SpringHttpRequestSigner Maven / Gradle / Ivy

Go to download

Zero dependency library for generating a Mastercard API compliant OAuth signature

The newest version!
package com.mastercard.developer.signers;

import com.mastercard.developer.oauth.OAuth;

import java.nio.charset.Charset;
import java.security.PrivateKey;

import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

/**
 * Utility class for signing Spring RestTemplate requests.
 */
public class SpringHttpRequestSigner extends AbstractSigner {
    
    public SpringHttpRequestSigner(String consumerKey, PrivateKey signingKey) {
        super(consumerKey, signingKey);
    }
    
    public void sign(HttpRequest request, byte[] bytes) {
        HttpMethod method = request.getMethod();
        if (method == null) {
            throw new IllegalStateException("Can't sign a request with a null HTTP method!");
        }
        HttpHeaders headers = request.getHeaders();
        Charset charset = getCharset(headers);
        String payload = (null == bytes ? null : new String(bytes, charset));
        String authHeader = OAuth.getAuthorizationHeader(request.getURI(), method.toString(), payload, charset, consumerKey, signingKey);
        headers.add(OAuth.AUTHORIZATION_HEADER_NAME, authHeader);
    }
    
    private static Charset getCharset(HttpHeaders headers){
        Charset defaultCharset = Charset.defaultCharset();
        MediaType contentType = headers.getContentType();
        if(contentType != null){
            Charset charset = contentType.getCharset();
            if(charset != null){
                return charset;
            }
        }
        return defaultCharset;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy