简体   繁体   English

如何在GitHub上托管的Python脚本中保持开发人员密钥的秘密

[英]How to keep a developer key secret in a Python script that is hosted on GitHub

I am developing an open source Python-powered Twitter client, and to access the Twitter API and login using OAuth, I have registered my client with Twitter and they have given me a unique consumer key and consumer token (henceforth to be referred to as "developer key"). 我正在开发一个开源的Python支持的Twitter客户端,并且使用OAuth访问Twitter API和登录,我已经在Twitter上注册了我的客户端,他们给了我一个独特的消费者密钥和消费者令牌(以下称为“开发人员密钥“)。 These are unique to my client, and all copies of my client have to use the same developer key. 这些对我的客户来说是独一无二的,我客户的所有副本都必须使用相同的开发人员密钥。 Now, I have to use the developer key in a Python script (main.py) and since it is a script, there is no binary. 现在,我必须在Python脚本(main.py)中使用开发人员密钥,因为它是一个脚本,所以没有二进制文件。 Also, I have to upload my code to GitHub since I am using git on GitHub for content tracking. 此外,我必须将我的代码上传到GitHub,因为我在GitHub上使用git进行内容跟踪。 How do I keep my developer key secret? 如何保密我的开发人员密钥? Please keep in mind that I plan to distribute the same client to users. 请记住,我计划将相同的客户端分发给用户。

A keyring seems the best option, but I want a way that only the application can access the keyring, not even its users (outside the application). 密钥环似乎是最好的选择,但我想要一种方法,只有应用程序才能访问密钥环,甚至不是用户(在应用程序之外)。 And nobody should be able to figure out how to access the keyring by looking at my code. 没有人能够通过查看我的代码来弄清楚如何访问密钥环。

Note: "To use the Twitter API, the first thing you have to do is register a client application. Each client application you register will be provisioned a consumer key and secret. This key and secret scheme is similar to the public and private keys used in protocols such as ssh for those who are familiar. This key and secret will be used, in conjunction with an OAuth library in your programming language of choice, to sign every request you make to the API. It is through this signing process that we trust that the traffic that identifies itself is you is in fact you." 注意:“要使用Twitter API,您要做的第一件事就是注册客户端应用程序。您注册的每个客户端应用程序都将配置一个消费者密钥和密钥。此密钥和秘密方案类似于使用的公钥和私钥对于那些熟悉的人来说,在ssh这样的协议中,这个密钥和秘密将与您选择的编程语言中的OAuth库结合使用,来签署您对API所做的每一个请求。通过这个签名过程,我们相信自己确定的流量就是你。“ - http://dev.twitter.com/pages/auth - http://dev.twitter.com/pages/auth

You can use OAuth.io for this purpose. 您可以将OAuth.io用于此目的。

The concept is simple: 这个概念很简单:

  • you just have to put your API Keys in the key manager of OAuth.io 您只需将API密钥放在OAuth.io的密钥管理器中即可
  • in your source code, use the OAuth.io's public key 在源代码中,使用OAuth.io的公钥

Your secret key won't be leaked in this way. 您的密钥不会以这种方式泄露。

Check this blogpost using Twitter API with OAuth.io: http://blog.oauth.io/api-call-using-twitter-api/ 使用Twitter API和OAuth.io查看此博客帖子: http ://blog.oauth.io/api-call-using-twitter-api/

The complete sample code (in javascript) is on JSFiddle: http://jsfiddle.net/thyb/kZExJ/5 完整的示例代码(在javascript中)在JSFiddle上: http//jsfiddle.net/thyb/kZExJ/5

$('button').click(function() {
    OAuth.initialize('oEcDIQahkO4TUAND-yTs-H6oY_M') //OAuth.io public key
    OAuth.popup('twitter', function(err, res) {
        // res contains tokens (res.oauth_token and res.oauth_token_secret)
        res.get('/1.1/statuses/home_timeline.json').done(function(data) {
            // do what you want with data
        })
    })
})

The key must be outside the source code, and be passed to the program through the command line or a configuration file. 密钥必须在源代码之外,并通过命令行或配置文件传递给程序。 There's no way to hide the key if you embed it in the source code (a debugger, fi, will show it). 如果将其嵌入源代码(调试器,fi,将显示它),则无法隐藏密钥。

More importantly, to avoid collisions or users getting to know the key, one should not have different users share the same key . 更重要的是,为避免冲突或用户了解密钥,不应让不同的用户共享相同的密钥。 What's typically done is to set up a web service that knows the key and talks to the final server (Twitter). 通常做的是建立一个知道密钥并与最终服务器(Twitter)通信的Web服务。 The client software would communicate with the service using a per-user key. 客户端软件将使用每用户密钥与服务通信。

Extending on Apalala's answer, I believe what is meant is a 'proxy' web service. 根据Apalala的回答,我认为这意味着“代理”网络服务。 People send you their requests and you sign it on their behalf and send it to twitter, once they allow your application access of course. 当人们允许您的应用程序访问时,人们会向您发送他们的请求并代表他们签名并将其发送给Twitter。

You don't have to worry about people spamming you because they will have to log in to twitter anyway to use it. 您不必担心人们会向您发送垃圾邮件,因为他们无论如何都必须登录到Twitter才能使用它。

Only problem, like anywhere else, is how do I trust your application enough to allow it in the first place :) 唯一的问题,就像其他地方一样,是我如何信任你的应用程序,足以让它在第一时间:)

I would not distribute any key with the code; 我不会用代码分发任何密钥; if people want to use it, they will just have to apply for their own key. 如果人们想要使用它,他们只需要申请自己的密钥。 Any other approach can be abused. 任何其他方法都可能被滥用。

Create a configuration file where you will keep the key. 创建一个配置文件,您将保留密钥。 Do not post the original configuration file into git-hub. 不要将原始配置文件发布到git-hub。

You can use Python Config Module (overkill) or YAML (my choice) or plain Files. 您可以使用Python配置模块(overkill)或YAML(我的选择)或普通文件。

If you want people just to get up and running you can create a prompt which runs only the first time in a system and generate the configuration file by taking user input. 如果您希望人们只是启动并运行,您可以创建一个仅在系统中第一次运行的提示,并通过获取用户输入来生成配置文件。

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

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