-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.go
50 lines (43 loc) · 1.21 KB
/
middleware.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
49
50
package api
import (
"github.com/DWHengr/aurora/pkg/httpclient"
"github.com/gin-gonic/gin"
"net/http"
)
func Cors() gin.HandlerFunc {
return func(ctx *gin.Context) {
method := ctx.Request.Method
ctx.Header("Access-Control-Allow-Origin", "*")
ctx.Header("Access-Control-Allow-Headers", "Content-Type,AccessToken,X-CSRF-Token,Authorization,aurora-token")
ctx.Header("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE,PATCH,PUT")
ctx.Header("Access-Control-Expose-Headers", "Content-Length,Access-Control-Allow-Origin,Access-Control-Allow-Headers,Content-Type")
ctx.Header("Access-Control-Allow-Credentials", "true")
if method == "OPTIONS" {
ctx.AbortWithStatus(http.StatusNoContent)
}
}
}
func JWTAuth() gin.HandlerFunc {
return func(ctx *gin.Context) {
authHeader := ctx.Request.Header.Get("Aurora-Token")
if authHeader == "" {
ctx.JSON(http.StatusOK, gin.H{
"code": httpclient.NoAccess,
"msg": "No access",
})
ctx.Abort()
return
}
claims, err := ParseToken(authHeader)
if err != nil {
ctx.JSON(http.StatusOK, gin.H{
"code": httpclient.TokenFailure,
"msg": "Invalid Token",
})
ctx.Abort()
return
}
ctx.Set("claims", claims)
ctx.Next()
}
}