-
Notifications
You must be signed in to change notification settings - Fork 1
/
controllers.go
48 lines (42 loc) · 1.39 KB
/
controllers.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
44
45
46
47
48
package controllers
import (
"fmt"
"github.com/golang/glog"
"github.com/auth0/go-jwt-middleware"
jwt "github.com/dgrijalva/jwt-go"
"github.com/gin-gonic/gin"
)
// ResponseObject is a simple mapping obejct
type ResponseObject map[string]interface{}
// CORSMiddleware enable CORS
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Max-Age", "86400")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, UPDATE")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length, Authorization, access-control-allow-origin")
c.Writer.Header().Set("Access-Control-Expose-Headers", "Content-Length")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
if c.Request.Method == "OPTIONS" {
fmt.Println("OPTIONS")
c.AbortWithStatus(200)
} else {
c.Next()
}
}
}
// CheckJWT checks the JWT
func CheckJWT(secret string) gin.HandlerFunc {
return func(c *gin.Context) {
jwtMid := *jwtmiddleware.New(jwtmiddleware.Options{
ValidationKeyGetter: func(token *jwt.Token) (interface{}, error) {
return []byte(secret), nil
},
SigningMethod: jwt.SigningMethodHS256,
})
if err := jwtMid.CheckJWT(c.Writer, c.Request); err != nil {
glog.Error(err.Error())
c.AbortWithStatus(401)
}
}
}