Mastercard / oauth1-signer-python

Python library for generating a Mastercard API compliant OAuth signature.
https://developer.mastercard.com/platform/documentation/security-and-authentication/using-oauth-1a-to-access-mastercard-apis/
MIT License
29 stars 22 forks source link

[BUG] OAuthSigner fails #38

Closed jleefresh closed 2 years ago

jleefresh commented 2 years ago

Bug Report Checklist

Description OAuthSigner fails with the following error:

  File ".../python3.9/site-packages/oauth1/signer.py", line 39, in sign_request
    oauth_key = OAuth.get_authorization_header(uri, request.method, request.data, self.consumer_key,          
AttributeError: 'PreparedRequest' object has no attribute 'data'                                              

To Reproduce Use OAuthSigner

Suggest a fix/enhancement https://github.com/Mastercard/oauth1-signer-python/blob/main/oauth1/signer.py#L39 uses request.data when it should be request.body

Tested on python 3.9 w/ python requests v2.27.1

ech0s7r commented 2 years ago

Hi @jleefresh,

Thanks for opening the issue.

Could you please provide a simple code sample to reproduce the issue?

Thanks!

jleefresh commented 2 years ago
import requests
from requests.auth import AuthBase

import oauth1.authenticationutils as authenticationutils
from oauth1.signer import OAuthSigner

KEY_FILE = 'signing-key.p12'
KEY_PASS = '<REDACTED>'
CONSUMER_KEY = '<REDACTED>'
ICA = '<REDACTED>'

class MCSigner(AuthBase):
    def __init__(self, consumer_key, signing_key):
        self.signer = OAuthSigner(consumer_key, signing_key)

    def __call__(self, request):
        self.signer.sign_request(request.url, request)
        return request

signing_key = authenticationutils.load_signing_key(KEY_FILE, KEY_PASS)
signer = MCSigner(CONSUMER_KEY, signing_key)
resp = requests.get(
    f'https://sandbox.api.mastercard.com/track/bps/supplier-payment-agents/{ICA}/suppliers',
    auth=signer
)

This produces the aforementioned error.

ech0s7r commented 2 years ago

Thanks, @jleefresh for sharing the code sample.

The AttributeError is thrown by the OAuthSigner.sign_request function because it is expecting a Request object instead of a PreparedRequest. We are going to update the function to work with both types and release a new version of the library. In the meantime, you could sign the requests with the supported Request object as documented in the README or by using the OAuth1RSA helper (see: https://github.com/Mastercard/oauth1-signer-python#usage-of-the-oauth_ext).