2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-15 12:00:08 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
package consoleql
|
2018-11-14 10:50:15 +00:00
|
|
|
|
|
|
|
import (
|
2019-04-10 20:16:10 +01:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2018-11-14 10:50:15 +00:00
|
|
|
"github.com/graphql-go/graphql"
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
2018-12-13 20:01:43 +00:00
|
|
|
|
2019-04-10 20:16:10 +01:00
|
|
|
"storj.io/storj/internal/post"
|
2019-01-15 13:03:24 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2019-04-10 20:16:10 +01:00
|
|
|
"storj.io/storj/satellite/mailservice"
|
2018-11-14 10:50:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Query is immutable graphql request
|
|
|
|
Query = "query"
|
2019-01-24 16:26:36 +00:00
|
|
|
// UserQuery is a query name for user
|
|
|
|
UserQuery = "user"
|
|
|
|
// ProjectQuery is a query name for project
|
|
|
|
ProjectQuery = "project"
|
|
|
|
// MyProjectsQuery is a query name for projects related to account
|
|
|
|
MyProjectsQuery = "myProjects"
|
|
|
|
// TokenQuery is a query name for token
|
|
|
|
TokenQuery = "token"
|
2019-04-10 20:16:10 +01:00
|
|
|
// ForgotPasswordQuery is a query name for password recovery request
|
|
|
|
ForgotPasswordQuery = "forgotPassword"
|
2018-11-14 10:50:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// rootQuery creates query for graphql populated by AccountsClient
|
2019-04-10 20:16:10 +01:00
|
|
|
func rootQuery(service *console.Service, mailService *mailservice.Service, types *TypeCreator) *graphql.Object {
|
2018-11-14 10:50:15 +00:00
|
|
|
return graphql.NewObject(graphql.ObjectConfig{
|
|
|
|
Name: Query,
|
|
|
|
Fields: graphql.Fields{
|
2019-01-24 16:26:36 +00:00
|
|
|
UserQuery: &graphql.Field{
|
2019-04-04 15:56:20 +01:00
|
|
|
Type: types.user,
|
2018-11-14 10:50:15 +00:00
|
|
|
Args: graphql.FieldConfigArgument{
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldID: &graphql.ArgumentConfig{
|
2018-12-11 15:54:45 +00:00
|
|
|
Type: graphql.String,
|
2018-11-14 10:50:15 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2019-01-24 16:26:36 +00:00
|
|
|
id, err := uuidIDAuthFallback(p, FieldID)
|
2018-11-14 10:50:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-11 15:54:45 +00:00
|
|
|
return service.GetUser(p.Context, *id)
|
2018-11-26 10:47:23 +00:00
|
|
|
},
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
ProjectQuery: &graphql.Field{
|
2019-04-04 15:56:20 +01:00
|
|
|
Type: types.project,
|
2018-11-26 10:47:23 +00:00
|
|
|
Args: graphql.FieldConfigArgument{
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldID: &graphql.ArgumentConfig{
|
2018-11-26 10:47:23 +00:00
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2019-01-24 16:26:36 +00:00
|
|
|
inputID, _ := p.Args[FieldID].(string)
|
2018-11-26 10:47:23 +00:00
|
|
|
|
|
|
|
id, err := uuid.Parse(inputID)
|
2018-11-14 10:50:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-26 10:47:23 +00:00
|
|
|
return service.GetProject(p.Context, *id)
|
|
|
|
},
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
MyProjectsQuery: &graphql.Field{
|
2019-04-04 15:56:20 +01:00
|
|
|
Type: graphql.NewList(types.project),
|
2018-11-26 10:47:23 +00:00
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
return service.GetUsersProjects(p.Context)
|
2018-11-14 10:50:15 +00:00
|
|
|
},
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
TokenQuery: &graphql.Field{
|
2019-04-04 15:56:20 +01:00
|
|
|
Type: types.token,
|
2018-11-14 10:50:15 +00:00
|
|
|
Args: graphql.FieldConfigArgument{
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldEmail: &graphql.ArgumentConfig{
|
2018-11-14 10:50:15 +00:00
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
2019-01-24 16:26:36 +00:00
|
|
|
FieldPassword: &graphql.ArgumentConfig{
|
2018-11-14 10:50:15 +00:00
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
2019-01-24 16:26:36 +00:00
|
|
|
email, _ := p.Args[FieldEmail].(string)
|
|
|
|
pass, _ := p.Args[FieldPassword].(string)
|
2018-11-14 10:50:15 +00:00
|
|
|
|
2018-11-21 15:51:43 +00:00
|
|
|
token, err := service.Token(p.Context, email, pass)
|
2018-11-14 10:50:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-22 10:38:58 +00:00
|
|
|
return tokenWrapper{Token: token}, nil
|
2018-11-14 10:50:15 +00:00
|
|
|
},
|
|
|
|
},
|
2019-04-10 20:16:10 +01:00
|
|
|
ForgotPasswordQuery: &graphql.Field{
|
|
|
|
Type: graphql.Boolean,
|
|
|
|
Args: graphql.FieldConfigArgument{
|
|
|
|
FieldEmail: &graphql.ArgumentConfig{
|
|
|
|
Type: graphql.NewNonNull(graphql.String),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
|
|
|
|
email, _ := p.Args[FieldEmail].(string)
|
|
|
|
|
|
|
|
user, err := service.GetUserByEmail(p.Context, email)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("%s is not found", email)
|
|
|
|
}
|
|
|
|
|
|
|
|
recoveryToken, err := service.GeneratePasswordRecoveryToken(p.Context, user.ID, user.Email)
|
|
|
|
if err != nil {
|
|
|
|
return false, errors.New("failed to generate password recovery token")
|
|
|
|
}
|
|
|
|
|
|
|
|
rootObject := p.Info.RootValue.(map[string]interface{})
|
|
|
|
origin := rootObject["origin"].(string)
|
|
|
|
link := origin + rootObject[PasswordRecoveryPath].(string) + recoveryToken
|
|
|
|
userName := user.ShortName
|
|
|
|
if user.ShortName == "" {
|
|
|
|
userName = user.FullName
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: think of a better solution
|
|
|
|
go func() {
|
|
|
|
_ = mailService.SendRendered(
|
|
|
|
p.Context,
|
|
|
|
[]post.Address{{Address: user.Email, Name: userName}},
|
|
|
|
&ForgotPasswordEmail{
|
|
|
|
Origin: origin,
|
|
|
|
ResetLink: link,
|
|
|
|
UserName: userName,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
},
|
|
|
|
},
|
2018-11-14 10:50:15 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|