-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.go
More file actions
81 lines (68 loc) · 1.58 KB
/
middleware.go
File metadata and controls
81 lines (68 loc) · 1.58 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"AuthServer/service"
"github.com/gin-gonic/gin"
"log"
"net/http"
_ "time"
)
const (
authorizationHeaderKey = "authorization"
)
func TokenAuthMiddleware() gin.HandlerFunc {
return func(ctx *gin.Context) {
//authorizationHeader := ctx.GetHeader(authorizationHeaderKey)
//if len(authorizationHeader) == 0 {
// err := errors.New("authorization header is not provided")
// ctx.AbortWithStatusJSON(http.StatusUnauthorized, err)
// return
//}
token := ctx.Request.FormValue("auth_key")
if token == "" {
ctx.JSON(http.StatusBadRequest, gin.H{"message": "Token must be filled"})
return
}
if !tokenAuthorized(token) {
ctx.JSON(http.StatusBadRequest, gin.H{"message": "Token invalido"})
return
}
ctx.Next()
}
}
func tokenAuthorized(token string) bool {
value := service.GetValue(nil, token)
//value := service.GetPg("frf", 4).Nonce
return value != ""
}
//func Logger() gin.HandlerFunc {
// //return func(c *gin.Context) {
// // t := time.Now()
// //
// // // Set example variable
// // c.Set("example", "12345")
// //
// // // before request
// //
// // c.Next()
// //
// // // after request
// // latency := time.Since(t)
// // log.Print(latency)
// //
// // // access the status we are sending
// // status := c.Writer.Status()
// // log.Println(status)
// //}
// return TokenAuthMiddleware()
//}
func main() {
r := gin.New()
r.Use(TokenAuthMiddleware())
r.GET("/test", func(c *gin.Context) {
example := c.MustGet("example").(string)
// it would print: "12345"
log.Println(example)
})
// Listen and serve on 0.0.0.0:8080
r.Run(":8080")
}