storj/satellite/console/consoleweb/consoleql/token.go

53 lines
1.3 KiB
Go
Raw Normal View History

2019-01-24 16:26:36 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
2018-11-22 10:38:58 +00:00
// See LICENSE for copying information.
package consoleql
2018-11-22 10:38:58 +00:00
import (
"github.com/graphql-go/graphql"
"storj.io/storj/pkg/auth"
"storj.io/storj/satellite/console"
2018-11-22 10:38:58 +00:00
)
const (
2019-01-24 16:26:36 +00:00
// TokenType is graphql type name for token
TokenType = "token"
2018-11-22 10:38:58 +00:00
)
// graphqlToken creates *graphql.Object type that encapsulates user and token string
2019-04-04 15:56:20 +01:00
func graphqlToken(service *console.Service, types *TypeCreator) *graphql.Object {
2018-11-22 10:38:58 +00:00
return graphql.NewObject(graphql.ObjectConfig{
2019-01-24 16:26:36 +00:00
Name: TokenType,
2018-11-22 10:38:58 +00:00
Fields: graphql.Fields{
2019-01-24 16:26:36 +00:00
TokenType: &graphql.Field{
2018-11-22 10:38:58 +00:00
Type: graphql.String,
},
2019-01-24 16:26:36 +00:00
UserType: &graphql.Field{
2019-04-04 15:56:20 +01:00
Type: types.user,
2018-11-22 10:38:58 +00:00
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
wrapper, _ := p.Source.(tokenWrapper)
ctx := auth.WithAPIKey(p.Context, []byte(wrapper.Token))
auth, err := service.Authorize(ctx)
if err != nil {
return nil, HandleError(err)
}
// pass context to root value so child resolvers could get auth auth
2018-11-22 10:38:58 +00:00
rootValue := p.Info.RootValue.(map[string]interface{})
rootValue["context"] = console.WithAuth(ctx, auth)
2018-11-22 10:38:58 +00:00
return &auth.User, nil
2018-11-22 10:38:58 +00:00
},
},
},
})
}
// tokenWrapper holds token string value so it can be parsed by graphql pkg
type tokenWrapper struct {
Token string
}