-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
64 lines (57 loc) · 1.56 KB
/
auth.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
package data
import (
"context"
"github.com/go-kratos/kratos/v2/log"
pb "github.com/kdimtricp/aical/api/auth/v1"
"github.com/kdimtricp/aical/internal/biz"
"time"
)
type authRepo struct {
data *Data
log *log.Helper
}
func NewAuthRepo(data *Data, logger log.Logger) biz.AuthRepo {
return &authRepo{
data: data,
log: log.NewHelper(logger),
}
}
func (ar *authRepo) SetState(_ context.Context, ad *biz.AuthData, duration time.Duration) (*biz.AuthData, error) {
ar.log.Debug("State data")
if err := ar.data.cache.Set(ad.State, ad.UserId, duration).Err(); err != nil {
return nil, err
}
if err := ar.data.cache.Set(ad.UserId, ad.State, duration).Err(); err != nil {
return nil, err
}
return ad, nil
}
func (ar *authRepo) CheckState(_ context.Context, ad *biz.AuthData) (*biz.AuthData, error) {
ar.log.Debug("Callback data")
tx := ar.data.cache.Get(ad.State)
if tx == nil {
ar.log.Error("CallbackStateCheck data: chat id not found")
return nil, pb.ErrorStateNotFound("chat id not found: %s", ad.State)
}
if tx.Err() != nil {
return nil, tx.Err()
}
chatID := tx.Val()
tx = ar.data.cache.Get(chatID)
if tx == nil {
ar.log.Error("CallbackStateCheck data: state not found")
return nil, pb.ErrorStateNotFound("state not found: %s", ad.State)
}
if tx.Err() != nil {
return nil, tx.Err()
}
state := tx.Val()
if ad.State != state {
ar.log.Error("CallbackStateCheck data: state not match")
return nil, pb.ErrorStateNotMatch("state not match: req[%s] check [%s]", ad.State, state)
}
return &biz.AuthData{
State: state,
UserId: chatID,
}, nil
}