Skip to content

Python library for generating a Mastercard API compliant OAuth signature.

License

Notifications You must be signed in to change notification settings

mahendrachhipa/oauth1-signer-python

 
 

Repository files navigation

oauth1-signer-python

Table of Contents

Overview

Python library for generating a Mastercard API compliant OAuth signature.

Compatibility

Python 3.6+

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

pip install mastercard-oauth1-signer

Importing the Code

import oauth1.authenticationutils as authenticationutils
from oauth1.oauth import OAuth

Loading the Signing Key

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>')

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.

POST example

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

GET example

uri = 'https://sandbox.api.mastercard.com/service'
authHeader = OAuth().get_authorization_header(uri, 'GET', None, '<insert consumer key>', signing_key)

Signing HTTP Client Request Objects

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.

Requests: HTTP for Humans™

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)

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.

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:

python

OpenAPI Generator

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:

Usage of the oauth1.signer_interceptor
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()
# ...

About

Python library for generating a Mastercard API compliant OAuth signature.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 100.0%