-
Notifications
You must be signed in to change notification settings - Fork 4
/
helper.go
43 lines (35 loc) · 1.06 KB
/
helper.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
42
43
package http
import (
"github.com/SparrowDb/sparrowdb/auth"
"github.com/gin-gonic/gin"
)
// BasicMiddleware middleware of gin to handler OPTIONS
// and cors
func BasicMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Server", "SparrowDb")
if c.Request.Method == "OPTIONS" {
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization")
}
c.Next()
}
}
// AuthMiddleware middleware to check auth token
func AuthMiddleware(onerr func(c *gin.Context)) gin.HandlerFunc {
return func(c *gin.Context) {
token, _, err := auth.ParseClaimFromRequest(c.Request)
if err != nil || !token.Valid {
onerr(c)
}
c.Next()
}
}
func hasPermission(c *gin.Context, role int) bool {
_, u, err := auth.ParseClaimFromRequest(c.Request)
if err != nil {
return false
}
return auth.CheckUserPermission(u, role)
}