graphql-go / graphql-go/graphql

Question: implement authorization using extensions.

Open
#494 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
10.1k
Forks
845
PR merge metrics
No merged PRs in 30d

Description

Hi!, first of all I want to thank you guys for your work in this project!,

I've been poking w authentication and authorization the last few days, I'm pretty much done w the authentication part (I receive the token via http headers and I parse it from a custom middleware, identify the current user and pass the information along using Context).

On the other hand, i'm still trying to figure out the best way to implement authorization, is it possible to do it with a custom extension?

So, to clarify, given the following extension, is it possible to implement a basic authorization flow? (see method ExecutionDidStart)
```
package extensions

import (
"context"
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/gqlerrors"
)

type Authorization struct{}

func (t *Authorization) Init(ctx context.Context, p *graphql.Params) context.Context {
return ctx
}

func (t *Authorization) Name() string {
return "Authorization"
}

func (t *Authorization) HasResult() bool {
return false
}

func (t *Authorization) GetResult(ctx context.Context) interface{} {
return nil
}

func (t *Authorization) ParseDidStart(ctx context.Context) (context.Context, graphql.ParseFinishFunc) {
return ctx, func(err error) {}
}

func (t *Authorization) ValidationDidStart(ctx context.Context) (context.Context, graphql.ValidationFinishFunc) {
return ctx, func(errs []gqlerrors.FormattedError) {}
}

func (t *Authorization) ExecutionDidStart(ctx context.Context) (context.Context, graphql.ExecutionFinishFunc) {

// pick current session and params from ctx, if user is not authorized return an error somehow

return ctx, func(*graphql.Result) {}
}

func (t *Authorization) ResolveFieldDidStart(ctx context.Context, i *graphql.ResolveInfo) (context.Context, graphql.ResolveFieldFinishFunc) {
return ctx, func(v interface{}, err error) {}
}

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.