From 28260274a17f46592ef7fdd0d20491e0d323110d Mon Sep 17 00:00:00 2001 From: Ju-Wiluis William Rodriguez Hernandez Date: Mon, 15 Jul 2024 10:17:45 +0200 Subject: [PATCH] Added Opaque Tokens --- auth/opaque.go | 18 ++++++++++++++++++ redis/redis.go | 2 +- 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 auth/opaque.go diff --git a/auth/opaque.go b/auth/opaque.go new file mode 100644 index 0000000..b320364 --- /dev/null +++ b/auth/opaque.go @@ -0,0 +1,18 @@ +package auth + +import ( + "crypto/rand" + "encoding/base64" + "fmt" +) + +func GenerateOpaqueToken(length int) (string, error) { + // Crea un slice de bytes de longitud especificada + token := make([]byte, length) + // Llena el slice de bytes con datos aleatorios + if _, err := rand.Read(token); err != nil { + return "", fmt.Errorf("error generating token: %v", err) + } + // Codifica el slice de bytes en una cadena base64 + return base64.URLEncoding.EncodeToString(token), nil +} diff --git a/redis/redis.go b/redis/redis.go index 1c1c7c1..78f40ff 100644 --- a/redis/redis.go +++ b/redis/redis.go @@ -26,7 +26,7 @@ func GetInstance() *redis.Client { } func GenerateToken(userId string) (string, error) { - token := auth.GenerateJWT(auth.User{Id: userId}) + token, _ := auth.GenerateOpaqueToken(32) err := GetInstance().Set(context.Background(), fmt.Sprintf("auth_token:%s", token), userId, 5*time.Minute).Err() if err != nil { return "", err