Skip to content

Commit fd5b6d2

Browse files
author
soasurs
authored
Merge pull request #12 from leancloud/engine
🩹 Compatibility Fix
2 parents 7cebaa6 + f92629e commit fd5b6d2

File tree

1 file changed

+67
-11
lines changed

1 file changed

+67
-11
lines changed

leancloud/server.go

Lines changed: 67 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func corsHandler(w http.ResponseWriter, r *http.Request) {
6969
}
7070

7171
func metadataHandler(w http.ResponseWriter, r *http.Request) {
72-
if validateMasterKey(r) {
72+
if !validateMasterKey(r) {
7373
errorResponse(w, r, fmt.Errorf("Master Key check failed, request from %s", r.RemoteAddr))
7474
return
7575
}
@@ -235,7 +235,15 @@ func constructRequest(r *http.Request, name string, rpc bool) (*FunctionRequest,
235235
request.Meta = map[string]string{
236236
"remoteAddr": r.RemoteAddr,
237237
}
238-
sessionToken := r.Header.Get("X-LC-Session")
238+
var sessionToken string
239+
if r.Header.Get("X-LC-Session") != "" {
240+
sessionToken = r.Header.Get("X-LC-Session")
241+
} else if r.Header.Get("x-uluru-session-token") != "" {
242+
sessionToken = r.Header.Get("x-uluru-session-token")
243+
} else if r.Header.Get("x-avoscloud-session-token") != "" {
244+
sessionToken = r.Header.Get("x-avoscloud-session-token")
245+
}
246+
239247
if functions[name].defineOption["fetchUser"] == true && sessionToken != "" {
240248
user, err := client.Users.Become(sessionToken)
241249
if err != nil {
@@ -292,30 +300,71 @@ func generateMetadata() ([]byte, error) {
292300
return json.Marshal(meta)
293301
}
294302

303+
func validateAppID(r *http.Request) bool {
304+
if r.Header.Get("X-LC-Id") != "" {
305+
if os.Getenv("LEANCLOUD_APP_ID") != r.Header.Get("X-LC-Id") {
306+
return false
307+
}
308+
} else if r.Header.Get("x-avoscloud-application-id") != "" {
309+
if os.Getenv("LEANCLOUD_APP_ID") != r.Header.Get("x-avoscloud-application-id") {
310+
return false
311+
}
312+
} else if r.Header.Get("x-uluru-application-id") != "" {
313+
if os.Getenv("LEANCLOUD_APP_ID") != r.Header.Get("x-uluru-application-id") {
314+
return false
315+
}
316+
}
317+
318+
return true
319+
}
320+
295321
func validateAppKey(r *http.Request) bool {
296-
if os.Getenv("LEANCLOUD_APP_ID") != r.Header.Get("X-LC-Id") {
322+
if !validateAppID(r) {
297323
return false
298324
}
299-
if os.Getenv("LEANCLOUD_APP_KEY") != r.Header.Get("X-LC-Key") {
300-
return false
325+
326+
if r.Header.Get("X-LC-Key") != "" {
327+
if os.Getenv("LEANCLOUD_APP_KEY") != r.Header.Get("X-LC-Key") {
328+
return false
329+
}
330+
} else if r.Header.Get("x-avoscloud-application-key") != "" {
331+
if os.Getenv("LEANCLOUD_APP_ID") != r.Header.Get("x-avoscloud-application-key") {
332+
return false
333+
}
334+
} else if r.Header.Get("x-uluru-application-key") != "" {
335+
if os.Getenv("LEANCLOUD_APP_ID") != r.Header.Get("x-uluru-application-key") {
336+
return false
337+
}
301338
}
302339
return true
303340
}
304341

305342
func validateMasterKey(r *http.Request) bool {
306-
if os.Getenv("LEANCLOUD_APP_ID") != r.Header.Get("X-LC-Id") {
343+
if !validateAppID(r) {
307344
return false
308345
}
309-
if strings.TrimSuffix(r.Header.Get("X-LC-Key"), ",master") != os.Getenv("LEANCLOUD_APP_MASTER_KEY") {
310-
return false
346+
347+
if r.Header.Get("X-LC-Key") != "" {
348+
if strings.TrimSuffix(r.Header.Get("X-LC-Key"), ",master") != os.Getenv("LEANCLOUD_APP_MASTER_KEY") {
349+
return false
350+
}
351+
} else if r.Header.Get("x-avoscloud-master-key") != "" {
352+
if r.Header.Get("x-avoscloud-master-key") != os.Getenv("LEANCLOUD_APP_MASTER_KEY") {
353+
return false
354+
}
355+
} else if r.Header.Get("x-uluru-master-key") != "" {
356+
if r.Header.Get("x-uluru-master-key") != os.Getenv("LEANCLOUD_APP_MASTER_KEY") {
357+
return false
358+
}
311359
}
312360
return true
313361
}
314362

315363
func validateHookKey(r *http.Request) bool {
316-
if os.Getenv("LEANCLOUD_APP_ID") != r.Header.Get("X-LC-Id") {
364+
if !validateAppID(r) {
317365
return false
318366
}
367+
319368
if os.Getenv("LEANCLOUD_APP_HOOK_KEY") != r.Header.Get("X-LC-Hook-Key") {
320369
return false
321370
}
@@ -324,10 +373,17 @@ func validateHookKey(r *http.Request) bool {
324373

325374
func validateSignature(r *http.Request) (bool, bool) {
326375
var master, pass bool
327-
if os.Getenv("LEANCLOUD_APP_ID") != r.Header.Get("X-LC-Id") {
376+
if !validateAppID(r) {
328377
return master, pass
329378
}
330-
sign := r.Header.Get("X-LC-Sign")
379+
380+
var sign string
381+
if r.Header.Get("X-LC-Sign") != "" {
382+
sign = r.Header.Get("X-LC-Sign")
383+
} else if r.Header.Get("x-avoscloud-request-sign") != "" {
384+
sign = r.Header.Get("x-avoscloud-request-sign")
385+
}
386+
331387
if sign == "" {
332388
return master, pass
333389
}

0 commit comments

Comments
 (0)