-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmiddlewares.go
119 lines (101 loc) · 2.86 KB
/
middlewares.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 nbc
import (
"context"
"net/http"
"strings"
"github.com/go-chi/chi/v5"
"github.com/pmoieni/nimbus-cloud/internal/db/auth"
"github.com/pmoieni/nimbus-cloud/internal/db/store"
)
var emailCtxKey = ctxKey("email")
func (s *AuthService) CheckAuth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
at := r.Header.Get(authorizationHeader)
bearer := strings.Split(at, " ")
if !(len(bearer) > 1) {
w.WriteHeader(http.StatusUnauthorized)
return
}
userInfo, err := parseAccessTokenWithValidate(bearer[1])
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
privateClaims := userInfo.PrivateClaims()
email, ok := privateClaims["email"].(string)
if !ok {
w.WriteHeader(http.StatusUnauthorized)
return
}
ctx := context.WithValue(r.Context(), emailCtxKey, email)
next.ServeHTTP(w, r.WithContext(ctx))
})
}
var fileInfoCtxKey = ctxKey("file-info")
func (s *StoreService) CheckFilePermission(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
email, ok := r.Context().Value(emailCtxKey).(string)
if !ok {
w.WriteHeader(http.StatusForbidden)
return
}
on := chi.URLParam(r, "name")
fq := store.New(s.DBCon)
fileInfo, err := fq.GetFileByObjectName(context.Background(), on)
if err != nil {
w.WriteHeader(http.StatusForbidden)
return
}
uq := auth.New(s.DBCon)
userInfo, err := uq.GetUserByEmail(context.Background(), email)
if err != nil {
w.WriteHeader(http.StatusForbidden)
return
}
ctx := context.WithValue(r.Context(), fileInfoCtxKey, fileInfo)
if fileInfo.Owner != userInfo.ID {
permittedFiles, err := fq.ListPermittedFiles(context.Background(), userInfo.ID)
if err != nil {
w.WriteHeader(http.StatusForbidden)
return
}
for _, file := range permittedFiles {
if file.ObjectName == on {
next.ServeHTTP(w, r.WithContext(ctx))
return
}
}
w.WriteHeader(http.StatusForbidden)
return
}
next.ServeHTTP(w, r.WithContext(ctx))
})
}
func (s *StoreService) CheckFileOwner(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
email, ok := r.Context().Value(emailCtxKey).(string)
if !ok {
w.WriteHeader(http.StatusForbidden)
return
}
on := chi.URLParam(r, "name")
fq := store.New(s.DBCon)
fileInfo, err := fq.GetFileByObjectName(context.Background(), on)
if err != nil {
w.WriteHeader(http.StatusForbidden)
return
}
uq := auth.New(s.DBCon)
userInfo, err := uq.GetUserByEmail(context.Background(), email)
if err != nil {
w.WriteHeader(http.StatusForbidden)
return
}
ctx := context.WithValue(r.Context(), fileInfoCtxKey, fileInfo)
if fileInfo.Owner != userInfo.ID {
w.WriteHeader(http.StatusForbidden)
return
}
next.ServeHTTP(w, r.WithContext(ctx))
})
}