Zero dependency library for generating a Mastercard API compliant OAuth signature.
- Ruby 2.5+
- Truffle Ruby 1.0.0+
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)
gem install mastercard_oauth1_signer
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;
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);
OpenAPI Generator generates API client libraries from OpenAPI Specs. It provides generators and library templates for supporting multiple languages and frameworks.
Generators currently supported:
Client libraries can be generated using the following command:
openapi-generator-cli generate -i openapi-spec.yaml -g ruby -o out
See also:
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