35 lines
871 B
Go
35 lines
871 B
Go
package services
|
|
|
|
import (
|
|
"encoding/base64"
|
|
"fmt"
|
|
|
|
"git.secnex.io/secnex/auth-api/database"
|
|
"git.secnex.io/secnex/auth-api/models"
|
|
"git.secnex.io/secnex/auth-api/utils"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func CreateApiKey() *utils.HTTPResponse {
|
|
keyID := uuid.New()
|
|
key := utils.GenerateRandomString(32)
|
|
createApiKey := &models.ApiKey{
|
|
ID: keyID,
|
|
Key: key,
|
|
}
|
|
|
|
if err := database.DB.Create(createApiKey).Error; err != nil {
|
|
return utils.NewHTTPResponse(fiber.StatusInternalServerError, &fiber.Map{
|
|
"message": "Error creating API key",
|
|
}, "", nil, nil)
|
|
}
|
|
|
|
apiKeyPlain := fmt.Sprintf("%s:%s", keyID.String(), key)
|
|
apiKey := base64.StdEncoding.EncodeToString([]byte(apiKeyPlain))
|
|
return utils.NewHTTPResponse(fiber.StatusOK, &fiber.Map{
|
|
"message": "API key created successfully",
|
|
"key": apiKey,
|
|
}, "", nil, nil)
|
|
}
|