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.

164 lines
3.6 KiB

9 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
9 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. "regexp"
  8. "strings"
  9. "github.com/chrislusf/seaweedfs/go/glog"
  10. )
  11. var (
  12. ErrUnauthorized = errors.New("unauthorized token")
  13. )
  14. /*
  15. Guard is to ensure data access security.
  16. There are 2 ways to check access:
  17. 1. white list. It's checking request ip address.
  18. 2. JSON Web Token(JWT) generated from secretKey.
  19. The jwt can come from:
  20. 1. url parameter jwt=...
  21. 2. request header "Authorization"
  22. 3. cookie with the name "jwt"
  23. The white list is checked first because it is easy.
  24. Then the JWT is checked.
  25. The Guard will also check these claims if provided:
  26. 1. "exp" Expiration Time
  27. 2. "nbf" Not Before
  28. Generating JWT:
  29. 1. use HS256 to sign
  30. 2. optionally set "exp", "nbf" fields, in Unix time,
  31. the number of seconds elapsed since January 1, 1970 UTC.
  32. Referenced:
  33. https://github.com/pkieltyka/jwtauth/blob/master/jwtauth.go
  34. */
  35. type Guard struct {
  36. whiteList []string
  37. SecretKey Secret
  38. isActive bool
  39. }
  40. func NewGuard(whiteList []string, secretKey string) *Guard {
  41. g := &Guard{whiteList: whiteList, SecretKey: Secret(secretKey)}
  42. g.isActive = len(g.whiteList) != 0 || len(g.SecretKey) != 0
  43. return g
  44. }
  45. func (g *Guard) WhiteList(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
  46. if !g.isActive {
  47. //if no security needed, just skip all checkings
  48. return f
  49. }
  50. return func(w http.ResponseWriter, r *http.Request) {
  51. if err := g.checkWhiteList(w, r); err != nil {
  52. w.WriteHeader(http.StatusUnauthorized)
  53. return
  54. }
  55. f(w, r)
  56. }
  57. }
  58. func (g *Guard) Secure(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
  59. if !g.isActive {
  60. //if no security needed, just skip all checkings
  61. return f
  62. }
  63. return func(w http.ResponseWriter, r *http.Request) {
  64. if err := g.checkJwt(w, r); err != nil {
  65. w.WriteHeader(http.StatusUnauthorized)
  66. return
  67. }
  68. f(w, r)
  69. }
  70. }
  71. func GetActualRemoteHost(r *http.Request) (host string, err error) {
  72. host = r.Header.Get("HTTP_X_FORWARDED_FOR")
  73. if host == "" {
  74. host = r.Header.Get("X-FORWARDED-FOR")
  75. }
  76. if strings.Contains(host, ",") {
  77. host = host[0:strings.Index(host, ",")]
  78. }
  79. if host == "" {
  80. host, _, err = net.SplitHostPort(r.RemoteAddr)
  81. }
  82. return
  83. }
  84. func (g *Guard) checkWhiteList(w http.ResponseWriter, r *http.Request) error {
  85. if len(g.whiteList) == 0 {
  86. return nil
  87. }
  88. host, err := GetActualRemoteHost(r)
  89. if err == nil {
  90. for _, ip := range g.whiteList {
  91. // If the whitelist entry contains a "/" it
  92. // is a CIDR range, and we should check the
  93. // remote host is within it
  94. match, _ := regexp.MatchString("/", ip)
  95. if match {
  96. _, cidrnet, err := net.ParseCIDR(ip)
  97. if err != nil {
  98. panic(err)
  99. }
  100. remote := net.ParseIP(host)
  101. if cidrnet.Contains(remote) {
  102. return nil
  103. }
  104. }
  105. //
  106. // Otherwise we're looking for a literal match.
  107. //
  108. if ip == host {
  109. return nil
  110. }
  111. }
  112. }
  113. glog.V(1).Infof("Not in whitelist: %s", r.RemoteAddr)
  114. return fmt.Errorf("Not in whitelis: %s", r.RemoteAddr)
  115. }
  116. func (g *Guard) checkJwt(w http.ResponseWriter, r *http.Request) error {
  117. if g.checkWhiteList(w, r) == nil {
  118. return nil
  119. }
  120. if len(g.SecretKey) == 0 {
  121. return nil
  122. }
  123. tokenStr := GetJwt(r)
  124. if tokenStr == "" {
  125. return ErrUnauthorized
  126. }
  127. // Verify the token
  128. token, err := DecodeJwt(g.SecretKey, tokenStr)
  129. if err != nil {
  130. glog.V(1).Infof("Token verification error from %s: %v", r.RemoteAddr, err)
  131. return ErrUnauthorized
  132. }
  133. if !token.Valid {
  134. glog.V(1).Infof("Token invliad from %s: %v", r.RemoteAddr, tokenStr)
  135. return ErrUnauthorized
  136. }
  137. glog.V(1).Infof("No permission from %s", r.RemoteAddr)
  138. return fmt.Errorf("No write permisson from %s", r.RemoteAddr)
  139. }