|
1
|
|
|
package jwt |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"strings" |
|
5
|
|
|
|
|
6
|
|
|
"github.com/golang-jwt/jwt/v4" |
|
7
|
|
|
"github.com/memnix/memnix-rest/config" |
|
8
|
|
|
"github.com/memnix/memnix-rest/pkg/utils" |
|
9
|
|
|
) |
|
10
|
|
|
|
|
11
|
|
|
// GenerateToken generates a jwt token from a user id |
|
12
|
|
|
// and returns the token and an error |
|
13
|
|
|
// |
|
14
|
|
|
// It's signing method is defined in utils.JwtSigningMethod |
|
15
|
|
|
// It's expiration time is defined in utils.GetExpirationTime |
|
16
|
|
|
// It's secret key is defined in the environment variable SECRET_KEY |
|
17
|
|
|
// see: utils/config.go for more information |
|
18
|
|
|
func GenerateToken(userID uint) (string, error) { |
|
19
|
|
|
// Create the Claims for the token |
|
20
|
|
|
claims := jwt.NewWithClaims(config.JwtSigningMethod, jwt.RegisteredClaims{ |
|
21
|
|
|
Issuer: utils.ConvertUIntToStr(userID), // Issuer is the user id |
|
22
|
|
|
ExpiresAt: utils.GetExpirationTime(), // ExpiresAt is the expiration time |
|
23
|
|
|
}) |
|
24
|
|
|
|
|
25
|
|
|
// Sign and get the complete encoded token as a string using the secret |
|
26
|
|
|
token, err := claims.SignedString([]byte(utils.GetSecretKey())) |
|
27
|
|
|
if err != nil { |
|
28
|
|
|
return "", err |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
return token, nil |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
// VerifyToken verifies a jwt token |
|
35
|
|
|
// and returns the user id and an error |
|
36
|
|
|
func VerifyToken(token *jwt.Token) (uint, error) { |
|
37
|
|
|
// claims is of type jwt.MapClaims |
|
38
|
|
|
claims := token.Claims.(jwt.MapClaims) |
|
39
|
|
|
// Get the issuer from the claims and convert it to uint |
|
40
|
|
|
userID, err := utils.ConvertStrToUInt(claims["iss"].(string)) |
|
41
|
|
|
if err != nil { |
|
42
|
|
|
return 0, err |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return userID, nil |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
// GetToken gets a jwt.Token token from a string |
|
49
|
|
|
// and returns the jwt.Token and an error |
|
50
|
|
|
func GetToken(token string) (*jwt.Token, error) { |
|
51
|
|
|
// Parse takes the token string and a function for looking up the key. |
|
52
|
|
|
return jwt.Parse(token, func(token *jwt.Token) (interface{}, error) { |
|
53
|
|
|
return []byte(utils.GetSecretKey()), nil // Return the secret key as the signing key |
|
54
|
|
|
}) |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
func GetExpirationTime(token *jwt.Token) int64 { |
|
58
|
|
|
claims := token.Claims.(jwt.MapClaims) |
|
59
|
|
|
return int64(claims["exp"].(float64)) |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// extractToken function to extract token from header |
|
63
|
|
|
func extractToken(token string) string { |
|
64
|
|
|
// Normally Authorization HTTP header. |
|
65
|
|
|
onlyToken := strings.Split(token, " ") // Split token |
|
66
|
|
|
if len(onlyToken) == config.JwtTokenHeaderLen { |
|
67
|
|
|
return onlyToken[1] // Return only token |
|
68
|
|
|
} |
|
69
|
|
|
return "" // Return empty string |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
func GetConnectedUserID(tokenHeader string) (uint, error) { |
|
73
|
|
|
// Get the token from the Authorization header |
|
74
|
|
|
tokenString := extractToken(tokenHeader) |
|
75
|
|
|
|
|
76
|
|
|
token, err := GetToken(tokenString) |
|
77
|
|
|
if err != nil { |
|
78
|
|
|
return 0, err |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// Check if the token is valid |
|
82
|
|
|
userID, err := VerifyToken(token) |
|
83
|
|
|
if err != nil { |
|
84
|
|
|
return 0, err |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return userID, nil |
|
88
|
|
|
} |
|
89
|
|
|
|