Skip to content

Commit

Permalink
Refactorized Token Generation to add Parameterized TTL
Browse files Browse the repository at this point in the history
  • Loading branch information
Ju-Wiluis William Rodriguez Hernandez committed Sep 23, 2024
1 parent 916ab65 commit 1799636
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
2 changes: 1 addition & 1 deletion handlers/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
}

// Generate JWT token and save to Redis
token, err := redis.GenerateToken(userId)
token, err := redis.GenerateToken(credentials.Organization, userId)
if err != nil {
println("Error generating token: ", err.Error())
writeJsonError(w, "Error generating token", http.StatusInternalServerError)
Expand Down
10 changes: 1 addition & 9 deletions handlers/renew.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,7 @@ func RenewHandler(w http.ResponseWriter, r *http.Request) {
return
}

// Validate token against Redis
userId, err := redis.ValidateToken(token)
if err != nil {
println("Error validating token: ", err.Error())
writeJsonError(w, "Invalid token", http.StatusUnauthorized)
return
}

token, err = redis.RenewToken(token, userId)
token, err := redis.RenewToken(token)
if err != nil {
println("Error renewing token: ", err.Error())
writeJsonError(w, "Invalid token", http.StatusConflict)
Expand Down
31 changes: 25 additions & 6 deletions redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"os"
"strings"
"sync"
"time"

Expand All @@ -25,23 +26,41 @@ func GetInstance() *redis.Client {
return redisClient
}

func GenerateToken(userId string) (string, error) {
func GenerateToken(orgId, userId string) (string, error) {
token, _ := auth.GenerateOpaqueToken(32)
err := GetInstance().Set(context.Background(), fmt.Sprintf("auth_token:%s", token), userId, 5*time.Minute).Err()
serializedData := orgId + "," + userId
// Almacenar el token con la data serializada
err := GetInstance().Set(context.Background(), fmt.Sprintf("auth_token:%s", token), string(serializedData), GetDefaultTTL(orgId, userId)).Err()
if err != nil {
return "", err
}
err2 := GetInstance().Set(context.Background(), fmt.Sprintf("auth_user:%s", userId), token, 5*time.Minute).Err()

// Asociar el token con el userId
err2 := GetInstance().Set(context.Background(), fmt.Sprintf("auth_user:%s", userId), token, GetDefaultTTL(orgId, userId)).Err()
if err2 != nil {
GetInstance().Del(context.Background(), fmt.Sprintf("auth_token:%s", token))
return "", err2
}

return token, nil
}

func RenewToken(token string, userId string) (string, error) {
DeleteToken(token, userId)
return GenerateToken(userId)
func GetDefaultTTL(orgId, userId string) time.Duration {
val, err := GetInstance().Get(context.Background(), fmt.Sprintf("user_ttl:%s", userId)).Int64()
if err != nil {
val, err = GetInstance().Get(context.Background(), fmt.Sprintf("app_ttl:%s", orgId)).Int64()
if err != nil {
return 5 * time.Minute
}
}
return time.Duration(val) * time.Minute
}

func RenewToken(token string) (string, error) {
data, _ := ValidateToken(token)
splited := strings.Split(data, ",")
DeleteToken(token, splited[1])
return GenerateToken(splited[0], splited[1])
}

func DeleteToken(token string, userId string) {
Expand Down

0 comments on commit 1799636

Please sign in to comment.