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.

129 lines
2.9 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package security
  2. import (
  3. "errors"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "github.com/chrislusf/weed-fs/go/glog"
  8. )
  9. var (
  10. ErrUnauthorized = errors.New("unauthorized token")
  11. )
  12. /*
  13. Guard is to ensure data access security.
  14. There are 2 ways to check access:
  15. 1. white list. It's checking request ip address.
  16. 2. JSON Web Token(JWT) generated from secretKey.
  17. The jwt can come from:
  18. 1. url parameter jwt=...
  19. 2. request header "Authorization"
  20. 3. cookie with the name "jwt"
  21. The white list is checked first because it is easy.
  22. Then the JWT is checked.
  23. The Guard will also check these claims if provided:
  24. 1. "exp" Expiration Time
  25. 2. "nbf" Not Before
  26. Generating JWT:
  27. 1. use HS256 to sign
  28. 2. optionally set "exp", "nbf" fields, in Unix time,
  29. the number of seconds elapsed since January 1, 1970 UTC.
  30. Referenced:
  31. https://github.com/pkieltyka/jwtauth/blob/master/jwtauth.go
  32. */
  33. type Guard struct {
  34. whiteList []string
  35. SecretKey Secret
  36. isActive bool
  37. }
  38. func NewGuard(whiteList []string, secretKey string) *Guard {
  39. g := &Guard{whiteList: whiteList, SecretKey: Secret(secretKey)}
  40. g.isActive = len(g.whiteList) != 0 || len(g.SecretKey) != 0
  41. return g
  42. }
  43. func (g *Guard) WhiteList(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
  44. if !g.isActive {
  45. //if no security needed, just skip all checkings
  46. return f
  47. }
  48. return func(w http.ResponseWriter, r *http.Request) {
  49. if err := g.checkWhiteList(w, r); err != nil {
  50. w.WriteHeader(http.StatusUnauthorized)
  51. return
  52. }
  53. f(w, r)
  54. }
  55. }
  56. func (g *Guard) Secure(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
  57. if !g.isActive {
  58. //if no security needed, just skip all checkings
  59. return f
  60. }
  61. return func(w http.ResponseWriter, r *http.Request) {
  62. if err := g.checkJwt(w, r); err != nil {
  63. w.WriteHeader(http.StatusUnauthorized)
  64. return
  65. }
  66. f(w, r)
  67. }
  68. }
  69. func (g *Guard) checkWhiteList(w http.ResponseWriter, r *http.Request) error {
  70. if len(g.whiteList) == 0 {
  71. return nil
  72. }
  73. host, _, err := net.SplitHostPort(r.RemoteAddr)
  74. if err == nil {
  75. for _, ip := range g.whiteList {
  76. if ip == host {
  77. return nil
  78. }
  79. }
  80. }
  81. glog.V(1).Infof("Not in whitelist: %s", r.RemoteAddr)
  82. return fmt.Errorf("Not in whitelis: %s", r.RemoteAddr)
  83. }
  84. func (g *Guard) checkJwt(w http.ResponseWriter, r *http.Request) error {
  85. if g.checkWhiteList(w, r) == nil {
  86. return nil
  87. }
  88. if len(g.SecretKey) == 0 {
  89. return nil
  90. }
  91. tokenStr := GetJwt(r)
  92. if tokenStr == "" {
  93. return ErrUnauthorized
  94. }
  95. // Verify the token
  96. token, err := DecodeJwt(g.SecretKey, tokenStr)
  97. if err != nil {
  98. glog.V(1).Infof("Token verification error from %s: %v", r.RemoteAddr, err)
  99. return ErrUnauthorized
  100. }
  101. if !token.Valid {
  102. glog.V(1).Infof("Token invliad from %s: %v", r.RemoteAddr, tokenStr)
  103. return ErrUnauthorized
  104. }
  105. glog.V(1).Infof("No permission from %s", r.RemoteAddr)
  106. return fmt.Errorf("No write permisson from %s", r.RemoteAddr)
  107. }