https://github.com/mastercard/oauth1-signer-python
Python library for generating a Mastercard API compliant OAuth signature.
https://github.com/mastercard/oauth1-signer-python
mastercard oauth1 oauth1a openapi python python3
Last synced: 3 months ago
JSON representation
Python library for generating a Mastercard API compliant OAuth signature.
- Host: GitHub
- URL: https://github.com/mastercard/oauth1-signer-python
- Owner: Mastercard
- License: mit
- Created: 2019-04-15T10:33:14.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2024-07-03T12:16:17.000Z (11 months ago)
- Last Synced: 2025-03-04T05:34:38.715Z (3 months ago)
- Topics: mastercard, oauth1, oauth1a, openapi, python, python3
- Language: Python
- Homepage: https://developer.mastercard.com/platform/documentation/security-and-authentication/using-oauth-1a-to-access-mastercard-apis/
- Size: 217 KB
- Stars: 29
- Watchers: 16
- Forks: 22
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# oauth1-signer-python
[](https://developer.mastercard.com/)[](https://github.com/Mastercard/oauth1-signer-python/actions?query=workflow%3A%22Build+%26+Test%22)
[](https://sonarcloud.io/dashboard?id=Mastercard_oauth1-signer-python)
[](https://sonarcloud.io/dashboard?id=Mastercard_oauth1-signer-python)
[](https://sonarcloud.io/dashboard?id=Mastercard_oauth1-signer-python)
[](https://github.com/Mastercard/oauth1-signer-python/actions?query=workflow%3A%22broken+links%3F%22)
[](https://pypi.org/project/mastercard-oauth1-signer)
[](https://github.com/Mastercard/oauth1-signer-python/blob/master/LICENSE)## Table of Contents
- [Overview](#overview)
* [Compatibility](#compatibility)
* [References](#references)
* [Versioning and Deprecation Policy](#versioning)
- [Usage](#usage)
* [Prerequisites](#prerequisites)
* [Adding the Library to Your Project](#adding-the-library-to-your-project)
* [Importing the Code](#importing-the-code)
* [Loading the Signing Key](#loading-the-signing-key)
* [Creating the OAuth Authorization Header](#creating-the-oauth-authorization-header)
* [Signing HTTP Client Request Objects](#signing-http-client-request-objects)
* [Integrating with OpenAPI Generator API Client Libraries](#integrating-with-openapi-generator-api-client-libraries)## Overview
Python library for generating a Mastercard API compliant OAuth signature.### References
* [OAuth 1.0a specification](https://tools.ietf.org/html/rfc5849)
* [Body hash extension for non application/x-www-form-urlencoded payloads](https://tools.ietf.org/id/draft-eaton-oauth-bodyhash-00.html)### Versioning and Deprecation Policy
* [Mastercard Versioning and Deprecation Policy](https://github.com/Mastercard/.github/blob/main/CLIENT_LIBRARY_DEPRECATION_POLICY.md)## Usage
### Prerequisites
Before using this library, you will need to set up a project in the [Mastercard Developers Portal](https://developer.mastercard.com).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``` python
import oauth1.authenticationutils as authenticationutils
from oauth1.oauth import OAuth
```
### Loading the Signing KeyA private key object can be created by calling the `authenticationutils.load_signing_key` method:
``` python
signing_key = authenticationutils.load_signing_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.#### POST example
```python
uri = 'https://sandbox.api.mastercard.com/service'
payload = 'Hello world!'
authHeader = OAuth.get_authorization_header(uri, 'POST', payload, '', signing_key)
```#### GET example
```python
uri = 'https://sandbox.api.mastercard.com/service'
authHeader = OAuth.get_authorization_header(uri, 'GET', None, '', signing_key)
```#### Use of authHeader with requests module (POST and GET example)
```python
headerdict = {'Authorization' : authHeader}
requests.post(uri, headers=headerdict, data=payload)
requests.get(uri, headers=headerdict)
```### 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™](#requests)
#### Requests: HTTP for Humans™
You can sign [request](https://requests.readthedocs.io/en/latest/user/quickstart#make-a-request) objects using the `OAuthSigner` class.
Usage:
```python
uri = "https://sandbox.api.mastercard.com/service"
request = Request()
request.method = "POST"
# …signer = OAuthSigner(consumer_key, signing_key)
request = signer.sign_request(uri, request)
```#### Usage of the `oauth_ext`
The requests library supports custom authentication extensions, with which the procedure of creating and calling such requests can simplify the process of request signing. Please, see the examples below:###### POST example
```python
from oauth1.oauth_ext import OAuth1RSA
from oauth1.oauth_ext import HASH_SHA256
import requestsuri = 'https://sandbox.api.mastercard.com/service'
oauth = OAuth1RSA(consumer_key, signing_key)
header = {'Content-type' : 'application/json', 'Accept' : 'application/json'}# Passing payload for data parameter as string
payload = '{"key" : "value"}'
response = requests.post(uri, data=payload, auth=oauth, headers=header)# Passing payload for data parameter as Json object
payload = {'key' : 'value'}
response = requests.post(uri, data=json.dumps(payload), auth=oauth, headers=header)# Passing payload for json parameter Json object
payload = {'key' : 'value'}
response = requests.post(uri, json=payload, auth=oauth, headers=header)
```###### GET example
```python
from oauth1.oauth_ext import OAuth1RSA
import requestsuri = 'https://sandbox.api.mastercard.com/service'
oauth = OAuth1RSA(consumer_key, signing_key)# Operation for get call
response = requests.get(uri, auth=oauth)
```### Integrating with OpenAPI Generator API Client Libraries
[OpenAPI Generator](https://github.com/OpenAPITools/openapi-generator) generates API client libraries from [OpenAPI Specs](https://github.com/OAI/OpenAPI-Specification).
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](#python)##### OpenAPI Generator
Client libraries can be generated using the following command:
```shell
openapi-generator-cli generate -i openapi-spec.yaml -g python -o out
```
See also:
* [OpenAPI Generator CLI Installation](https://openapi-generator.tech/docs/installation/)
* [CONFIG OPTIONS for python](https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/python.md)##### Usage of the `oauth1.signer_interceptor`
```python
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, '', '', '')
some_api = openapi_client.SomeApi(client)
result = some_api.do_something()
# …
```