【问题标题】:Authentication using Google Service Account in a flask app and deploying on Google App Engine在烧瓶应用中使用 Google 服务帐户进行身份验证并在 Google App Engine 上部署
【发布时间】:2021-03-31 07:22:23
【问题描述】:

以下是我的要求。

  • 开发一个烧瓶应用程序。
  • 在应用的 Firebase 中使用集合。
  • 使用标准服务帐户在 Google App Engine 上部署此应用

我做了什么。

  • 创建了服务帐号
  • 下载了对应的凭证json;我称它为 key.json
  • 写了一个main.py
cred = credentials.Certificate('key.json') 
default_app = initialize_app(cred) 
db = firestore.client()

user_ref = db.collection_group('Users')

@app.route('/', methods=['GET']) 
def home():
return "<h1>Welcome to my first app</h1>"

@app.route('/users', methods=['GET'])
def getUsers():
    try:
        result = [user.to_dict() for user in user_ref .stream()]
        return jsonify(result), 200
    except Exception as e:
        result = { "message:"failed"}
        return jsonify(result), 500

我已经在本地进行了测试,也在 Google App Engine 上进行了部署。

在这两种情况下,key.json 都与代码位于同一目录中。 我已经验证,如果这个 key.json 被修改为存储错误的数据,那么 /users 端点将无法工作并给我一个 500 错误。

到目前为止一切顺利。我想知道这是否是正确的方法。

  • 我希望即使对根/端点也应用 key.json 身份验证。 即,如果用户提供了一个有效的 key.json,那么只有 Welcome to my first app 应该被显示。 否则,需要显示Unauthorized user 消息。

【问题讨论】:

  • key.json 不是通常用于对最终用户进行身份验证的东西。我认为您需要重新考虑您的身份验证方案。
  • 从你的问题中不清楚你想做什么。看起来“key.json”保存在应用程序的源代码中。您是否希望用户提供 json 内容?就像@gaefan 提到的那样,您通常不使用 key.json 来验证用户。

标签: google-app-engine flask postman service-accounts


【解决方案1】:

正如@Gaefan 和@DishantMakwana 以及在此documentation 中提到的:

API 密钥仅标识应用程序,不需要用户身份验证。访问公共数据就足够了。

因此,为了验证/授权您的用户,您应该重新考虑您的策略。我建议您按照Authenticating as an end user Documentation 中的说明进行操作。

【讨论】:

  • 由于这是非常基本的身份验证,因此我的经理已同意使用 API 密钥。
  • 另外,我们正在使用秘密管理器来存储这些 api 密钥。
  • 我对如何使用身份验证机制有了一个很好的了解。我也会尽快尝试 oauth。
  • 好的,只要您知道所涉及的限制和风险,您就可以采取这条路线,重要的是要注意,为您的应用程序的每个用户提供key.json 是非常不切实际的,具体取决于用户和应用结构。
  • 另外,我找到了这个链接。 youtube.com/watch?v=4HAbnin3nto 也可能有帮助。我现在正在看。
【解决方案2】:

我发现我们可以使用 Google Cloud Endpoints 进行 API 管理。用作魅力。

【讨论】:

    猜你喜欢
    • 2019-05-01
    • 2010-12-23
    • 1970-01-01
    • 2014-08-14
    • 2015-01-02
    • 2020-03-21
    • 2012-01-26
    • 2020-05-21
    • 2011-02-12
    相关资源
    最近更新 更多