seaweedfs/go/security/guard.go

151 lines
3.7 KiB
Go
Raw Normal View History

2015-01-05 22:20:04 +00:00
package security
import (
"errors"
"fmt"
"net"
"regexp"
2015-01-05 22:20:04 +00:00
"net/http"
"github.com/chrislusf/seaweedfs/go/glog"
2015-01-05 22:20:04 +00:00
)
var (
ErrUnauthorized = errors.New("unauthorized token")
)
/*
Guard is to ensure data access security.
There are 2 ways to check access:
1. white list. It's checking request ip address.
2. JSON Web Token(JWT) generated from secretKey.
The jwt can come from:
1. url parameter jwt=...
2. request header "Authorization"
3. cookie with the name "jwt"
The white list is checked first because it is easy.
Then the JWT is checked.
The Guard will also check these claims if provided:
1. "exp" Expiration Time
2. "nbf" Not Before
Generating JWT:
1. use HS256 to sign
2. optionally set "exp", "nbf" fields, in Unix time,
the number of seconds elapsed since January 1, 1970 UTC.
Referenced:
https://github.com/pkieltyka/jwtauth/blob/master/jwtauth.go
*/
type Guard struct {
whiteList []string
2015-02-07 23:35:28 +00:00
SecretKey Secret
2015-01-05 22:20:04 +00:00
isActive bool
}
func NewGuard(whiteList []string, secretKey string) *Guard {
2015-02-07 23:35:28 +00:00
g := &Guard{whiteList: whiteList, SecretKey: Secret(secretKey)}
g.isActive = len(g.whiteList) != 0 || len(g.SecretKey) != 0
2015-01-05 22:20:04 +00:00
return g
}
2015-02-07 23:35:28 +00:00
func (g *Guard) WhiteList(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
2015-01-05 22:20:04 +00:00
if !g.isActive {
//if no security needed, just skip all checkings
return f
}
return func(w http.ResponseWriter, r *http.Request) {
2015-02-07 23:35:28 +00:00
if err := g.checkWhiteList(w, r); err != nil {
2015-01-05 22:20:04 +00:00
w.WriteHeader(http.StatusUnauthorized)
return
}
f(w, r)
}
}
2015-02-07 23:35:28 +00:00
func (g *Guard) Secure(f func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
2015-01-05 22:20:04 +00:00
if !g.isActive {
2015-02-07 23:35:28 +00:00
//if no security needed, just skip all checkings
return f
}
return func(w http.ResponseWriter, r *http.Request) {
if err := g.checkJwt(w, r); err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
f(w, r)
2015-01-05 22:20:04 +00:00
}
}
2015-02-07 23:35:28 +00:00
func (g *Guard) checkWhiteList(w http.ResponseWriter, r *http.Request) error {
if len(g.whiteList) == 0 {
return nil
}
2015-01-05 22:20:04 +00:00
2015-02-07 23:35:28 +00:00
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err == nil {
for _, ip := range g.whiteList {
// If the whitelist entry contains a "/" it
// is a CIDR range, and we should check the
// remote host is within it
match, _ := regexp.MatchString("/", ip)
if ( match ) {
_, cidrnet, err := net.ParseCIDR(ip)
if err != nil {
panic(err)
}
remote := net.ParseIP(host)
if cidrnet.Contains(remote) {
return nil
}
}
//
// Otherwise we're looking for a literal match.
//
2015-02-07 23:35:28 +00:00
if ip == host {
return nil
2015-01-05 22:20:04 +00:00
}
}
}
2015-02-07 23:35:28 +00:00
glog.V(1).Infof("Not in whitelist: %s", r.RemoteAddr)
return fmt.Errorf("Not in whitelis: %s", r.RemoteAddr)
}
2015-01-05 22:20:04 +00:00
2015-02-07 23:35:28 +00:00
func (g *Guard) checkJwt(w http.ResponseWriter, r *http.Request) error {
if g.checkWhiteList(w, r) == nil {
return nil
}
2015-01-05 22:20:04 +00:00
2015-02-07 23:35:28 +00:00
if len(g.SecretKey) == 0 {
return nil
}
2015-01-05 22:20:04 +00:00
2015-02-07 23:35:28 +00:00
tokenStr := GetJwt(r)
2015-01-05 22:20:04 +00:00
2015-02-07 23:35:28 +00:00
if tokenStr == "" {
return ErrUnauthorized
}
2015-01-05 22:20:04 +00:00
2015-02-07 23:35:28 +00:00
// Verify the token
token, err := DecodeJwt(g.SecretKey, tokenStr)
if err != nil {
glog.V(1).Infof("Token verification error from %s: %v", r.RemoteAddr, err)
return ErrUnauthorized
}
if !token.Valid {
glog.V(1).Infof("Token invliad from %s: %v", r.RemoteAddr, tokenStr)
return ErrUnauthorized
2015-01-05 22:20:04 +00:00
}
glog.V(1).Infof("No permission from %s", r.RemoteAddr)
return fmt.Errorf("No write permisson from %s", r.RemoteAddr)
}