-
Notifications
You must be signed in to change notification settings - Fork 35
/
verify_password.go
69 lines (63 loc) 路 1.66 KB
/
verify_password.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
package utils
import (
"context"
"github.com/buger/jsonparser"
"github.com/dgraph-io/dgo/v230"
"github.com/dgraph-io/dgo/v230/protos/api"
"github.com/rs/zerolog/log"
"go.elastic.co/apm/module/apmgrpc/v2"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func VerifyPassword(uid string, password string) (bool, error) {
config := GetConfig()
conn, err := grpc.Dial(
config.DgraphHost+":"+config.DgraphGRPCPort,
grpc.WithUnaryInterceptor(apmgrpc.NewUnaryClientInterceptor()),
grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
log.Error().Err(err).Msg("grpc.Dial")
return false, err
}
defer func(conn *grpc.ClientConn) {
err := conn.Close()
if err != nil {
log.Error().Err(err).Msg("conn.Close")
}
}(conn)
dgraphClient := dgo.NewDgraphClient(api.NewDgraphClient(conn))
ctx := context.Background()
txn := dgraphClient.NewTxn()
defer func(txn *dgo.Txn, ctx context.Context) {
err := txn.Discard(ctx)
if err != nil {
log.Error().Err(err).Msg("txn.Discard")
}
}(txn, ctx)
q := `query VerifyPassword($uid: string, $password: string) {
verifyPassword(func: uid($uid)) {
checkpwd(password, $password)
}
}`
req := &api.Request{
Query: q,
Vars: map[string]string{
"$uid": uid,
"$password": password,
},
}
res, err := txn.Do(ctx, req)
if err != nil {
log.Error().Err(err).Msg("txn.Do")
return false, err
}
isPasswordValid, err := jsonparser.GetBoolean(res.Json, "verifyPassword", "[0]", "checkpwd(password)")
if err != nil {
log.Error().
Err(err).
Bytes("res.Json", res.Json).
Msg("jsonparser.GetString")
return false, err
}
return isPasswordValid, nil
}