|
@@ -29,6 +29,10 @@ func InitMiddleware(r *gin.Engine) {
|
|
|
r.Use(GinLogger(logger.WithGroup("gin")))
|
|
|
// Global Recover
|
|
|
r.Use(GinRecovery(logger.WithGroup("ginRecovery")))
|
|
|
+ // check header
|
|
|
+ r.Use(CheckLanguage)
|
|
|
+ r.Use(CheckSource)
|
|
|
+ r.Use(CheckProduct)
|
|
|
// check token
|
|
|
r.Use(CheckAuth)
|
|
|
}
|
|
@@ -73,13 +77,51 @@ func Secure(c *gin.Context) {
|
|
|
// c.Header("Content-Security-Policy", "script-src 'self' https://cdnjs.cloudflare.com")
|
|
|
}
|
|
|
|
|
|
+func CheckLanguage(c *gin.Context) {
|
|
|
+ ok := false
|
|
|
+ language := c.Request.Header.Get("Language")
|
|
|
+ for _, l := range common.MetadataConfig.GetLanguages() {
|
|
|
+ slog.Info("----------", "language", l)
|
|
|
+ if language == l.ToString() {
|
|
|
+ ok = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ slog.Info("----------", "language", language, "ok", ok)
|
|
|
+ if !ok {
|
|
|
+ c.AbortWithStatusJSON(200, common.ErrToH(common.InvalidLanguage, c.GetHeader("locale")))
|
|
|
+ } else {
|
|
|
+ c.Set("language", language)
|
|
|
+ }
|
|
|
+ c.Next()
|
|
|
+}
|
|
|
+
|
|
|
+func CheckSource(c *gin.Context) {
|
|
|
+ source := c.Request.Header.Get("Source")
|
|
|
+ ok := false
|
|
|
+ for _, s := range common.MetadataConfig.GetSources() {
|
|
|
+ if source == s.ToString() {
|
|
|
+ ok = true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if !ok {
|
|
|
+ c.AbortWithStatusJSON(200, common.ErrToH(common.InvalidSource, c.GetHeader("locale")))
|
|
|
+ } else {
|
|
|
+ c.Set("source", source)
|
|
|
+ }
|
|
|
+ c.Next()
|
|
|
+}
|
|
|
+
|
|
|
+func CheckProduct(c *gin.Context) {
|
|
|
+ product := c.Request.Header.Get("Product")
|
|
|
+ if product != common.MetadataConfig.GetProduct().ToString() {
|
|
|
+ c.AbortWithStatusJSON(200, common.ErrToH(common.InvalidProduct, product))
|
|
|
+ } else {
|
|
|
+ c.Set("product", product)
|
|
|
+ }
|
|
|
+ c.Next()
|
|
|
+}
|
|
|
+
|
|
|
func CheckAuth(c *gin.Context) {
|
|
|
- //if common.Hostname == "DESKTOP-2VF4H05" {
|
|
|
- // c.Set("uid", cast.ToUint(1))
|
|
|
- // c.Set("username", "dev")
|
|
|
- // c.Next()
|
|
|
- // return
|
|
|
- //}
|
|
|
if strings.HasPrefix(c.FullPath(), "/dr/api/v1/auth") {
|
|
|
token := c.Request.Header.Get("Authorization")
|
|
|
uid, username, err := common.ParseToken(strings.TrimPrefix(token, "Bearer "))
|