-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from bancodobrasil/auth-middleware
feat: middlewares package and verify api key
- Loading branch information
Showing
4 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |