-
Notifications
You must be signed in to change notification settings - Fork 1
/
routes.go
142 lines (107 loc) · 3.2 KB
/
routes.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"fmt"
"github.com/appleboy/gin-jwt"
"github.com/gin-gonic/gin"
"github.com/tinchi/gin-react/controllers"
"github.com/tinchi/gin-react/db"
"github.com/tinchi/gin-react/models"
"golang.org/x/crypto/bcrypt"
// "net/http"
"net/url"
"regexp"
"time"
)
func hasPermission(url *url.URL, userRole string) bool {
fmt.Println(url.Path, userRole)
matchUserMe, _ := regexp.MatchString("v1/user/me", url.Path)
fmt.Println(matchUserMe)
if matchUserMe == true {
return true
}
switch userRole {
case "admin":
return true
case "manager":
match, _ := regexp.MatchString("v1/users(/\\d)?", url.Path)
return match
case "user":
match, _ := regexp.MatchString("v1/(deposits(/\\d)?|revenue/report)", url.Path)
return match
default:
fmt.Println("Unknown role!")
return false
}
return false
}
func initializeRoutes(router *gin.Engine) {
router.Static("/assets", "./assets")
authMiddleware := &jwt.GinJWTMiddleware{
Realm: "test zone",
Key: []byte("secret key"),
Timeout: time.Hour,
MaxRefresh: time.Hour,
Authenticator: func(userId string, password string, c *gin.Context) (string, bool) {
fmt.Println("Authenticator:", userId, password)
var hashFromDatabase string
_, err := db.Engine.Table("users").Where("email = ?", userId).Cols("password").Get(&hashFromDatabase)
if err != nil {
fmt.Println(err)
fmt.Println(err.Error())
return userId, false
}
err = bcrypt.CompareHashAndPassword([]byte(hashFromDatabase), []byte(password))
if err != nil {
fmt.Println("Wrong password!")
fmt.Println(err)
return userId, false
}
fmt.Println("Password was correct!")
return userId, true
},
Authorizator: func(userId string, c *gin.Context) bool {
var user models.User
fmt.Println("Authorizator")
_, err := db.Engine.Where("users.email = ?", userId).Get(&user)
if err != nil {
fmt.Println(err.Error())
}
c.Set("current_user", user)
return hasPermission(c.Request.URL, user.Role)
},
Unauthorized: func(c *gin.Context, code int, message string) {
c.JSON(code, gin.H{
"code": code,
"message": message,
})
},
TokenLookup: "header:Authorization",
TokenHeadName: "Bearer",
TimeFunc: time.Now,
}
auth := new(controllers.AuthController)
router.POST("/auth/login", authMiddleware.LoginHandler)
router.POST("/auth/register", auth.RegisterEndpoint)
v1 := router.Group("/v1")
v1.Use(authMiddleware.MiddlewareFunc())
{
deposit := new(controllers.DepositController)
v1.GET("/deposits", deposit.IndexEndpoint)
v1.POST("/deposits", deposit.CreateEndpoint)
v1.GET("/deposits/:id", deposit.ShowEndpoint)
v1.PUT("/deposits/:id", deposit.UpdateEndpoint)
v1.DELETE("/deposits/:id", deposit.DeleteEndpoint)
user := new(controllers.UserController)
v1.GET("/users", user.IndexEndpoint)
v1.POST("/users", user.CreateEndpoint)
v1.GET("/users/:id", user.ShowEndpoint)
v1.PUT("/users/:id", user.UpdateEndpoint)
v1.DELETE("/users/:id", user.DeleteEndpoint)
v1.GET("/user/me", user.MeEndpoint)
revenue := new(controllers.RevenueController)
v1.POST("/revenue/report", revenue.ReportEndpoint)
}
router.NoRoute(func(c *gin.Context) {
c.File("./assets/index.html")
})
}