31 lines
699 B
Go
31 lines
699 B
Go
|
package middlewares
|
||
|
|
||
|
import (
|
||
|
"strings"
|
||
|
|
||
|
"git.jamesravey.me/ravenscroftj/indiescrobble/config"
|
||
|
"github.com/gin-gonic/gin"
|
||
|
)
|
||
|
|
||
|
func AuthMiddleware() gin.HandlerFunc {
|
||
|
return func(c *gin.Context) {
|
||
|
config := config.GetConfig()
|
||
|
reqKey := c.Request.Header.Get("X-Auth-Key")
|
||
|
reqSecret := c.Request.Header.Get("X-Auth-Secret")
|
||
|
|
||
|
var key string
|
||
|
var secret string
|
||
|
if key = config.GetString("http.auth.key"); len(strings.TrimSpace(key)) == 0 {
|
||
|
c.AbortWithStatus(500)
|
||
|
}
|
||
|
if secret = config.GetString("http.auth.secret"); len(strings.TrimSpace(secret)) == 0 {
|
||
|
c.AbortWithStatus(401)
|
||
|
}
|
||
|
if key != reqKey || secret != reqSecret {
|
||
|
c.AbortWithStatus(401)
|
||
|
return
|
||
|
}
|
||
|
c.Next()
|
||
|
}
|
||
|
}
|