forked from manabie-com/togo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo.go
152 lines (124 loc) · 3.18 KB
/
todo.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package services
import (
"context"
"fmt"
"github.com/banhquocdanh/togo/internal/cache"
"github.com/banhquocdanh/togo/internal/config"
"github.com/banhquocdanh/togo/internal/storages"
"github.com/dgrijalva/jwt-go"
"time"
"github.com/google/uuid"
)
var Now = time.Now
// ToDoService implement HTTserver
type ToDoService struct {
config *config.Config
Store storages.StoreInterface
Cache cache.Cache
}
type Option func(service *ToDoService)
func NewToDoService(opts ...Option) *ToDoService {
srv := &ToDoService{}
for _, opt := range opts {
opt(srv)
}
return srv
}
func WithConfig(cfg *config.Config) Option {
return func(srv *ToDoService) {
srv.config = cfg
}
}
func WithStore(db storages.StoreInterface) Option {
return func(srv *ToDoService) {
srv.Store = db
}
}
func WithCache(cache cache.Cache) Option {
return func(srv *ToDoService) {
srv.Cache = cache
}
}
func (s *ToDoService) ListTasks(ctx context.Context, userID, createDate string) ([]*storages.Task, error) {
if userID == "" {
return nil, fmt.Errorf("user id invalid")
}
if createDate == "" {
return nil, fmt.Errorf("created date invalid")
}
return s.Store.RetrieveTasks(
ctx,
userID,
createDate,
)
}
func (s *ToDoService) AddTask(ctx context.Context, userID, content string) (*storages.Task, error) {
if userID == "" {
return nil, fmt.Errorf("invalid userID")
}
if content == "" {
return nil, fmt.Errorf("invalid task's content")
}
now := Now()
t := &storages.Task{
ID: uuid.New().String(),
Content: content,
UserID: userID,
CreatedDate: now.Format("2006-01-02"),
}
return t, s.Store.AddTask(ctx, t)
}
func (s *ToDoService) validateUser(ctx context.Context, userID, pw string) bool {
if userID == "" || pw == "" {
return false
}
return s.Store.ValidateUser(ctx, userID, pw)
}
func (s *ToDoService) Login(ctx context.Context, userID, pw string, jwtKey string) (string, error) {
if userID == "" || pw == "" {
return "", fmt.Errorf("invalid user/pw")
}
if !s.validateUser(ctx, userID, pw) {
return "", fmt.Errorf("user/pw is incorrect")
}
token, err := s.createToken(userID, jwtKey)
if err != nil {
return token, nil
}
return token, s.Cache.AddToken(ctx, token, s.config.TokenTIL())
}
func (s *ToDoService) createToken(id string, jwtKey string) (string, error) {
atClaims := jwt.MapClaims{}
atClaims["user_id"] = id
atClaims["exp"] = Now().Add(s.config.TokenTIL()).Unix()
at := jwt.NewWithClaims(jwt.SigningMethodHS256, atClaims)
token, err := at.SignedString([]byte(jwtKey))
if err != nil {
return "", err
}
return token, nil
}
func (s *ToDoService) ValidToken(token, jwtKey string) (string, error) {
if token == "" {
return "", fmt.Errorf("token is empty")
}
claims := make(jwt.MapClaims)
t, err := jwt.ParseWithClaims(token, claims, func(*jwt.Token) (interface{}, error) {
return []byte(jwtKey), nil
})
if err != nil {
return "", err
}
if !t.Valid {
return "", fmt.Errorf("invalid token")
}
id, ok := claims["user_id"].(string)
if !ok {
return "", fmt.Errorf("not found userID")
}
_, ok = claims["exp"]
if !ok {
return "", fmt.Errorf("not found expired time")
}
return id, s.Cache.ValidateToken(context.Background(), token)
}