This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
middleware.go
119 lines (104 loc) · 2.69 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
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
package main
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/lnbits/infinity/api/apiutils"
"github.com/lnbits/infinity/models"
"github.com/lnbits/infinity/storage"
)
func jsonHeaderMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if strings.HasPrefix(r.URL.Path, "/api/") && !strings.HasPrefix(r.URL.Path, "/v/") {
w.Header().Set("Content-Type", "application/json")
}
next.ServeHTTP(w, r)
})
}
func userMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.HasPrefix(r.URL.Path, "/api/user") {
next.ServeHTTP(w, r)
return
}
var user models.User
var err error
masterKey := r.Header.Get("X-MasterKey")
if masterKey == "" {
err = fmt.Errorf("X-MasterKey header not provided")
} else {
err = storage.DB.Where("master_key", masterKey).First(&user).Error
}
if err != nil {
// the user is required for /api/user, but not for /api/create-wallet
if r.URL.Path != "/api/user/create-wallet" {
apiutils.SendJSONError(w, 401, "error fetching user: %s", err.Error())
return
}
} else {
r = r.WithContext(
context.WithValue(
r.Context(),
"user",
&user,
),
)
}
next.ServeHTTP(w, r)
})
}
func walletMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !strings.HasPrefix(r.URL.Path, "/api/wallet") && // better API routes
!strings.HasPrefix(r.URL.Path, "/api/v1/") { // lnbits-compatibility
next.ServeHTTP(w, r)
return
}
var permission string
var wallet models.Wallet
var err error
// try header
walletKey := r.Header.Get("X-Api-Key")
if walletKey == "" {
// try querystring
walletKey = r.URL.Query().Get("api-key")
}
if walletKey == "" {
err = fmt.Errorf("X-Api-Key header not provided")
} else {
result := storage.DB.
Where("admin_key", walletKey).Or("invoice_key", walletKey).
First(&wallet)
if result.Error != nil {
err = result.Error
} else if wallet.AdminKey == walletKey {
permission = "admin"
} else if wallet.InvoiceKey == walletKey {
permission = "invoice"
}
}
// all app internal routes require 'admin'
if permission != "admin" && strings.HasPrefix(r.URL.Path, "/api/wallet/app/") {
w.WriteHeader(401)
return
}
if err != nil {
apiutils.SendJSONError(w, 401, "error fetching wallet: %s", err.Error())
return
} else {
r = r.WithContext(
context.WithValue(
context.WithValue(
r.Context(),
"wallet",
&wallet,
),
"permission",
permission,
),
)
}
next.ServeHTTP(w, r)
})
}