{"id":26558105,"url":"https://github.com/mastercard/oauth1-signer-python","last_synced_at":"2025-03-22T12:22:34.053Z","repository":{"id":45427113,"uuid":"181465073","full_name":"Mastercard/oauth1-signer-python","owner":"Mastercard","description":"Python library for generating a Mastercard API compliant OAuth signature.","archived":false,"fork":false,"pushed_at":"2024-07-03T12:16:17.000Z","size":222,"stargazers_count":29,"open_issues_count":0,"forks_count":22,"subscribers_count":16,"default_branch":"main","last_synced_at":"2025-03-04T05:34:38.715Z","etag":null,"topics":["mastercard","oauth1","oauth1a","openapi","python","python3"],"latest_commit_sha":null,"homepage":"https://developer.mastercard.com/platform/documentation/security-and-authentication/using-oauth-1a-to-access-mastercard-apis/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Mastercard.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-04-15T10:33:14.000Z","updated_at":"2024-07-29T20:30:12.000Z","dependencies_parsed_at":"2023-02-14T06:30:27.924Z","dependency_job_id":"c2517dfb-683f-4004-9669-db199d275d56","html_url":"https://github.com/Mastercard/oauth1-signer-python","commit_stats":{"total_commits":204,"total_committers":18,"mean_commits":"11.333333333333334","dds":0.5931372549019608,"last_synced_commit":"7205e450328fa932457a311d536afd6768304aec"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mastercard%2Foauth1-signer-python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mastercard%2Foauth1-signer-python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mastercard%2Foauth1-signer-python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Mastercard%2Foauth1-signer-python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Mastercard","download_url":"https://codeload.github.com/Mastercard/oauth1-signer-python/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244954282,"owners_count":20537744,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mastercard","oauth1","oauth1a","openapi","python","python3"],"created_at":"2025-03-22T12:22:33.433Z","updated_at":"2025-03-22T12:22:34.040Z","avatar_url":"https://github.com/Mastercard.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# oauth1-signer-python\n[![](https://developer.mastercard.com/_/_/src/global/assets/svg/mcdev-logo-dark.svg)](https://developer.mastercard.com/)\n\n[![](https://github.com/Mastercard/oauth1-signer-python/workflows/Build%20\u0026%20Test/badge.svg)](https://github.com/Mastercard/oauth1-signer-python/actions?query=workflow%3A%22Build+%26+Test%22)\n[![](https://sonarcloud.io/api/project_badges/measure?project=Mastercard_oauth1-signer-python\u0026metric=alert_status)](https://sonarcloud.io/dashboard?id=Mastercard_oauth1-signer-python)\n[![](https://sonarcloud.io/api/project_badges/measure?project=Mastercard_oauth1-signer-python\u0026metric=coverage)](https://sonarcloud.io/dashboard?id=Mastercard_oauth1-signer-python)\n[![](https://sonarcloud.io/api/project_badges/measure?project=Mastercard_oauth1-signer-python\u0026metric=vulnerabilities)](https://sonarcloud.io/dashboard?id=Mastercard_oauth1-signer-python)\n[![](https://github.com/Mastercard/oauth1-signer-python/workflows/broken%20links%3F/badge.svg)](https://github.com/Mastercard/oauth1-signer-python/actions?query=workflow%3A%22broken+links%3F%22)\n[![](https://img.shields.io/pypi/v/mastercard-oauth1-signer.svg?style=flat\u0026color=blue)](https://pypi.org/project/mastercard-oauth1-signer)\n[![](https://img.shields.io/badge/license-MIT-yellow.svg)](https://github.com/Mastercard/oauth1-signer-python/blob/master/LICENSE)\n\n\n## Table of Contents\n- [Overview](#overview)\n  * [Compatibility](#compatibility)\n  * [References](#references)\n  * [Versioning and Deprecation Policy](#versioning)\n- [Usage](#usage)\n  * [Prerequisites](#prerequisites)\n  * [Adding the Library to Your Project](#adding-the-library-to-your-project)\n  * [Importing the Code](#importing-the-code)\n  * [Loading the Signing Key](#loading-the-signing-key) \n  * [Creating the OAuth Authorization Header](#creating-the-oauth-authorization-header)\n  * [Signing HTTP Client Request Objects](#signing-http-client-request-objects)\n  * [Integrating with OpenAPI Generator API Client Libraries](#integrating-with-openapi-generator-api-client-libraries)\n\n## Overview \u003ca name=\"overview\"\u003e\u003c/a\u003e\nPython library for generating a Mastercard API compliant OAuth signature.\n\n### Compatibility \u003ca name=\"compatibility\"\u003e\u003c/a\u003e\nPython 3.8+\n\n### References \u003ca name=\"references\"\u003e\u003c/a\u003e\n* [OAuth 1.0a specification](https://tools.ietf.org/html/rfc5849)\n* [Body hash extension for non application/x-www-form-urlencoded payloads](https://tools.ietf.org/id/draft-eaton-oauth-bodyhash-00.html)\n\n### Versioning and Deprecation Policy \u003ca name=\"versioning\"\u003e\u003c/a\u003e\n* [Mastercard Versioning and Deprecation Policy](https://github.com/Mastercard/.github/blob/main/CLIENT_LIBRARY_DEPRECATION_POLICY.md)\n\n## Usage \u003ca name=\"usage\"\u003e\u003c/a\u003e\n### Prerequisites \u003ca name=\"prerequisites\"\u003e\u003c/a\u003e\nBefore using this library, you will need to set up a project in the [Mastercard Developers Portal](https://developer.mastercard.com). \n\nAs part of this set up, you'll receive credentials for your app:\n* A consumer key (displayed on the Mastercard Developer Portal)\n* A private request signing key (matching the public certificate displayed on the Mastercard Developer Portal)\n\n### Adding the Library to Your Project \u003ca name=\"adding-the-library-to-your-project\"\u003e\u003c/a\u003e\n\n```\npip install mastercard-oauth1-signer\n```\n### Importing the Code \u003ca name=\"importing-the-code\"\u003e\u003c/a\u003e\n\n``` python\nimport oauth1.authenticationutils as authenticationutils\nfrom oauth1.oauth import OAuth\n```\n### Loading the Signing Key \u003ca name=\"loading-the-signing-key\"\u003e\u003c/a\u003e\n\nA private key object can be created by calling the `authenticationutils.load_signing_key` method:\n``` python\nsigning_key = authenticationutils.load_signing_key('\u003cinsert PKCS#12 key file path\u003e', '\u003cinsert key password\u003e')\n```\n\n### Creating the OAuth Authorization Header \u003ca name=\"creating-the-oauth-authorization-header\"\u003e\u003c/a\u003e\nThe 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.\n\n#### POST example\n\n```python\nuri = 'https://sandbox.api.mastercard.com/service'\npayload = 'Hello world!'\nauthHeader = OAuth.get_authorization_header(uri, 'POST', payload, '\u003cinsert consumer key\u003e', signing_key)\n```\n\n#### GET example\n```python\nuri = 'https://sandbox.api.mastercard.com/service'\nauthHeader = OAuth.get_authorization_header(uri, 'GET', None, '\u003cinsert consumer key\u003e', signing_key)\n```\n\n#### Use of authHeader with requests module (POST and GET example)\n```python\nheaderdict = {'Authorization' : authHeader}\nrequests.post(uri, headers=headerdict, data=payload)\nrequests.get(uri, headers=headerdict)\n```\n\n### Signing HTTP Client Request Objects \u003ca name=\"signing-http-client-request-objects\"\u003e\u003c/a\u003e\n\nAlternatively, you can use helper classes for some of the commonly used HTTP clients.\n\nThese 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. \n\nUsage briefly described below, but you can also refer to the test project for examples. \n\n+ [Requests: HTTP for Humans™](#requests)\n\n#### Requests: HTTP for Humans™ \u003ca name=\"requests\"\u003e\u003c/a\u003e\n\nYou can sign [request](https://requests.readthedocs.io/en/latest/user/quickstart#make-a-request) objects using the `OAuthSigner` class. \n\nUsage:\n```python\nuri = \"https://sandbox.api.mastercard.com/service\"\nrequest = Request()\nrequest.method = \"POST\"\n# …\n\nsigner = OAuthSigner(consumer_key, signing_key)\nrequest = signer.sign_request(uri, request)\n```\n\n\n#### Usage of the `oauth_ext`\nThe 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:\n\n###### POST example\n\n```python\nfrom oauth1.oauth_ext import OAuth1RSA\nfrom oauth1.oauth_ext import HASH_SHA256\nimport requests\n\nuri = 'https://sandbox.api.mastercard.com/service'\noauth = OAuth1RSA(consumer_key, signing_key)\nheader = {'Content-type' : 'application/json', 'Accept' : 'application/json'}\n\n# Passing payload for data parameter as string\npayload = '{\"key\" : \"value\"}'\nresponse = requests.post(uri, data=payload, auth=oauth, headers=header)\n\n# Passing payload for data parameter as Json object\npayload = {'key' : 'value'}\nresponse = requests.post(uri, data=json.dumps(payload), auth=oauth, headers=header)\n\n# Passing payload for json parameter Json object\npayload = {'key' : 'value'}\nresponse = requests.post(uri, json=payload, auth=oauth, headers=header)\n```\n\n###### GET example\n\n```python\nfrom oauth1.oauth_ext import OAuth1RSA\nimport requests\n\nuri = 'https://sandbox.api.mastercard.com/service'\noauth = OAuth1RSA(consumer_key, signing_key)\n\n# Operation for get call\nresponse = requests.get(uri, auth=oauth)\n```\n\n### Integrating with OpenAPI Generator API Client Libraries \u003ca name=\"integrating-with-openapi-generator-api-client-libraries\"\u003e\u003c/a\u003e\n\n[OpenAPI Generator](https://github.com/OpenAPITools/openapi-generator) generates API client libraries from [OpenAPI Specs](https://github.com/OAI/OpenAPI-Specification). \nIt provides generators and library templates for supporting multiple languages and frameworks.\n\nThis 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.\n\nGenerators currently supported:\n+ [python](#python)\n\n#### python \u003ca name=\"python\"\u003e\u003c/a\u003e\n\n##### OpenAPI Generator\n\nClient libraries can be generated using the following command:\n```shell\nopenapi-generator-cli generate -i openapi-spec.yaml -g python -o out\n```\nSee also:\n* [OpenAPI Generator CLI Installation](https://openapi-generator.tech/docs/installation/)\n* [CONFIG OPTIONS for python](https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/python.md)\n\n##### Usage of the `oauth1.signer_interceptor`\n\n```python\nimport openapi_client\nfrom oauth1.signer_interceptor import add_signer_layer\n\n# …\nconfig = openapi_client.Configuration()\nconfig.host = 'https://sandbox.api.mastercard.com'\nclient = openapi_client.ApiClient(config)\nadd_signer_layer(client, '\u003cinsert PKCS#12 key file path\u003e', '\u003cinsert key password\u003e', '\u003cinsert consumer key\u003e')\nsome_api = openapi_client.SomeApi(client)\nresult = some_api.do_something()\n# …\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmastercard%2Foauth1-signer-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmastercard%2Foauth1-signer-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmastercard%2Foauth1-signer-python/lists"}