Repository is archived
No release in over a year
Zero dependency library for generating a Mastercard API compliant OAuth signature
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 1.5
~> 5.0
>= 12.3.3
~> 0.16.1
 Project Readme

oauth1-signer-ruby

maintenance-status

Table of Contents

  • Overview
    • Compatibility
    • References
    • Versioning and Deprecation Policy
  • Usage
    • Prerequisites
    • Adding the Library to Your Project
    • Loading the Signing Key
    • Creating the OAuth Authorization Header
    • Integrating with OpenAPI Generator API Client Libraries

Overview

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

Compatibility

  • Ruby 2.5+
  • Truffle Ruby 1.0.0+

References

Versioning and Deprecation Policy

Usage

Prerequisites

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

As part of this set up, you'll receive credentials for your app:

  • A consumer key (displayed on the Mastercard Developer Portal)
  • A private request signing key (matching the public certificate displayed on the Mastercard Developer Portal)

Adding the Library to Your Project

gem install mastercard_oauth1_signer

Loading the Signing Key

The following code shows how to load the private key using OpenSSL:

require 'openssl'

is = File.binread("<insert PKCS#12 key file path>");
signing_key = OpenSSL::PKCS12.new(is, "<insert key password>").key;

Creating the OAuth Authorization Header

The method that does all the heavy lifting is Mastercard::OAuth.get_authorization_header. 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.

require 'mastercard/oauth'

consumer_key = "<insert consumer key>";
uri = "https://sandbox.api.mastercard.com/service";
method = "POST";
payload = "Hello world!";
authHeader = Mastercard::OAuth.get_authorization_header(uri, method, payload, consumer_key, signing_key);

Integrating with OpenAPI Generator API Client Libraries

OpenAPI Generator generates API client libraries from OpenAPI Specs. It provides generators and library templates for supporting multiple languages and frameworks.

Generators currently supported:

  • ruby

ruby

OpenAPI Generator

Client libraries can be generated using the following command:

openapi-generator-cli generate -i openapi-spec.yaml -g ruby -o out

See also:

Callback method Typhoeus.before

The Authorization header can be hooked into before a request run:

config = OpenapiClient::Configuration.default
api_client = OpenapiClient::ApiClient.new
config.basePath = "https://sandbox.api.mastercard.com"
api_client.config = config

Typhoeus.before { |request|
  authHeader =
      Mastercard::OAuth.get_authorization_header request.url, request.options[:method],
                                     request.options[:body], consumer_key, signing_key.key
  request.options[:headers] = request.options[:headers].merge({'Authorization' => authHeader})
}
    
serviceApi = service.ServiceApi.new api_client

opts = {}
serviceApi.call opts
// 

See also: https://rubydoc.info/github/typhoeus/typhoeus/frames/Typhoeus#before-class_method