/
service.go
58 lines (48 loc) · 1.41 KB
/
service.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
package authsvc
import (
"context"
"errors"
)
var (
ErrAuthenticationFailed = errors.New("authentication failed")
)
type AuthService interface {
AddUser(ctx context.Context, userName, password string) (User, error)
GetUser(ctx context.Context, userName string) (User, error)
AddForbiddenDevice(ctx context.Context, userId string, deviceId int) error
Login(ctx context.Context, userName, password string) (User, error)
}
func NewAuthService(r Repository) AuthService {
return &authService{
r: r,
}
}
type authService struct {
r Repository
}
func (s *authService) AddUser(_ context.Context, userName, password string) (User, error) {
user, err := s.r.AddUser(userName, password)
if err != nil {
return user, err
}
// err = NscAddUser(user.UserName)
// if err != nil {
// // the logic here sucks (in production we would use a transactional system)
// s.r.DeleteUser(user.UserName)
// return User{}, err
// }
return user, nil
}
func (s authService) GetUser(_ context.Context, userName string) (User, error) {
return s.r.GetUser(userName)
}
func (s *authService) AddForbiddenDevice(_ context.Context, userId string, deviceId int) error {
return s.r.AddForbiddenDevice(userId, deviceId)
}
func (s authService) Login(_ context.Context, userName, password string) (User, error) {
user, err := s.r.FindUser(userName, password)
if err != nil {
return User{}, ErrAuthenticationFailed
}
return user, nil
}