Skip to content

Commit 1cd5c92

Browse files
committed
Add validate
1 parent 9d6381d commit 1cd5c92

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

common/constants.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ package common
22

33
const (
44
SERVICENAME = "casaos"
5-
VERSION = "0.4.4"
5+
VERSION = "0.4.4.1"
66
BODY = " "
77
)

route/v2.go

+30
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,21 @@ func InitV2DocRouter(docHTML string, docYAML string) http.Handler {
148148

149149
func InitFile() http.Handler {
150150
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
151+
token := r.URL.Query().Get("token")
152+
if len(token) == 0 {
153+
w.Header().Set("Content-Type", "application/json")
154+
w.WriteHeader(http.StatusUnauthorized)
155+
w.Write([]byte(`{"message": "token not found"}`))
156+
return
157+
}
158+
159+
valid, _, errs := jwt.Validate(token, func() (*ecdsa.PublicKey, error) { return external.GetPublicKey(config.CommonInfo.RuntimePath) })
160+
if errs != nil || !valid {
161+
w.Header().Set("Content-Type", "application/json")
162+
w.WriteHeader(http.StatusUnauthorized)
163+
w.Write([]byte(`{"message": "validation failure"}`))
164+
return
165+
}
151166
filePath := r.URL.Query().Get("path")
152167
fileName := path.Base(filePath)
153168
w.Header().Add("Content-Disposition", "attachment; filename*=utf-8''"+url.PathEscape(fileName))
@@ -158,6 +173,21 @@ func InitFile() http.Handler {
158173

159174
func InitDir() http.Handler {
160175
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
176+
token := r.URL.Query().Get("token")
177+
if len(token) == 0 {
178+
w.Header().Set("Content-Type", "application/json")
179+
w.WriteHeader(http.StatusUnauthorized)
180+
w.Write([]byte(`{"message": "token not found"}`))
181+
return
182+
}
183+
184+
valid, _, errs := jwt.Validate(token, func() (*ecdsa.PublicKey, error) { return external.GetPublicKey(config.CommonInfo.RuntimePath) })
185+
if errs != nil || !valid {
186+
w.Header().Set("Content-Type", "application/json")
187+
w.WriteHeader(http.StatusUnauthorized)
188+
w.Write([]byte(`{"message": "validation failure"}`))
189+
return
190+
}
161191
t := r.URL.Query().Get("format")
162192
files := r.URL.Query().Get("files")
163193

0 commit comments

Comments
 (0)