diff options
Diffstat (limited to 'weed/security/jwt.go')
| -rw-r--r-- | weed/security/jwt.go | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/weed/security/jwt.go b/weed/security/jwt.go index 0bd7fa974..c6da5c7aa 100644 --- a/weed/security/jwt.go +++ b/weed/security/jwt.go @@ -1,13 +1,16 @@ package security import ( + "bytes" "fmt" "net/http" "strings" "time" + "github.com/dgrijalva/jwt-go" + "github.com/valyala/fasthttp" + "github.com/chrislusf/seaweedfs/weed/glog" - jwt "github.com/dgrijalva/jwt-go" ) type EncodedJwt string @@ -39,7 +42,7 @@ func GenJwt(signingKey SigningKey, expiresAfterSec int, fileId string) EncodedJw return EncodedJwt(encoded) } -func GetJwt(r *http.Request) EncodedJwt { +func OldGetJwt(r *http.Request) EncodedJwt { // Get token from query params tokenStr := r.URL.Query().Get("jwt") @@ -55,6 +58,22 @@ func GetJwt(r *http.Request) EncodedJwt { return EncodedJwt(tokenStr) } +func GetJwt(ctx *fasthttp.RequestCtx) EncodedJwt { + + // Get token from query params + tokenStr := ctx.FormValue("jwt") + + // Get token from authorization header + if tokenStr == nil { + bearer := ctx.Request.Header.Peek("Authorization") + if len(bearer) > 7 && string(bytes.ToUpper(bearer[0:6])) == "BEARER" { + tokenStr = bearer[7:] + } + } + + return EncodedJwt(tokenStr) +} + func DecodeJwt(signingKey SigningKey, tokenString EncodedJwt) (token *jwt.Token, err error) { // check exp, nbf return jwt.ParseWithClaims(string(tokenString), &SeaweedFileIdClaims{}, func(token *jwt.Token) (interface{}, error) { |
