Giter Club home page Giter Club logo

oauth1-signer-java's Introduction

Table of contents

Overview

Zero dependency library for generating a Mastercard API compliant OAuth signature.

Compatibility

Java 1.6+

References

OAuth 1.0a specification

Body hash extension for non application/x-www-form-urlencoded payloads

Usage

Maven

<dependency>
    <groupId>com.mastercard.developer</groupId>
    <artifactId>oauth1-signer</artifactId>
    <version>1.0.0</version>
</dependency>

Prerequisites

Before using this library, you will need to set up a project and key in the Mastercard Developers Portal.

The two key pieces of information you will need are:

  • Consumer key
  • Private key matching the public key uploaded to Mastercard Developer Portal

Creating a valid OAuth string

The method that does all the heavy lifting is OAuth.getAuthorizationHeader. You can call into it directly and as long as you provide the correct parameters, it will return a string that you can add into your request's Authorization header.

String consumerKey = <insert consumer key from developer portal>;
PrivateKey signingKey = <initialize private key matching the consumer key>;
URI uri = URI.create("https://sandbox.api.mastercard.com/service");
String method = "GET";
String payload = "Hello world!";
Charset charset = Charset.forName("UTF-8");

String authHeader = OAuth.getAuthorizationHeader(uri, method, payload, charset, consumerKey, signingKey);

Using HTTP client helpers

Alternatively, you can use a helper class for some of the commonly used HTTP clients provided in the com.mastercard.developer.signers package. These classes will modify the provided request object in-place and add the correct Authorization header. Once instantiated with a consumer key and private key, these objects can be reused. Usage briefly described below, but you can also refer to the test package for examples.

Java HttpsURLConnection

String consumerKey = <insert consumer key from developer portal>;
PrivateKey signingKey = <initialize private key matching the consumer key>;
Charset charset = Charset.forName("UTF-8");
URL url = new URL("https://sandbox.api.mastercard.com/service");
String payload = "{\"foo\":\"bar\"}";

HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("Content-Type", "application/json; charset=" + charset.name());

HttpsUrlConnectionSigner signer = new HttpsUrlConnectionSigner(charset, consumerKey, signingKey);
signer.sign(con, payload);

Apache HTTP Client 4

String consumerKey = <insert consumer key from developer portal>;
PrivateKey signingKey = <initialize private key matching the consumer key>;
String payload = "{\"foo\":\"bar\"}";

HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost("https://sandbox.api.mastercard.com/service");
httpPost.setEntity(new StringEntity(payload, ContentType.APPLICATION_JSON));

ApacheHttpClient4Signer signer = new ApacheHttpClient4Signer(consumerKey, signingKey);
signer.sign(httpPost);

OkHttp

MediaType JSON = MediaType.parse("application/json; charset=utf-8");
String payload = "{\"foo\":\"bar\"}";

OkHttpClient client = new OkHttpClient();
RequestBody body = RequestBody.create(JSON, payload);
Request.Builder request = new Request.Builder()
        .url("https://sandbox.api.mastercard.com/service")
        .post(body);

OkHttpSigner signer = new OkHttpSigner(consumerKey, signingKey);
signer.sign(request);

oauth1-signer-java's People

Contributors

kkrauth avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.