feat(auth): Add /token endpoint to request a access token
This commit is contained in:
62
app/repositories/session.go
Normal file
62
app/repositories/session.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"git.secnex.io/secnex/masterlog"
|
||||
"git.secnex.io/secnex/oauth2-api/cache"
|
||||
"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 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
|
||||
}
|
||||
Reference in New Issue
Block a user