Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/controllers/api-key/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import (
)

func Usage(c *gin.Context, deps dependencies.Dependencies) {
apiKey, _ := c.Get("api-key-model")
apiKeyData, _ := c.Get("api-key-model")

// Apply model to apiKey
apiKeyModel, _ := apiKey.(models.APIKey)
apiKeyModel, _ := apiKeyData.(models.APIKey)

c.JSON(http.StatusOK, gin.H{
"message": apiKeyModel,
Expand Down
47 changes: 47 additions & 0 deletions pkg/middleware/api-key/track_key_usage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package middleware

import (
"net/http"
"time"

"github.com/Frier03/KeyAuth-API/pkg/models"
"github.com/Frier03/KeyAuth-API/pkg/services"
"github.com/gin-gonic/gin"
)

func TrackKeyUsage(badgerService *services.BadgerService) gin.HandlerFunc {
return func(c *gin.Context) {
apiKey := c.GetHeader("X-Api-Key")
apiKeyData, _ := c.Get("api-key-model")

// Apply model to apiKeyData
apiKeyModel, _ := apiKeyData.(models.APIKey)

// Check if limit on API key has exceeded
if apiKeyModel.Limit <= apiKeyModel.Usage {
c.AbortWithStatusJSON(http.StatusTooManyRequests, gin.H{
"error": "Rate limit exceeded",
})
return
}

// Increment the API key usage count
apiKeyModel.Usage++

// Update the API last used
apiKeyModel.LastUsed = time.Now()

// Update the API key in the database
err := badgerService.PutAPIKey([]byte(apiKey), &apiKeyModel)
if err != nil {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{
"error": "Failed to update API key",
})
return
}

// Update c set for api-key-model
c.Set("api-key-model", apiKeyModel)
c.Next()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
"github.com/gin-gonic/gin"
)

// APIKeyValidationMiddleware is a middleware to validate the API key
func APIKeyValidationMiddleware(badgerService *services.BadgerService) gin.HandlerFunc {
func ValidateKey(badgerService *services.BadgerService) gin.HandlerFunc {
return func(c *gin.Context) {
apiKey := c.GetHeader("X-Api-Key")

Expand Down Expand Up @@ -49,7 +48,6 @@ func APIKeyValidationMiddleware(badgerService *services.BadgerService) gin.Handl
return
}

// Set custom header for the api key model retrieved
c.Set("api-key-model", apiKeyModel)
c.Next()
}
Expand Down
11 changes: 0 additions & 11 deletions pkg/middleware/api_key_usage.go

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/Frier03/KeyAuth-API/pkg/models"
)

func ValidateModelMiddleware(model interface{}) gin.HandlerFunc {
func ValidateModel(model interface{}) gin.HandlerFunc {
return func(c *gin.Context) {
if err := c.ShouldBindJSON(model); err != nil {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{
Expand Down
14 changes: 8 additions & 6 deletions pkg/routes/api_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package routes
import (
"github.com/gin-gonic/gin"

api "github.com/Frier03/KeyAuth-API/pkg/controllers/api-key"
apiController "github.com/Frier03/KeyAuth-API/pkg/controllers/api-key"
"github.com/Frier03/KeyAuth-API/pkg/dependencies"
"github.com/Frier03/KeyAuth-API/pkg/middleware"
middleware "github.com/Frier03/KeyAuth-API/pkg/middleware"
apiMiddleware "github.com/Frier03/KeyAuth-API/pkg/middleware/api-key"
"github.com/Frier03/KeyAuth-API/pkg/models"
)

Expand All @@ -14,16 +15,17 @@ func SetupAPIKeyRoutes(r *gin.Engine, deps dependencies.Dependencies) {
apiKeyRoutes := r.Group("/api-key")
{
apiKeyRoutes.POST("/generate",
middleware.ValidateModelMiddleware(&models.APIKeyGenerateRequest{}),
middleware.ValidateModel(&models.APIKeyGenerateRequest{}),
func(c *gin.Context) {
api.Generate(c, deps)
apiController.Generate(c, deps)
},
)

apiKeyRoutes.GET("/usage",
middleware.APIKeyValidationMiddleware(deps.BadgerService),
apiMiddleware.ValidateKey(deps.BadgerService),
apiMiddleware.TrackKeyUsage(deps.BadgerService),
func(c *gin.Context) {
api.Usage(c, deps)
apiController.Usage(c, deps)
},
)
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/routes/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package routes
import (
"github.com/gin-gonic/gin"

auth "github.com/Frier03/KeyAuth-API/pkg/controllers/auth"
"github.com/Frier03/KeyAuth-API/pkg/middleware"
authController "github.com/Frier03/KeyAuth-API/pkg/controllers/auth"
middleware "github.com/Frier03/KeyAuth-API/pkg/middleware"
"github.com/Frier03/KeyAuth-API/pkg/models"
)

Expand All @@ -13,17 +13,17 @@ func SetupAuthRoutes(r *gin.Engine) {
authRoutes := r.Group("/auth")
{
authRoutes.POST("/login",
middleware.ValidateModelMiddleware(&models.LoginRequest{}), // Validate login request payload
auth.Login, // Expected resource logic
middleware.ValidateModel(&models.LoginRequest{}), // Validate login request payload
authController.Login, // Expected resource logic
)

authRoutes.POST("/register",
middleware.ValidateModelMiddleware(&models.RegisterRequest{}), // Validate register request payload
auth.Register, // Expected resource logic
middleware.ValidateModel(&models.RegisterRequest{}), // Validate register request payload
authController.Register, // Expected resource logic
)

authRoutes.POST("/logout",
auth.Logout, // Expected resource logic
authController.Logout, // Expected resource logic
)
}
}