Python library for generating a Mastercard API compliant OAuth signature.
Python 3.6+
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)
pip install mastercard-oauth1-signer
import oauth1.authenticationutils as authenticationutils
from oauth1.oauth import OAuth
A private key object can be created by calling the authenticationutils.load_signing_key
method:
signing_key = authenticationutils.load_signing_key('<insert PKCS#12 key file path>', '<insert key password>')
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.
uri = 'https://sandbox.api.mastercard.com/service'
payload = 'Hello world!'
authHeader = OAuth().get_authorization_header(uri, 'POST', payload, '<insert consumer key>', signing_key)
uri = 'https://sandbox.api.mastercard.com/service'
authHeader = OAuth().get_authorization_header(uri, 'GET', None, '<insert consumer key>', signing_key)
Alternatively, you can use helper classes for some of the commonly used HTTP clients.
These classes will modify the provided request object in-place and will 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 project for examples.
You can sign request objects using the OAuthSigner
class.
Usage:
uri = "https://sandbox.api.mastercard.com/service"
request = Request()
request.method = "POST"
# ...
signer = OAuthSigner(consumer_key, signing_key)
request = signer.sign_request(uri, request)
OpenAPI Generator generates API client libraries from OpenAPI Specs. It provides generators and library templates for supporting multiple languages and frameworks.
This project provides you with classes you can use when configuring your API client. These classes will take care of adding the correct Authorization
header before sending the request.
Generators currently supported:
Client libraries can be generated using the following command:
java -jar openapi-generator-cli.jar generate -i openapi-spec.yaml -g python -o out
See also:
import openapi_client
from oauth1.signer_interceptor import add_signer_layer
# ...
config = openapi_client.Configuration()
config.host = 'https://sandbox.api.mastercard.com'
client = openapi_client.ApiClient(config)
add_signer_layer(client, '<insert PKCS#12 key file path>', '<insert key password>', '<insert consumer key>')
some_api = openapi_client.SomeApi(client)
result = some_api.do_something()
# ...