oauth1-signer-ruby
Table of Contents
Overview
Zero dependency library for generating a Mastercard API compliant OAuth signature.
Compatibility
- Ruby 2.4.4+
- Truffle Ruby 1.0.0+
References
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 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 'oauth'
consumer_key = "<insert consumer key>";
uri = "https://sandbox.api.mastercard.com/service";
method = "POST";
payload = "Hello world!";
authHeader = 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
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:
Typhoeus.before
Callback method 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 =
OAuth.get_authorization_header request.base_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