File size: 1,223 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
package apikey
import (
"context"
"github.com/99designs/gqlgen/graphql"
"github.com/target/goalert/permission"
"github.com/vektah/gqlparser/v2/gqlerror"
)
type Middleware struct{}
var _ graphql.OperationParameterMutator = Middleware{}
func (Middleware) ExtensionName() string { return "GQLAPIKeyMiddleware" }
func (Middleware) Validate(schema graphql.ExecutableSchema) error { return nil }
func (Middleware) MutateOperationParameters(ctx context.Context, rc *graphql.RawParams) *gqlerror.Error {
p := PolicyFromContext(ctx)
if p == nil {
return nil
}
if rc.Query == "" {
// Allow query to be omitted for API key requests,
// since they are always fixed to the key itself.
//
// The stored query hass been validated beforehand against
// the API key's embedded hash.
//
// This helps with things like key rotations, where the
// query may not be known to the client, or would otherwise
// be difficult to update.
rc.Query = p.Query
}
if p.Query != rc.Query {
return &gqlerror.Error{
Err: permission.Unauthorized(),
Message: "wrong query for API key",
Extensions: map[string]interface{}{
"code": "invalid_query",
},
}
}
return nil
}
|