feat(auth): Add /token endpoint to request a access token
This commit is contained in:
@@ -7,9 +7,9 @@ import (
|
||||
"git.secnex.io/secnex/oauth2-api/models"
|
||||
)
|
||||
|
||||
func GetApplicationByClientID(clientID string) (*models.Application, error) {
|
||||
func GetApplicationByID(applicationID string) (*models.Application, error) {
|
||||
var application *models.Application
|
||||
if err := database.DB.Where("client_id = ? AND expires_at > ?", clientID, time.Now().UTC()).First(&application).Error; err != nil {
|
||||
if err := database.DB.Where("id = ? AND expires_at > ?", applicationID, time.Now().UTC()).First(&application).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return application, nil
|
||||
|
||||
@@ -1,10 +1,24 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.secnex.io/secnex/oauth2-api/database"
|
||||
"git.secnex.io/secnex/oauth2-api/models"
|
||||
)
|
||||
|
||||
func GetAuthorizationByID(id string) (*models.Authorization, error) {
|
||||
var authorization *models.Authorization
|
||||
if err := database.DB.Where("id = ? AND expires_at > ?", id, time.Now().UTC()).First(&authorization).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return authorization, nil
|
||||
}
|
||||
|
||||
func CreateAuthorization(authorization *models.Authorization) error {
|
||||
return database.DB.Create(authorization).Error
|
||||
}
|
||||
|
||||
func ExpireAuthorization(authorizationID string) error {
|
||||
return database.DB.Model(&models.Authorization{}).Where("id = ?", authorizationID).Update("expires_at", time.Now().UTC()).Error
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
10
app/repositories/token.go
Normal file
10
app/repositories/token.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"git.secnex.io/secnex/oauth2-api/database"
|
||||
"git.secnex.io/secnex/oauth2-api/models"
|
||||
)
|
||||
|
||||
func CreateToken(token *models.Token) error {
|
||||
return database.DB.Create(token).Error
|
||||
}
|
||||
@@ -6,6 +6,14 @@ import (
|
||||
"git.secnex.io/secnex/oauth2-api/models"
|
||||
)
|
||||
|
||||
func GetUserByID(id string) (*models.User, error) {
|
||||
var user *models.User
|
||||
if err := database.DB.Where("id = ?", id).First(&user).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func GetUserByUsername(username string) (*models.User, error) {
|
||||
var user *models.User
|
||||
if err := database.DB.Where("username = ?", username).First(&user).Error; err != nil {
|
||||
|
||||
Reference in New Issue
Block a user