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.
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
package consoleql
|
2018-11-22 10:38:58 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/graphql-go/graphql"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/auth"
|
2019-01-15 13:03:24 +00:00
|
|
|
"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))
|
2018-11-27 14:20:58 +00:00
|
|
|
|
|
|
|
auth, err := service.Authorize(ctx)
|
|
|
|
if err != nil {
|
2019-09-04 16:02:39 +01:00
|
|
|
return nil, HandleError(err)
|
2018-11-27 14:20:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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{})
|
2019-01-15 13:03:24 +00:00
|
|
|
rootValue["context"] = console.WithAuth(ctx, auth)
|
2018-11-22 10:38:58 +00:00
|
|
|
|
2018-11-27 14:20: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
|
|
|
|
}
|