moved db indices to uint
This commit is contained in:
@@ -17,7 +17,7 @@ import (
|
||||
)
|
||||
|
||||
type Session struct {
|
||||
UserID int64
|
||||
UserID uint
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
@@ -28,27 +28,27 @@ var (
|
||||
sessions = make(map[string]*Session)
|
||||
)
|
||||
|
||||
func verifyAndRenewToken(tokenString string) (string, int64, error) {
|
||||
func verifyAndRenewToken(tokenString string) (string, uint, error) {
|
||||
if tokenString == "" {
|
||||
logger.Error.Printf("empty tokenstring")
|
||||
return "", -1, fmt.Errorf("Authorization token is required")
|
||||
return "", 0, fmt.Errorf("Authorization token is required")
|
||||
}
|
||||
token, claims, err := ExtractContentFrom(tokenString)
|
||||
if err != nil {
|
||||
logger.Error.Printf("Couldn't parse JWT token String: %v", err)
|
||||
return "", -1, err
|
||||
return "", 0, err
|
||||
}
|
||||
sessionID := (*claims)["session_id"].(string)
|
||||
userID := int64((*claims)["user_id"].(float64))
|
||||
userID := uint((*claims)["user_id"].(float64))
|
||||
roleID := int8((*claims)["role_id"].(float64))
|
||||
|
||||
session, ok := sessions[sessionID]
|
||||
if !ok {
|
||||
logger.Error.Printf("session not found")
|
||||
return "", -1, fmt.Errorf("session not found")
|
||||
return "", 0, fmt.Errorf("session not found")
|
||||
}
|
||||
if userID != session.UserID {
|
||||
return "", -1, fmt.Errorf("Cookie has been altered, aborting..")
|
||||
return "", 0, fmt.Errorf("Cookie has been altered, aborting..")
|
||||
}
|
||||
if token.Valid {
|
||||
// token is valid, so we can return the old tokenString
|
||||
@@ -58,7 +58,7 @@ func verifyAndRenewToken(tokenString string) (string, int64, error) {
|
||||
if time.Now().After(sessions[sessionID].ExpiresAt) {
|
||||
delete(sessions, sessionID)
|
||||
logger.Error.Printf("session expired")
|
||||
return "", -1, fmt.Errorf("session expired")
|
||||
return "", 0, fmt.Errorf("session expired")
|
||||
}
|
||||
session.ExpiresAt = time.Now().Add(sessionDuration)
|
||||
|
||||
@@ -67,7 +67,7 @@ func verifyAndRenewToken(tokenString string) (string, int64, error) {
|
||||
user := models.User{ID: userID, RoleID: roleID}
|
||||
newTokenString, err := GenerateToken(config.Auth.JWTSecret, &user, sessionID)
|
||||
if err != nil {
|
||||
return "", -1, err
|
||||
return "", 0, err
|
||||
}
|
||||
|
||||
return newTokenString, session.UserID, nil
|
||||
@@ -86,7 +86,7 @@ func AuthMiddleware() gin.HandlerFunc {
|
||||
newToken, userID, err := verifyAndRenewToken(tokenString)
|
||||
if err != nil {
|
||||
if err == customerrors.ErrValidToken {
|
||||
c.Set("user_id", int64(userID))
|
||||
c.Set("user_id", uint(userID))
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
@@ -97,7 +97,7 @@ func AuthMiddleware() gin.HandlerFunc {
|
||||
}
|
||||
|
||||
utils.SetCookie(c, newToken)
|
||||
c.Set("user_id", int64(userID))
|
||||
c.Set("user_id", uint(userID))
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -141,7 +141,7 @@ func ExtractContentFrom(tokenString string) (*jwt.Token, *jwt.MapClaims, error)
|
||||
return token, &claims, nil
|
||||
}
|
||||
|
||||
func UpdateSession(sessionID string, userID int64) {
|
||||
func UpdateSession(sessionID string, userID uint) {
|
||||
sessions[sessionID] = &Session{
|
||||
UserID: userID,
|
||||
ExpiresAt: time.Now().Add(sessionDuration),
|
||||
|
||||
Reference in New Issue
Block a user