【发布时间】:2020-04-07 01:25:33
【问题描述】:
我有一个 GraphQL API,我一直在用 go 编写,想知道当您已经在使用 context 传递数据源时如何管理 JWT 身份验证。
所以我的main 函数的缩写版本是:
import (
"net/http"
"github.com/graphql-go/graphql"
gqlhandler "github.com/graphql-go/handler"
)
func queryHandler(ds *sources.DataSources, gql *gqlhandler.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(context.Background(), sources.CtxSourcesKey, ds)
gql.ContextHandler(ctx, w, r)
})
}
func main() {
apiSchema, _ := schema.CompileSchema(schema.QueryType, schema.MutationType)
gql := gqlhandler.New(&gqlhandler.Config{
Schema: &apiSchema,
GraphiQL: !isDeployed,
Pretty: false,
Playground: false,
})
http.ListenAndServe(":41000", util.CreateChiRouter(healthCheckHandler(), queryHandler(ctxSources, gql)))
}
如您所见,我已经创建了一个新的context 实例来存储我的各种数据源的映射并将其传递给查询解析函数,但还需要能够解析出Authorization 标头为经过身份验证的路由传递可能的 JWT。
考虑到我目前的情况,最好的解决方法是什么? (将 JWT 与数据源上下文相结合?以不同方式处理数据源以释放 context?)
【问题讨论】:
标签: go authentication graphql jwt