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.

119 lines
2.7 KiB

10 years ago
10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
10 years ago
9 years ago
10 years ago
  1. package security
  2. import (
  3. "errors"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "strings"
  8. "github.com/chrislusf/seaweedfs/weed/glog"
  9. )
  10. var (
  11. ErrUnauthorized = errors.New("unauthorized token")
  12. )
  13. /*
  14. Guard is to ensure data access security.
  15. There are 2 ways to check access:
  16. 1. white list. It's checking request ip address.
  17. 2. JSON Web Token(JWT) generated from secretKey.
  18. The jwt can come from:
  19. 1. url parameter jwt=...
  20. 2. request header "Authorization"
  21. 3. cookie with the name "jwt"
  22. The white list is checked first because it is easy.
  23. Then the JWT is checked.
  24. The Guard will also check these claims if provided:
  25. 1. "exp" Expiration Time
  26. 2. "nbf" Not Before
  27. Generating JWT:
  28. 1. use HS256 to sign
  29. 2. optionally set "exp", "nbf" fields, in Unix time,
  30. the number of seconds elapsed since January 1, 1970 UTC.
  31. Referenced:
  32. https://github.com/pkieltyka/jwtauth/blob/master/jwtauth.go
  33. */
  34. type Guard struct {
  35. whiteList []string
  36. SigningKey SigningKey
  37. ExpiresAfterSec int
  38. isActive bool
  39. }
  40. func NewGuard(whiteList []string, signingKey string, expiresAfterSec int) *Guard {
  41. g := &Guard{whiteList: whiteList, SigningKey: SigningKey(signingKey), ExpiresAfterSec:expiresAfterSec}
  42. g.isActive = len(g.whiteList) != 0 || len(g.SigningKey) != 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 checking
  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 GetActualRemoteHost(r *http.Request) (host string, err error) {
  59. host = r.Header.Get("HTTP_X_FORWARDED_FOR")
  60. if host == "" {
  61. host = r.Header.Get("X-FORWARDED-FOR")
  62. }
  63. if strings.Contains(host, ",") {
  64. host = host[0:strings.Index(host, ",")]
  65. }
  66. if host == "" {
  67. host, _, err = net.SplitHostPort(r.RemoteAddr)
  68. }
  69. return
  70. }
  71. func (g *Guard) checkWhiteList(w http.ResponseWriter, r *http.Request) error {
  72. if len(g.whiteList) == 0 {
  73. return nil
  74. }
  75. host, err := GetActualRemoteHost(r)
  76. if err == nil {
  77. for _, ip := range g.whiteList {
  78. // If the whitelist entry contains a "/" it
  79. // is a CIDR range, and we should check the
  80. // remote host is within it
  81. if strings.Contains(ip, "/") {
  82. _, cidrnet, err := net.ParseCIDR(ip)
  83. if err != nil {
  84. panic(err)
  85. }
  86. remote := net.ParseIP(host)
  87. if cidrnet.Contains(remote) {
  88. return nil
  89. }
  90. }
  91. //
  92. // Otherwise we're looking for a literal match.
  93. //
  94. if ip == host {
  95. return nil
  96. }
  97. }
  98. }
  99. glog.V(0).Infof("Not in whitelist: %s", r.RemoteAddr)
  100. return fmt.Errorf("Not in whitelis: %s", r.RemoteAddr)
  101. }