简体   繁体   English

在Python库中存储OAuth令牌

[英]Storing OAuth Token in Python Library

I have a Python service which imports a library that talks to the PayPal API. 我有一个Python服务,它导入一个与PayPal API对话的库。 There is a config file that is passed into the library __init__() which contains the PayPal API username and password. 有一个配置文件传递到库__init__() ,其中包含PayPal API用户名和密码。

Calling the PayPal API token endpoint with the username and password will return a token used to authenticate during the pay call. 使用用户名和密码调用PayPal API令牌端点将返回用于在付费呼叫期间进行身份验证的令牌。 However, this token lasts for 90 minutes and should be reused. 但是,此令牌持续90分钟,应该重复使用。

There are multiple instances of this service running on different servers and they need to all share this one secret token. 在不同的服务器上运行此服务的多个实例,并且他们需要共享这一个秘密令牌。

What would the best way of storing this 9 minute token be? 存储这9分钟令牌的最佳方式是什么?

While you could persist this in a database, since it's only valid for 90 minutes, you might consider using an in-memory data store like Redis . 虽然您可以将其保留在数据库中,因为它仅在90分钟内有效,您可以考虑使用像Redis这样的内存数据存储。 It's very simple to set up and there are various Python clients available . 它的设置非常简单,并且有各种Python客户端可用

Redis in particular supports expiration time when setting a value , so you can make sure it'll only be kept for a set amount of time. Redis特别支持设置值时的到期时间,因此您可以确保它只会保留一段时间。 Of course, you should still have exception handling in place in case for some reason the key is invalidated early. 当然,如果由于某种原因密钥早期失效,您仍应该进行异常处理。

While this may introduce a software dependency if you're not already using a key-value store, it's not clear from your question how this library is intended to be used and thus whether this is an issue. 虽然如果您还没有使用键值存储,这可能会引入软件依赖性,但是从您的问题中不清楚该库是如何使用的,因此这是否是一个问题。

If installing other software is not an option, you could use a temporary file. 如果无法安装其他软件,则可以使用临时文件。 However, because Python's tempfile doesn't seem to support directly setting a temporary file's name, you might have to handle file management manually. 但是,由于Python的临时文件似乎不支持直接设置临时文件的名称,因此您可能必须手动处理文件管理。 For example: 例如:

import os
import time
import tempfile


# 90 minutes in seconds. Setting this a little lower would 
# probably be better to account for network latency.
MAX_AGE = 90 * 60
# /tmp/libname/ needs to exist for this to work; creating it
# if necessary shouldn't give you much trouble.
TOKEN_PATH = os.path.join(
    tempfile.gettempdir(), 
    'libname', 
    'paypal.token',
)


def get_paypal_token():
    token = None

    if os.path.isfile(TOKEN_PATH):
        token_age = time.time() - os.path.getmtime(TOKEN_PATH)

        if token_age < MAX_AGE:
            with open(TOKEN_PATH, 'r') as infile:
                # You might consider a test API call to establish token validity here.
                token = infile.read()

    if not token:
        # Get a token from the PayPal API and write it to TOKEN_PATH.
        token = 'dummy'

        with open(TOKEN_PATH, 'w') as outfile:
            outfile.write(token)

    return token

Depending on the environment, you would probably want to look into restricting permissions on this temp file. 根据环境的不同,您可能希望查看限制此临时文件的权限。 Regardless of how you persist the token, though, this code should be a useful example. 但是,无论您如何持久化令牌,此代码都应该是一个有用的示例。 I wouldn't be thrilled about sticking something like this on the file system, but if you already have the PayPal credentials used to request a token on disk, writing the token to temporary storage probably won't be a big deal. 我不会对在文件系统上粘贴这样的东西感到兴奋,但是如果你已经有用于在磁盘上请求令牌的PayPal凭证,那么将令牌写入临时存储可能不是什么大问题。

You could store the token as a system variable . 您可以将令牌存储为系统变量

import os

# Store token
os.environ['PAYPAL_API_TOKEN'] = <...>

# Retrieve token
token = os.environ['PAYPAL_API_TOKEN']

Be aware of the security implications though: Other processes could read the token. 但请注意安全隐患:其他进程可以读取令牌。

Just as a information: PayPal access token expires in 9 hours, not 90 minutes: 就像一个信息:PayPal访问令牌在9小时内到期,而不是90分钟:

"expires_in":32400 “expires_in”:32400

This time is in seconds: https://developer.paypal.com/docs/api/get-an-access-token-postman/ 这个时间是几秒钟: https//developer.paypal.com/docs/api/get-an-access-token-postman/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM