-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolver.go
99 lines (85 loc) · 2.57 KB
/
resolver.go
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package pub
import (
"context"
"errors"
gauthConf "github.com/hiroaki-yamamoto/gauth/config"
gauth "github.com/hiroaki-yamamoto/gauth/core"
gauthMid "github.com/hiroaki-yamamoto/gauth/middleware"
"github.com/hiroaki-yamamoto/go-gql-sample/backend/hash"
"github.com/hiroaki-yamamoto/go-gql-sample/backend/prisma"
)
// Resolver is a struct for pub-api reolver
type Resolver struct {
Db *prisma.Client
TokConf *gauthConf.Config
}
// PubM indicates a public mutation
func (r *Resolver) PubM() PubMResolver {
return &pubMResolver{r}
}
// PubQ indicates a public query
func (r *Resolver) PubQ() PubQResolver {
return &pubQResolver{r}
}
// Subscription indicates a public subscription (what??)
func (r *Resolver) Subscription() SubscriptionResolver {
return &subscriptionResolver{r}
}
type pubMResolver struct{ *Resolver }
func (r *pubMResolver) Login(
ctx context.Context,
username string,
password string,
) (SessionAndStatus, error) {
if gauthMid.GetUser(ctx) != nil {
return nil, errors.New("You are already logged in")
}
pwHash := hash.Argon2(password, username)
users, err := r.Db.Users(&prisma.UsersParams{
Where: &prisma.UserWhereInput{
Username: &username,
Password: &pwHash,
},
}).Exec(context.TODO())
if err != nil {
return nil, err
}
if len(users) < 1 {
return nil, errors.New("Username and / or password are mismatch")
}
// In the most case, this code is enought to tokenize:
tok, err := gauth.ComposeID(users[0].ID, r.TokConf)
// However, if you want to use in advanced mode,
// ComposeToken is also useful.
// now := time.Now().UTC()
// tok, err := gauth.ComposeToken(&jwt.JWT{
// Issuer: r.TokConf.Issuer,
// Subject: r.TokConf.Subject,
// Audience: r.TokConf.Audience,
// ExpirationTime: now.Add(2 * time.Hour).Unix(),
// IssuedAt: now.Unix(),
// ID: users[0].Username,
// }, r.TokConf.Signer)
if err != nil {
return nil, err
}
return Session{string(tok)}, nil
}
func (r *pubMResolver) Signup(
ctx context.Context,
username string,
password string,
) (UserAndStatus, error) {
return r.Db.CreateUser(prisma.UserCreateInput{
Username: username,
Password: hash.Argon2(password, username),
}).Exec(context.TODO())
}
type pubQResolver struct{ *Resolver }
func (r *pubQResolver) Country(ctx context.Context) ([]*string, error) {
return nil, errors.New("not implemented")
}
type subscriptionResolver struct{ *Resolver }
func (r *subscriptionResolver) User(ctx context.Context, where *UserSubscriptionWhereInput) (<-chan *UserSubscriptionPayload, error) {
return nil, errors.New("not implemented")
}