小编jma*_*ndt的帖子

使用 FastAPI 在基于 Python 的 GraphQL 服务器中进行身份验证验证

我在使用 FastAPI 构建的 GraphQL 服务器中实现身份验证验证时遇到问题。之前,我们使用 REST,但现在我们正在切换到 GraphQL,我想知道如何实现这一点。之前,我们有不同的路由器,并与FastAPI很容易基于使用的依赖关系为途径,以检查认证在这里。我们在授权标头中发送一个令牌,我们在后端对其进行解码并取回 user_id,然后我们可以在不同的端点中使用它。

我想知道这里如何使用 GraphQL。我们使用石墨烯,我查看了Starlettes 身份验证示例以及设置GraphQl 的介绍

import binascii
from fastapi import FastAPI
from starlette.authentication import (
    AuthenticationBackend, AuthenticationError, SimpleUser, AuthCredentials
)
from starlette.graphql import GraphQLApp
from starlette.middleware import Middleware
from starlette.middleware.authentication import AuthenticationMiddleware

from schemas.root import my_schema


class BasicAuthBackend(AuthenticationBackend):
    async def authenticate(self, request):
        if "Authorization" not in request.headers:
            raise AuthenticationError('No auth credentials')

        auth = request.headers["Authorization"]
        try:
            id_token = auth.split('Bearer ')[1]
            decoded_token = auth.verify_id_token(id_token)

        except (ValueError, UnicodeDecodeError, …
Run Code Online (Sandbox Code Playgroud)

python authentication graphql starlette fastapi

4
推荐指数
1
解决办法
2119
查看次数

Google Cloud 转发规则 http -> https 使用 terraform

我设置了转发规则,使用 Terraform 将 URL 映射到我的 GCS Bucket。现在,我正在寻找一种方法来自动将所有流量从 HTTP 转发到 HTTPS,因此每个通过 HTTP 访问我的页面的人都会自动进入安全页面。

知道如何使用 terraform 做到这一点吗?您可以在下面找到到目前为止我用来设置它的所有代码,这些代码运行良好。我只需要这个额外的转发规则,但不知道如何设置。任何帮助将不胜感激。

locals {
  static_bucket_name = "${var.environment}-${var.project_name}-static-pages"
  domain_name        = var.environment == "prd" ? "products.${project_name}.org" : "${var.environment}.products.${project_name}.org"
}

module "static-assets_cloud-storage-static-website" {
  source                           = "gruntwork-io/static-assets/google//modules/cloud-storage-static-website"
  version                          = "0.2.0"
  website_domain_name              = local.static_bucket_name
  project                          = var.project_id
  website_location                 = "EU"
  force_destroy_access_logs_bucket = true
  force_destroy_website            = true

  custom_labels = {
    environment = var.environment
    purpose     = "static-site"
  }
}


resource "google_compute_backend_bucket" "static_pages" {
  name        = local.static_bucket_name
  description = "Contains static app assets"
  bucket_name = …
Run Code Online (Sandbox Code Playgroud)

load-balancing google-cloud-storage google-cloud-platform terraform terraform-provider-gcp

3
推荐指数
1
解决办法
518
查看次数