Skip to content

Commit

Permalink
Merge pull request #19 from bancodobrasil/auth-middleware
Browse files Browse the repository at this point in the history
feat: middlewares package and verify api key
  • Loading branch information
eduardaguterres authored Oct 18, 2022
2 parents 28533b6 + 60b196e commit 10a4a9e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
5 changes: 5 additions & 0 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

// Specificate the docs package
_ "github.com/bancodobrasil/featws-resolver-adapter-go/docs"
"github.com/bancodobrasil/featws-resolver-adapter-go/middlewares"
"github.com/bancodobrasil/featws-resolver-adapter-go/routes"
"github.com/bancodobrasil/featws-resolver-adapter-go/services"
ginMonitor "github.com/bancodobrasil/gin-monitor"
Expand Down Expand Up @@ -34,6 +35,7 @@ func init() {
viper.SetDefault("RESOLVER_LOG_JSON", false)
viper.SetDefault("RESOLVER_LOG_LEVEL", "error")
viper.SetDefault("RESOLVER_SERVICE_NAME", "resolver-adapter-go")
viper.SetDefault("RESOLVER_API_KEY", "")
if err := viper.ReadInConfig(); err == nil {
log.Infof("Using config file: %s", viper.ConfigFileUsed())
}
Expand Down Expand Up @@ -84,6 +86,9 @@ func Run(resolverFunc services.ResolverFunc, config Config) error {
services.SetupResolver(resolverFunc)

router := gin.New()

middlewares.InitializeMiddlewares()

// Register ginLogrus log format to gin
router.Use(ginlogrus.Logger(log.StandardLogger()), gin.Recovery())

Expand Down
18 changes: 18 additions & 0 deletions middlewares/middlewares.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package middlewares

import "github.com/gin-gonic/gin"

// Middleware ...
type Middleware interface {
Run()
}

// InitializeMiddlewares ...
func InitializeMiddlewares() {
NewVerifyAPIKeyMiddleware()
}

// Helper function to abort the request with an error status code and message
func respondWithError(c *gin.Context, code int, message interface{}) {
c.AbortWithStatusJSON(code, gin.H{"error": message})
}
51 changes: 51 additions & 0 deletions middlewares/verify_api_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package middlewares

import (
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)

// VerifyAPIKeyMiddleware ...
type VerifyAPIKeyMiddleware struct {
key string
}

var verifyAPIKeyMiddleware *VerifyAPIKeyMiddleware

// VerifyAPIKey ...
func VerifyAPIKey() gin.HandlerFunc {
return verifyAPIKeyMiddleware.Run()
}

// NewVerifyAPIKeyMiddleware ...
func NewVerifyAPIKeyMiddleware() {
verifyAPIKeyMiddleware = &VerifyAPIKeyMiddleware{
key: viper.GetString("RESOLVER_API_KEY"),
}
}

// Run ...
func (m *VerifyAPIKeyMiddleware) Run() gin.HandlerFunc {
return func(c *gin.Context) {
if m.key == "" {
c.Next()
return
}

key := m.extractKeyFromHeader(c)

if key != m.key {
respondWithError(c, 401, "Unauthorized")
}

c.Next()
}
}

func (m *VerifyAPIKeyMiddleware) extractKeyFromHeader(c *gin.Context) string {
authorizationHeader := c.Request.Header.Get("X-API-Key")
if authorizationHeader == "" {
respondWithError(c, 401, "Missing X-API-Key Header")
}
return authorizationHeader
}
2 changes: 2 additions & 0 deletions routes/api/v1/v1.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package v1

import (
"github.com/bancodobrasil/featws-resolver-adapter-go/middlewares"
"github.com/gin-gonic/gin"
)

// Router define routes the API V1
func Router(router *gin.RouterGroup) {
router.Use(middlewares.VerifyAPIKey())
resolveRouter(router.Group("/resolve"))
}

0 comments on commit 10a4a9e

Please sign in to comment.