Skip to content

Commit

Permalink
Added TTL to TokenResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
Ju-Wiluis William Rodriguez Hernandez committed Jul 15, 2024
1 parent c0793b2 commit 95918b3
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 6 deletions.
1 change: 1 addition & 0 deletions auth/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Claim struct {

type ResponseToken struct {
Token string `json:"token"`
TTL int `json:"ttl"`
}

type SecuredFunc func(http.ResponseWriter, *http.Request, Claim)
18 changes: 14 additions & 4 deletions handlers/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"

"github.com/Luis97lol/auth-service/auth"
"github.com/Luis97lol/auth-service/database"
"github.com/Luis97lol/auth-service/redis"
)
Expand Down Expand Up @@ -33,9 +34,11 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
}

if token, err := redis.ValidateUser(userId); err == nil {
response := map[string]string{"token": token}
json.NewEncoder(w).Encode(response)
return
if ttl, err := redis.GetTokenTTL(token); err == nil {
response := auth.ResponseToken{Token: token, TTL: ttl}
json.NewEncoder(w).Encode(response)
return
}
}

// Generate JWT token and save to Redis
Expand All @@ -46,7 +49,14 @@ func LoginHandler(w http.ResponseWriter, r *http.Request) {
return
}

ttl, err := redis.GetTokenTTL(token)
if err != nil {
println("Error getting token ttl: ", err.Error())
writeJsonError(w, "Error getting token ttl", http.StatusInternalServerError)
return
}

// Respond with token
response := map[string]string{"token": token}
response := auth.ResponseToken{Token: token, TTL: ttl}
json.NewEncoder(w).Encode(response)
}
12 changes: 10 additions & 2 deletions handlers/renew.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"

"github.com/Luis97lol/auth-service/auth"
"github.com/Luis97lol/auth-service/redis"
)

Expand All @@ -30,7 +31,14 @@ func RenewHandler(w http.ResponseWriter, r *http.Request) {
return
}

// Respond with username
response := map[string]string{"token": token}
ttl, err := redis.GetTokenTTL(token)
if err != nil {
println("Error getting token ttl on renew: ", err.Error())
writeJsonError(w, "Error getting token ttl on renew", http.StatusInternalServerError)
return
}

// Respond with token
response := auth.ResponseToken{Token: token, TTL: ttl}
json.NewEncoder(w).Encode(response)
}
20 changes: 20 additions & 0 deletions redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,23 @@ func ValidateUser(userId string) (string, error) {
}
return val, nil
}

func GetTokenTTL(token string) (int, error) {
val, err := GetInstance().TTL(context.Background(), fmt.Sprintf("auth_token:%s", token)).Result()
if err == redis.Nil {
return 0, fmt.Errorf("token not found")
} else if err != nil {
return 0, err
}
return int(val.Seconds()), nil
}

func GetUserTTL(userId string) (int, error) {
val, err := GetInstance().TTL(context.Background(), fmt.Sprintf("auth_user:%s", userId)).Result()
if err == redis.Nil {
return 0, fmt.Errorf("token not found")
} else if err != nil {
return 0, err
}
return int(val.Seconds()), nil
}

0 comments on commit 95918b3

Please sign in to comment.