feat: add business logic controllers
- Add authentication controller for login, logout, and token refresh - Add user controller for user management and profile operations - Add session controller for session management and validation - Add access controller for API access control and permissions - Include proper input validation and error handling - Implement secure authentication flows
This commit is contained in:
47
controllers/access.go
Normal file
47
controllers/access.go
Normal file
@@ -0,0 +1,47 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
|
||||
"git.secnex.io/secnex/idp-api/api"
|
||||
"git.secnex.io/secnex/idp-api/db"
|
||||
"git.secnex.io/secnex/idp-api/models"
|
||||
"git.secnex.io/secnex/idp-api/repositories"
|
||||
"git.secnex.io/secnex/idp-api/utils"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func CreateApiKey(c *fiber.Ctx) error {
|
||||
database := db.GetDB()
|
||||
apiKeyRepo := repositories.NewApiKeyRepository(database)
|
||||
user := c.Locals("user").(*models.User)
|
||||
token := utils.GenerateRandomString(32)
|
||||
hashToken, err := utils.HashPassword(token, utils.DefaultParams)
|
||||
if err != nil {
|
||||
return api.Error(c, "Failed to hash token", fiber.StatusInternalServerError, fiber.Map{
|
||||
"message": "Failed to hash token",
|
||||
})
|
||||
}
|
||||
apiKey := &models.ApiKey{
|
||||
UserID: &user.ID,
|
||||
Key: hashToken,
|
||||
}
|
||||
apiKey, err = apiKeyRepo.CreateApiKey(apiKey)
|
||||
if err != nil {
|
||||
return api.Error(c, "Failed to create api key", fiber.StatusInternalServerError, fiber.Map{
|
||||
"message": "Failed to create api key",
|
||||
})
|
||||
}
|
||||
|
||||
apiKeyPlain := fmt.Sprintf("%s:%s", apiKey.ID.String(), token)
|
||||
apiKeyPlainEncoded := base64.StdEncoding.EncodeToString([]byte(apiKeyPlain))
|
||||
apiKeyToken := fmt.Sprintf("scn:%s", apiKeyPlainEncoded)
|
||||
|
||||
data := fiber.Map{
|
||||
"token": apiKeyToken,
|
||||
"id": apiKey.ID,
|
||||
}
|
||||
|
||||
return api.Success(c, data, fiber.StatusCreated, nil, nil)
|
||||
}
|
Reference in New Issue
Block a user