-
Notifications
You must be signed in to change notification settings - Fork 1
/
middleware.go
68 lines (58 loc) · 1.79 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package routes
import (
"fmt"
jwt "github.com/dgrijalva/jwt-go"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
)
func getCORSConfig() cors.Config {
corsConfig := cors.DefaultConfig()
corsConfig.AllowAllOrigins = true
corsConfig.AllowMethods = []string{"GET", "POST", "PUT", "HEAD", "DELETE"}
corsConfig.AllowHeaders = []string{"Origin", "Content-Length", "Content-Type", "Cache-Control"}
return corsConfig
}
// AuthRequired detects if a JWT token has been sent with the request and
// validates the token before completing the request.
func AuthRequired() gin.HandlerFunc {
return func(c *gin.Context) {
// Load the token from the Query String
token := c.Query("token")
if len(token) == 0 {
// Accept the token in the post body as well
token = c.PostForm("token")
}
if len(token) == 0 {
// Accept the token in the Authorization header as well
token = c.Request.Header.Get("Authorization")
}
if len(token) == 0 {
c.JSON(400, gin.H{"msg": "no token was found"})
c.AbortWithStatus(400)
return
}
// Load the hash from the URL
hash := c.Param("hash")
auth, err := jwt.Parse(token, func(t *jwt.Token) (interface{}, error) {
// Don't forget to validate the alg is what you expect:
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("unexpected signing method: %v", t.Header["alg"])
}
if t.Claims["hash"] == hash {
return []byte(viper.GetString("JwtKey")), nil
}
return nil, fmt.Errorf("signed hash does not match: %v", t.Claims["hash"])
})
if err != nil {
c.JSON(400, gin.H{"error": fmt.Sprintf("Problem with token: %s", err.Error())})
c.Abort()
return
}
if !auth.Valid {
c.JSON(400, gin.H{"error": "token was invalid for unknown reason"})
c.Abort()
}
c.Next()
}
}