forked from open-falcon/falcon-plus
/
auth_middle.go
41 lines (36 loc) · 1.16 KB
/
auth_middle.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package utils
import (
"net/http"
log "github.com/Sirupsen/logrus"
"github.com/gin-gonic/gin"
h "github.com/open-falcon/falcon-plus/modules/api/app/helper"
"github.com/spf13/viper"
)
func AuthSessionMidd(c *gin.Context) {
auth, err := h.SessionChecking(c)
if !viper.GetBool("skip_auth") {
if err != nil || auth != true {
log.Debugf("error: %v, auth: %v", err.Error(), auth)
c.Set("auth", auth)
h.JSONR(c, http.StatusUnauthorized, err)
c.Abort()
return
}
}
c.Set("auth", auth)
}
func CORS() gin.HandlerFunc {
return func(context *gin.Context) {
context.Writer.Header().Add("Access-Control-Allow-Origin", "*")
context.Writer.Header().Set("Access-Control-Max-Age", "86400")
context.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
context.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Apitoken")
context.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length")
context.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
if context.Request.Method == "OPTIONS" {
context.AbortWithStatus(200)
} else {
context.Next()
}
}
}