2022-02-05 14:55:57 +00:00
|
|
|
package middlewares
|
|
|
|
|
|
|
|
import (
|
2022-02-05 19:59:41 +00:00
|
|
|
"net/http"
|
2022-02-05 14:55:57 +00:00
|
|
|
|
2022-02-05 16:24:07 +00:00
|
|
|
"git.jamesravey.me/ravenscroftj/indiescrobble/controllers"
|
2022-02-05 14:55:57 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
2022-02-05 19:59:41 +00:00
|
|
|
func AuthMiddleware(requireValidUser bool) gin.HandlerFunc {
|
2022-02-05 14:55:57 +00:00
|
|
|
return func(c *gin.Context) {
|
2022-02-05 16:24:07 +00:00
|
|
|
// config := config.GetConfig()
|
|
|
|
|
|
|
|
iam := controllers.NewIndieAuthManager()
|
|
|
|
|
2022-02-05 19:59:41 +00:00
|
|
|
currentUser := iam.GetCurrentUser(c)
|
2022-02-05 16:24:07 +00:00
|
|
|
|
2022-02-05 19:59:41 +00:00
|
|
|
if requireValidUser && (currentUser == "") {
|
|
|
|
c.SetCookie("jwt", "", -1, "/", "", c.Request.URL.Scheme == "https", true)
|
|
|
|
c.Redirect(http.StatusSeeOther, "/")
|
|
|
|
}
|
2022-02-05 16:24:07 +00:00
|
|
|
|
2022-02-05 19:59:41 +00:00
|
|
|
c.Set("user", currentUser)
|
2022-02-05 16:24:07 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
// }
|
2022-02-05 14:55:57 +00:00
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
2022-02-05 16:24:07 +00:00
|
|
|
|