@rivascva/dt-idl 1.1.106 → 1.1.108
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/go/auth/jwt.go +36 -14
- package/go/request/auth_transport.go +1 -7
- package/package.json +1 -1
package/go/auth/jwt.go
CHANGED
|
@@ -2,12 +2,14 @@ package auth
|
|
|
2
2
|
|
|
3
3
|
import (
|
|
4
4
|
"fmt"
|
|
5
|
+
"strings"
|
|
5
6
|
"time"
|
|
6
7
|
|
|
7
8
|
"github.com/golang-jwt/jwt/v5"
|
|
8
9
|
)
|
|
9
10
|
|
|
10
11
|
// ValidateToken validates the given JWT token using the provided secret.
|
|
12
|
+
// It includes a check for the token expiration time.
|
|
11
13
|
func ValidateToken(token string, secret string) (*jwt.Token, error) {
|
|
12
14
|
// create a parser with the HS256 signing method
|
|
13
15
|
parser := jwt.NewParser(jwt.WithValidMethods([]string{jwt.SigningMethodHS256.Alg()}))
|
|
@@ -31,57 +33,77 @@ func ValidateToken(token string, secret string) (*jwt.Token, error) {
|
|
|
31
33
|
}
|
|
32
34
|
|
|
33
35
|
// NewUserAccessToken creates a new JWT access token for the given user id.
|
|
34
|
-
func NewUserAccessToken(accessTokenSecret string, issuer string, userId string, duration time.Duration) (
|
|
36
|
+
func NewUserAccessToken(accessTokenSecret string, issuer string, userId string, duration time.Duration) (*jwt.Token, error) {
|
|
35
37
|
return newAccessToken(accessTokenSecret, "user", issuer, userId, duration)
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
// NewServiceAccessToken creates a new JWT access token for the given service.
|
|
39
|
-
func NewServiceAccessToken(accessTokenSecret string, issuer string, service string, duration time.Duration) (
|
|
41
|
+
func NewServiceAccessToken(accessTokenSecret string, issuer string, service string, duration time.Duration) (*jwt.Token, error) {
|
|
40
42
|
return newAccessToken(accessTokenSecret, "service", issuer, service, duration)
|
|
41
43
|
}
|
|
42
44
|
|
|
43
45
|
// NewUserRefreshToken creates a new JWT refresh token for the given user id.
|
|
44
|
-
func NewUserRefreshToken(refreshTokenSecret string, issuer string, userId string, duration time.Duration) (
|
|
46
|
+
func NewUserRefreshToken(refreshTokenSecret string, issuer string, userId string, duration time.Duration) (*jwt.Token, error) {
|
|
45
47
|
return newRefreshToken(refreshTokenSecret, issuer, userId, duration)
|
|
46
48
|
}
|
|
47
49
|
|
|
48
50
|
// NewServiceRefreshToken creates a new JWT refresh token for the given service.
|
|
49
|
-
func NewServiceRefreshToken(refreshTokenSecret string, issuer string, service string, duration time.Duration) (
|
|
51
|
+
func NewServiceRefreshToken(refreshTokenSecret string, issuer string, service string, duration time.Duration) (*jwt.Token, error) {
|
|
50
52
|
return newRefreshToken(refreshTokenSecret, issuer, service, duration)
|
|
51
53
|
}
|
|
52
54
|
|
|
53
55
|
// newAccessToken creates a new JWT access token.
|
|
54
|
-
func newAccessToken(accessTokenSecret string, ttype string, issuer string, subject string, duration time.Duration) (
|
|
56
|
+
func newAccessToken(accessTokenSecret string, ttype string, issuer string, subject string, duration time.Duration) (*jwt.Token, error) {
|
|
55
57
|
t := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
|
56
58
|
"type": ttype,
|
|
57
59
|
"iss": issuer,
|
|
58
60
|
"sub": subject,
|
|
59
|
-
"iat": time.Now()
|
|
60
|
-
"exp": time.Now().Add(duration)
|
|
61
|
+
"iat": jwt.NewNumericDate(time.Now()),
|
|
62
|
+
"exp": jwt.NewNumericDate(time.Now().Add(duration)),
|
|
61
63
|
})
|
|
62
64
|
|
|
63
65
|
s, err := t.SignedString([]byte(accessTokenSecret))
|
|
64
66
|
if err != nil {
|
|
65
|
-
return
|
|
67
|
+
return nil, fmt.Errorf("unable to sign the access token: %w", err)
|
|
66
68
|
}
|
|
67
69
|
|
|
68
|
-
|
|
70
|
+
parts := strings.Split(s, ".")
|
|
71
|
+
if len(parts) != 3 {
|
|
72
|
+
return nil, fmt.Errorf("invalid raw access token")
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
token, err := ValidateToken(s, accessTokenSecret)
|
|
76
|
+
if err != nil {
|
|
77
|
+
return nil, fmt.Errorf("unable to validate the new raw access token: %w", err)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return token, nil
|
|
69
81
|
}
|
|
70
82
|
|
|
71
83
|
// newRefreshToken creates a new JWT refresh token.
|
|
72
|
-
func newRefreshToken(refreshTokenSecret string, issuer string, subject string, duration time.Duration) (
|
|
84
|
+
func newRefreshToken(refreshTokenSecret string, issuer string, subject string, duration time.Duration) (*jwt.Token, error) {
|
|
73
85
|
t := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
|
74
86
|
"type": "refresh",
|
|
75
87
|
"iss": issuer,
|
|
76
88
|
"sub": subject,
|
|
77
|
-
"iat": time.Now()
|
|
78
|
-
"exp": time.Now().Add(duration)
|
|
89
|
+
"iat": jwt.NewNumericDate(time.Now()),
|
|
90
|
+
"exp": jwt.NewNumericDate(time.Now().Add(duration)),
|
|
79
91
|
})
|
|
80
92
|
|
|
81
93
|
s, err := t.SignedString([]byte(refreshTokenSecret))
|
|
82
94
|
if err != nil {
|
|
83
|
-
return
|
|
95
|
+
return nil, fmt.Errorf("unable to sign the refresh token: %w", err)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
parts := strings.Split(s, ".")
|
|
99
|
+
if len(parts) != 3 {
|
|
100
|
+
return nil, fmt.Errorf("invalid raw refresh token")
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
token, err := ValidateToken(s, refreshTokenSecret)
|
|
104
|
+
if err != nil {
|
|
105
|
+
return nil, fmt.Errorf("unable to validate the new raw refresh token: %w", err)
|
|
84
106
|
}
|
|
85
107
|
|
|
86
|
-
return
|
|
108
|
+
return token, nil
|
|
87
109
|
}
|
|
@@ -83,17 +83,11 @@ func (t *AuthTransport) setNewAccessToken() error {
|
|
|
83
83
|
defer t.mu.Unlock()
|
|
84
84
|
|
|
85
85
|
// create a new service access token
|
|
86
|
-
|
|
86
|
+
accessToken, err := auth.NewServiceAccessToken(t.accessTokenSecret, t.service, t.service, auth.DefaultServiceAccessTokenDuration)
|
|
87
87
|
if err != nil {
|
|
88
88
|
return fmt.Errorf("failed to create a new service access token: %w", err)
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
-
// validate the access token
|
|
92
|
-
accessToken, err := auth.ValidateToken(rawAccessToken, t.accessTokenSecret)
|
|
93
|
-
if err != nil {
|
|
94
|
-
return fmt.Errorf("failed to validate the new access token: %w", err)
|
|
95
|
-
}
|
|
96
|
-
|
|
97
91
|
// set the new access token
|
|
98
92
|
t.accessToken = accessToken
|
|
99
93
|
|