feat(auth): Add login, register, session_info and api creation
This commit is contained in:
14
app/repositories/key.go
Normal file
14
app/repositories/key.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"git.secnex.io/secnex/auth-api/database"
|
||||
"git.secnex.io/secnex/auth-api/models"
|
||||
)
|
||||
|
||||
func GetApiKey(id string) (*models.ApiKey, error) {
|
||||
var apiKey *models.ApiKey
|
||||
if err := database.DB.Where("id = ? AND deleted_at IS NULL AND enabled = true", id).First(&apiKey).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return apiKey, nil
|
||||
}
|
||||
99
app/repositories/sessions.go
Normal file
99
app/repositories/sessions.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"git.secnex.io/secnex/auth-api/cache"
|
||||
"git.secnex.io/secnex/auth-api/database"
|
||||
"git.secnex.io/secnex/auth-api/models"
|
||||
"git.secnex.io/secnex/masterlog"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type SessionDetails struct {
|
||||
UserID uuid.UUID `json:"user_id"`
|
||||
Username string `json:"username"`
|
||||
Email string `json:"email"`
|
||||
FirstName string `json:"first_name"`
|
||||
LastName string `json:"last_name"`
|
||||
}
|
||||
|
||||
func CreateSession(user *models.User) *models.Session {
|
||||
session := &models.Session{
|
||||
ID: uuid.New(),
|
||||
UserID: user.ID,
|
||||
}
|
||||
if err := database.DB.Create(session).Error; err != nil {
|
||||
masterlog.Debug("Failed to create session in database", map[string]interface{}{"error": err.Error(), "user_id": user.ID})
|
||||
return nil
|
||||
}
|
||||
sessionDetails := SessionDetails{
|
||||
UserID: user.ID,
|
||||
Username: user.Username,
|
||||
Email: user.Email,
|
||||
FirstName: user.FirstName,
|
||||
LastName: user.LastName,
|
||||
}
|
||||
jsonData, err := json.Marshal(sessionDetails)
|
||||
if err != nil {
|
||||
masterlog.Debug("Failed to marshal session details", map[string]interface{}{"error": err.Error(), "session_id": session.ID})
|
||||
return nil
|
||||
}
|
||||
ttl := time.Hour * 24
|
||||
if cache.Cache.Client == nil {
|
||||
masterlog.Debug("Redis client not initialized, skipping cache storage", map[string]interface{}{"session_id": session.ID})
|
||||
return session
|
||||
}
|
||||
if err := cache.Cache.Client.Do(cache.Cache.Context, cache.Cache.Client.B().Set().Key(session.ID.String()).Value(string(jsonData)).Ex(ttl).Build()).Error(); err != nil {
|
||||
masterlog.Debug("Failed to store session in cache", map[string]interface{}{"error": err.Error(), "session_id": session.ID})
|
||||
return session
|
||||
}
|
||||
masterlog.Debug("Session stored in cache", map[string]interface{}{"session_id": session.ID})
|
||||
return session
|
||||
}
|
||||
|
||||
func GetSessionCache(sessionID string) *SessionDetails {
|
||||
masterlog.Debug("Retrieving session from cache", map[string]interface{}{"session_id": sessionID})
|
||||
if cache.Cache.Client == nil {
|
||||
masterlog.Debug("Redis client not initialized", map[string]interface{}{"session_id": sessionID})
|
||||
return nil
|
||||
}
|
||||
|
||||
res := cache.Cache.Client.Do(cache.Cache.Context, cache.Cache.Client.B().Get().Key(sessionID).Build())
|
||||
if res.Error() != nil {
|
||||
masterlog.Debug("Failed to get session from cache", map[string]interface{}{"error": res.Error(), "session_id": sessionID})
|
||||
return nil
|
||||
}
|
||||
|
||||
rawStr := res.String()
|
||||
if rawStr == "" {
|
||||
masterlog.Debug("Session not found in cache", map[string]interface{}{"session_id": sessionID})
|
||||
return nil
|
||||
}
|
||||
|
||||
// Parse the valkey response structure to extract the actual JSON string
|
||||
var valkeyResponse struct {
|
||||
Message struct {
|
||||
Value string `json:"Value"`
|
||||
Type string `json:"Type"`
|
||||
} `json:"Message"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(rawStr), &valkeyResponse); err != nil {
|
||||
// If it's not the wrapped format, use it directly
|
||||
masterlog.Debug("Cache response not in wrapped format, using directly", map[string]interface{}{"session_id": sessionID})
|
||||
} else {
|
||||
// Extract the actual JSON string from Message.Value
|
||||
rawStr = valkeyResponse.Message.Value
|
||||
masterlog.Debug("Extracted JSON from cache response", map[string]interface{}{"session_id": sessionID})
|
||||
}
|
||||
|
||||
var sessionDetails SessionDetails
|
||||
if err := json.Unmarshal([]byte(rawStr), &sessionDetails); err != nil {
|
||||
masterlog.Debug("Failed to unmarshal session details", map[string]interface{}{"error": err.Error(), "session_id": sessionID})
|
||||
return nil
|
||||
}
|
||||
|
||||
masterlog.Debug("Session retrieved from cache", map[string]interface{}{"session_id": sessionID, "user_id": sessionDetails.UserID})
|
||||
return &sessionDetails
|
||||
}
|
||||
40
app/repositories/users.go
Normal file
40
app/repositories/users.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"git.secnex.io/secnex/auth-api/database"
|
||||
"git.secnex.io/secnex/auth-api/models"
|
||||
"git.secnex.io/secnex/masterlog"
|
||||
)
|
||||
|
||||
func GetUserByUsername(username string) (*models.User, error) {
|
||||
var user *models.User
|
||||
if err := database.DB.Where("username = ?", username).First(&user).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func GetUserByUniqueFields(username, email string) (*models.User, error) {
|
||||
var user *models.User
|
||||
if err := database.DB.Where("username = ? OR email = ?", username, email).First(&user).Error; err != nil {
|
||||
return nil, nil
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func CreateUser(firstName, lastName, username, password, email string) error {
|
||||
user := &models.User{
|
||||
FirstName: firstName,
|
||||
LastName: lastName,
|
||||
Username: username,
|
||||
Password: password,
|
||||
Email: email,
|
||||
}
|
||||
|
||||
if err := database.DB.Create(user).Error; err != nil {
|
||||
masterlog.Debug("Failed to create user in database", map[string]interface{}{"error": err.Error(), "username": username, "email": email})
|
||||
return err
|
||||
}
|
||||
masterlog.Debug("User created successfully", map[string]interface{}{"username": username, "email": email})
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user