【问题标题】:OAuth2 in development and production开发和生产中的 OAuth2
【发布时间】:2013-09-17 17:03:58
【问题描述】:

在开发和生产环境中使用 OAuth2 身份验证的最佳策略是什么?

例如,我想创建一个基于 Web 的开源 GitHub 客户端。我已将我的客户注册为 GitHub 应用程序。根据 OAuth2 规范,我必须在注册应用程序时指定重定向 url。我应该使用什么 redirect_url,基于 localhost 的还是真实的生产 url?如果我使用基于 localhost(用于开发),我的生产站点显然会停止工作(反之亦然)。

将 client_id 和 client_secret 存储在公共代码中是否安全?如果不是,存储它的最佳策略是什么(即在一些未添加到源版本控制系统的配置文件中)?

【问题讨论】:

    标签: oauth-2.0 development-environment production-environment


    【解决方案1】:

    对于 Rails,我使用了一个名为 figaro 的 gem。

    您定义特定于环境的变量 (config/application.yml):

    CALENDAR_SCOPE: https://www.googleapis.com/auth/calendar
    
    production:
      CLIENT_ID: 393sdfgsdfg.apps.googleusercontent.com
      CLIENT_SECRET: sdfgdfsgsg
      OAUTH2_REDIRECT: http://mydomain/users/auth/google_oauth2/callback
      etc...
    
    development:
      CLIENT_ID: 24asdfsadfas.apps.googleusercontent.com
      CLIENT_SECRET: asdfsadf
      OAUTH2_REDIRECT: http://localhost:3000/users/auth/google_oauth2/callback
      etc....
    

    然后你在你的代码中使用这些:

    client = Google::APIClient.new({:auto_refresh_token => false})
    client.authorization.scope = ENV['CALENDAR_SCOPE']
    client.authorization.client_id = ENV['CLIENT_ID']
    client.authorization.client_secret = ENV['CLIENT_SECRET']
    client.authorization.redirect_uri = ENV['OAUTH2_REDIRECT']
    etc...
    

    您必须记住在启动服务器时设置环境。例如:

    thin -e production start
    

    thin -e development start
    

    您可能想要 gitignore application.yml 文件。

    【讨论】:

    • 所以您使用了两个独立的应用程序——一个用于生产,一个用于开发。有趣的想法。但是你如何在开发者之间共享你的 application.yml 文件呢?
    • 那么解决方案是根据环境的需要创建 OAuth 客户端 ID,并分别从环境变量中获取所有配置?
    猜你喜欢
    • 2013-09-01
    • 2011-09-02
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    • 2010-09-29
    • 2014-03-31
    • 1970-01-01
    • 2022-11-20
    相关资源
    最近更新 更多