You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.8 KiB

  1. package weed_server
  2. import (
  3. "net/http"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/security"
  6. "github.com/chrislusf/seaweedfs/weed/stats"
  7. )
  8. /*
  9. If volume server is started with a separated public port, the public port will
  10. be more "secure".
  11. Public port currently only supports reads.
  12. Later writes on public port can have one of the 3
  13. security settings:
  14. 1. not secured
  15. 2. secured by white list
  16. 3. secured by JWT(Json Web Token)
  17. */
  18. func (vs *VolumeServer) privateStoreHandler(w http.ResponseWriter, r *http.Request) {
  19. switch r.Method {
  20. case "GET", "HEAD":
  21. stats.ReadRequest()
  22. vs.GetOrHeadHandler(w, r)
  23. case "DELETE":
  24. stats.DeleteRequest()
  25. vs.guard.WhiteList(vs.DeleteHandler)(w, r)
  26. case "PUT", "POST":
  27. stats.WriteRequest()
  28. vs.guard.WhiteList(vs.PostHandler)(w, r)
  29. }
  30. }
  31. func (vs *VolumeServer) publicReadOnlyHandler(w http.ResponseWriter, r *http.Request) {
  32. switch r.Method {
  33. case "GET":
  34. stats.ReadRequest()
  35. vs.GetOrHeadHandler(w, r)
  36. case "HEAD":
  37. stats.ReadRequest()
  38. vs.GetOrHeadHandler(w, r)
  39. }
  40. }
  41. func (vs *VolumeServer) maybeCheckJwtAuthorization(r *http.Request, vid, fid string) bool {
  42. if len(vs.guard.SigningKey) == 0 {
  43. return true
  44. }
  45. tokenStr := security.GetJwt(r)
  46. if tokenStr == "" {
  47. glog.V(1).Infof("missing jwt from %s", r.RemoteAddr)
  48. return false
  49. }
  50. token, err := security.DecodeJwt(vs.guard.SigningKey, tokenStr)
  51. if err != nil {
  52. glog.V(1).Infof("jwt verification error from %s: %v", r.RemoteAddr, err)
  53. return false
  54. }
  55. if !token.Valid {
  56. glog.V(1).Infof("jwt invalid from %s: %v", r.RemoteAddr, tokenStr)
  57. return false
  58. }
  59. if sc, ok := token.Claims.(*security.SeaweedFileIdClaims); ok {
  60. return sc.Fid == vid+","+fid
  61. }
  62. glog.V(1).Infof("unexpected jwt from %s: %v", r.RemoteAddr, tokenStr)
  63. return false
  64. }