guard.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. ReadSigningKey SigningKey
  39. ReadExpiresAfterSec int
  40. isWriteActive bool
  41. }
  42. func NewGuard(whiteList []string, signingKey string, expiresAfterSec int, readSigningKey string, readExpiresAfterSec int) *Guard {
  43. g := &Guard{
  44. whiteList: whiteList,
  45. SigningKey: SigningKey(signingKey),
  46. ExpiresAfterSec: expiresAfterSec,
  47. ReadSigningKey: SigningKey(readSigningKey),
  48. ReadExpiresAfterSec: readExpiresAfterSec,
  49. }
  50. g.isWriteActive = len(g.whiteList) != 0 || len(g.SigningKey) != 0
  51. return g
  52. }
  53. func (g *Guard) WhiteList(f http.HandlerFunc) http.HandlerFunc {
  54. if !g.isWriteActive {
  55. //if no security needed, just skip all checking
  56. return f
  57. }
  58. return func(w http.ResponseWriter, r *http.Request) {
  59. if err := g.checkWhiteList(w, r); err != nil {
  60. w.WriteHeader(http.StatusUnauthorized)
  61. return
  62. }
  63. f(w, r)
  64. }
  65. }
  66. func GetActualRemoteHost(r *http.Request) (host string, err error) {
  67. host = r.Header.Get("HTTP_X_FORWARDED_FOR")
  68. if host == "" {
  69. host = r.Header.Get("X-FORWARDED-FOR")
  70. }
  71. if strings.Contains(host, ",") {
  72. host = host[0:strings.Index(host, ",")]
  73. }
  74. if host == "" {
  75. host, _, err = net.SplitHostPort(r.RemoteAddr)
  76. }
  77. return
  78. }
  79. func (g *Guard) checkWhiteList(w http.ResponseWriter, r *http.Request) error {
  80. if len(g.whiteList) == 0 {
  81. return nil
  82. }
  83. host, err := GetActualRemoteHost(r)
  84. if err == nil {
  85. for _, ip := range g.whiteList {
  86. // If the whitelist entry contains a "/" it
  87. // is a CIDR range, and we should check the
  88. // remote host is within it
  89. if strings.Contains(ip, "/") {
  90. _, cidrnet, err := net.ParseCIDR(ip)
  91. if err != nil {
  92. panic(err)
  93. }
  94. remote := net.ParseIP(host)
  95. if cidrnet.Contains(remote) {
  96. return nil
  97. }
  98. }
  99. //
  100. // Otherwise we're looking for a literal match.
  101. //
  102. if ip == host {
  103. return nil
  104. }
  105. }
  106. }
  107. glog.V(0).Infof("Not in whitelist: %s", r.RemoteAddr)
  108. return fmt.Errorf("Not in whitelis: %s", r.RemoteAddr)
  109. }