-
Notifications
You must be signed in to change notification settings - Fork 4
/
middleware.go
50 lines (39 loc) · 1.07 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 main
import (
pc "github.com/padloc/padlock-cloud/padlockcloud"
"net/http"
"strconv"
)
func NoSubRequired(a *pc.AuthToken) bool {
// return a != nil && a.Device != nil && a.Device.Platform == "iOS" && a.Device.AppVersion == "2.2.0"
return false
}
type CheckSubscription struct {
*Server
RequireSub bool
}
func (m *CheckSubscription) Wrap(h pc.Handler) pc.Handler {
return pc.HandlerFunc(func(w http.ResponseWriter, r *http.Request, a *pc.AuthToken) error {
if m.RequireSub {
w.Header().Set("X-Sub-Required", "true")
}
if a == nil {
return &pc.InvalidAuthToken{}
}
acc, err := m.GetOrCreateAccount(a.Email)
if err != nil {
return err
}
status, trialEnd := acc.SubscriptionStatus()
if NoSubRequired(a) {
status = "active"
}
w.Header().Set("X-Sub-Status", status)
w.Header().Set("X-Sub-Trial-End", strconv.FormatInt(trialEnd, 10))
w.Header().Set("X-Stripe-Pub-Key", m.StripeConfig.PublicKey)
if m.RequireSub && status != "trialing" && status != "active" {
return &SubscriptionRequired{}
}
return h.Handle(w, r, a)
})
}