-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: All token system moved to JWT instead of random DB generated st…
…ring
- Loading branch information
1 parent
b87da9d
commit 53baf4d
Showing
11 changed files
with
53 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,29 @@ | ||
package user_token | ||
|
||
import ( | ||
"os" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/limanmys/render-engine/app/models" | ||
"github.com/limanmys/render-engine/internal/database" | ||
gorandom "github.com/zekiahmetbayar/go-random" | ||
"github.com/dgrijalva/jwt-go" | ||
) | ||
|
||
// Create a new token or retrieve old one | ||
// Create a new token for user purpose of internal use | ||
func Create(user_id string) (string, error) { | ||
// Search token on database | ||
var token models.Token | ||
database.Connection().Model(&models.Token{}).Where("user_id = ?", user_id).First(&token) | ||
|
||
// If token does not exists, create token | ||
if token.ID == "" { | ||
// Create new id for token | ||
uid := uuid.New() | ||
// Generate token | ||
token := generate() | ||
// Create token on database | ||
if err := database.Connection().Model(&models.Token{}).Create(models.Token{ | ||
ID: uid.String(), | ||
CreatedAt: time.Now().Format(time.RFC3339), | ||
UpdatedAt: time.Now().Format(time.RFC3339), | ||
UserID: user_id, | ||
Token: token, | ||
}).Error; err != nil { | ||
return "", err | ||
} | ||
|
||
return token, nil | ||
} | ||
// Get token update date | ||
updateDate, err := time.Parse(time.RFC3339, token.UpdatedAt) | ||
if err != nil { | ||
return "", err | ||
} | ||
// If token updated after 6 hours | ||
if time.Since(updateDate).Hours() > 6 { | ||
// TODO: Update token | ||
token_str := generate() | ||
if err := database.Connection().Model(&token).Update("token", token_str).Error; err != nil { | ||
return "", err | ||
} | ||
return token_str, nil | ||
// Define the JWT claims | ||
claims := jwt.MapClaims{ | ||
"sub": user_id, | ||
"exp": time.Now().Add(time.Minute * 15).Unix(), // Token expiration time | ||
} | ||
|
||
return token.Token, nil | ||
} | ||
// Create the JWT token | ||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims) | ||
|
||
// Generate a new token | ||
func generate() string { | ||
token, err := gorandom.String(false, true, false, 32) | ||
// Sign the token with a secret key | ||
// Replace "your-secret-key" with your actual secret key | ||
tokenString, err := token.SignedString([]byte(os.Getenv("JWT_SECRET"))) | ||
if err != nil { | ||
return "" | ||
return "", err | ||
} | ||
|
||
return token | ||
return tokenString, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters