diff options
| author | Chris Lu <chris.lu@gmail.com> | 2020-02-10 20:23:04 -0800 |
|---|---|---|
| committer | Chris Lu <chris.lu@gmail.com> | 2020-02-10 20:23:04 -0800 |
| commit | ac3fc922566c42fb2d1d5603e7b0c167b868fce7 (patch) | |
| tree | 3451368af70559711e435ba3dbb92c3752ff919c /weed/security/jwt.go | |
| parent | 29945fad51320deb7c72f57d1c7a84bcc51429da (diff) | |
| download | seaweedfs-ac3fc922566c42fb2d1d5603e7b0c167b868fce7.tar.xz seaweedfs-ac3fc922566c42fb2d1d5603e7b0c167b868fce7.zip | |
partially doneorigin/fasthttp
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) { |
