forked from gitploy-io/gitploy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.go
More file actions
74 lines (63 loc) · 1.4 KB
/
Copy pathmiddleware.go
File metadata and controls
74 lines (63 loc) · 1.4 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
package shared
import (
"net/http"
"time"
"github.com/gin-gonic/gin"
gb "github.com/gitploy-io/gitploy/internal/server/global"
"github.com/gitploy-io/gitploy/pkg/e"
)
const (
extraDuration time.Duration = 30 * 24 * time.Hour
)
type (
Middleware struct {
i Interactor
}
)
func NewMiddleware(intr Interactor) *Middleware {
return &Middleware{
i: intr,
}
}
func (m *Middleware) IsLicenseExpired() gin.HandlerFunc {
return func(c *gin.Context) {
ctx := c.Request.Context()
lic, err := m.i.GetLicense(ctx)
if err != nil {
gb.AbortWithStatusAndError(c, http.StatusInternalServerError, err)
return
}
if lic.IsOSS() {
return
}
if lic.IsOverLimit() {
gb.AbortWithStatusAndError(
c,
http.StatusPaymentRequired,
e.NewErrorWithMessage(e.ErrorCodeLicenseRequired, "The license is over the limit.", nil),
)
return
}
if lic.IsStandard() && lic.IsExpired() {
now := time.Now()
if lic.ExpiredAt.Add(extraDuration).Before(now) {
gb.AbortWithStatusAndError(
c,
http.StatusPaymentRequired,
e.NewErrorWithMessage(e.ErrorCodeLicenseRequired, "The license is expired.", nil),
)
return
}
}
}
}
func (m *Middleware) OnlyAuthorized() gin.HandlerFunc {
return func(c *gin.Context) {
_, ok := c.Get(gb.KeyUser)
if !ok {
c.AbortWithStatusJSON(http.StatusUnauthorized, map[string]string{
"message": "Unauthorized user",
})
}
}
}