-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
65 lines (54 loc) · 1.58 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
65
package service
import (
"errors"
"github.com/dgrijalva/jwt-go"
"github.com/p12s/wildberries-http-api"
"github.com/p12s/wildberries-http-api/pkg/repository"
"github.com/spf13/viper"
"time"
)
const tokenTTL = 12 * time.Hour
type AuthService struct {
repo repository.Authorization
}
func NewAuthService(repo repository.Authorization) *AuthService {
return &AuthService{repo: repo}
}
func (s *AuthService) CreateUser(user common.User) (int, error) {
user.Password = GeneratePasswordHash(user.Password)
return s.repo.CreateUser(user)
}
type tokenClaims struct {
jwt.StandardClaims
IdUser int `json:"id_user"`
}
func (s *AuthService) GenerateToken(username, password string) (string, error) {
user, err := s.repo.GetUser(username, GeneratePasswordHash(password))
if err != nil {
return "", err
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, &tokenClaims{
jwt.StandardClaims{
ExpiresAt: time.Now().Add(tokenTTL).Unix(),
IssuedAt: time.Now().Unix(),
},
user.Id,
})
return token.SignedString([]byte(viper.GetString("db.signingKey")))
}
func (s *AuthService) ParseToken(accessToken string) (int, error) {
token, err := jwt.ParseWithClaims(accessToken, &tokenClaims{}, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, errors.New("invalid signing method")
}
return []byte(viper.GetString("db.signingKey")), nil
})
if err != nil {
return 0, err
}
claims, ok := token.Claims.(*tokenClaims)
if !ok {
return 0, errors.New("token claims are not of type *token")
}
return claims.IdUser, nil
}