-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_usecase.go
85 lines (73 loc) · 2.07 KB
/
token_usecase.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
package usecases
import (
"strings"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/hashicorp/go-multierror"
"github.com/pkg/errors"
"github.com/thewizardplusplus/go-exercises-backend/entities"
)
// UserGetter ...
type UserGetter interface {
GetUser(username string) (entities.User, error)
}
// Clock ...
type Clock func() time.Time
// TokenUsecase ...
type TokenUsecase struct {
TokenSigningKey string
TokenTTL time.Duration
UserGetter UserGetter
Clock Clock
}
// CreateToken ...
func (usecase TokenUsecase) CreateToken(
user entities.User,
) (
entities.Credentials,
error,
) {
foundUser, err := usecase.UserGetter.GetUser(user.Username)
if err != nil {
return entities.Credentials{}, errors.Wrap(err, "unable to get the user")
}
if foundUser.IsDisabled != nil && *foundUser.IsDisabled {
return entities.Credentials{}, entities.ErrUserIsDisabled
}
if err := user.CheckPassword(foundUser.PasswordHash); err != nil {
return entities.Credentials{}, err
}
foundUser.PasswordHash = ""
tokenExpirationTime := usecase.Clock().Add(usecase.TokenTTL).Unix()
token := jwt.NewWithClaims(jwt.SigningMethodHS256, entities.AccessTokenClaims{
StandardClaims: jwt.StandardClaims{ExpiresAt: tokenExpirationTime},
User: foundUser,
})
signedToken, err := token.SignedString([]byte(usecase.TokenSigningKey))
if err != nil {
return entities.Credentials{}, errors.Wrap(err, "unable to create the token")
}
credentials := entities.Credentials{AccessToken: signedToken}
return credentials, nil
}
// ParseToken ...
func (usecase TokenUsecase) ParseToken(
authorizationHeader string,
) (
*entities.AccessTokenClaims,
error,
) {
tokenAsStr := strings.TrimPrefix(authorizationHeader, "Bearer ")
token, err := jwt.ParseWithClaims(
tokenAsStr,
&entities.AccessTokenClaims{},
func(token *jwt.Token) (interface{}, error) {
return []byte(usecase.TokenSigningKey), nil
},
)
if err != nil {
return nil, multierror.Append(err, entities.ErrFailedTokenChecking)
}
tokenClaims := token.Claims.(*entities.AccessTokenClaims)
return tokenClaims, nil
}