forked from golang-jwt/jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP on migrating request parsing stuff
- Loading branch information
Showing
4 changed files
with
64 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package request | ||
|
||
import ( | ||
"github.com/dgrijalva/jwt-go" | ||
"strings" | ||
"net/http" | ||
) | ||
|
||
// Try to find the token in an http.Request. | ||
// This method will call ParseMultipartForm if there's no token in the header. | ||
// Currently, it looks in the Authorization header as well as | ||
// looking for an 'access_token' request parameter in req.Form. | ||
func ParseFromRequest(req *http.Request, keyFunc jwt.Keyfunc) (token *jwt.Token, err error) { | ||
|
||
// Look for an Authorization header | ||
if ah := req.Header.Get("Authorization"); ah != "" { | ||
// Should be a bearer token | ||
if len(ah) > 6 && strings.ToUpper(ah[0:6]) == "BEARER" { | ||
return jwt.Parse(ah[7:], keyFunc) | ||
} | ||
} | ||
|
||
// Look for "access_token" parameter | ||
req.ParseMultipartForm(10e6) | ||
if tokStr := req.Form.Get("access_token"); tokStr != "" { | ||
return jwt.Parse(tokStr, keyFunc) | ||
} | ||
|
||
return nil, jwt.ErrNoTokenInRequest | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package request | ||
|
||
// func TestParseRequest(t *testing.T) { | ||
// // Bearer token request | ||
// for _, data := range jwtTestData { | ||
// // FIXME: custom parsers are not supported by this helper. skip tests that require them | ||
// if data.parser != nil { | ||
// t.Logf("Skipping [%v]. Custom parsers are not supported by ParseRequest", data.name) | ||
// continue | ||
// } | ||
// | ||
// if data.tokenString == "" { | ||
// data.tokenString = makeSample(data.claims) | ||
// } | ||
// | ||
// r, _ := http.NewRequest("GET", "/", nil) | ||
// r.Header.Set("Authorization", fmt.Sprintf("Bearer %v", data.tokenString)) | ||
// token, err := jwt.ParseFromRequest(r, data.keyfunc) | ||
// | ||
// if token == nil { | ||
// t.Errorf("[%v] Token was not found: %v", data.name, err) | ||
// continue | ||
// } | ||
// if !reflect.DeepEqual(data.claims, token.Claims) { | ||
// t.Errorf("[%v] Claims mismatch. Expecting: %v Got: %v", data.name, data.claims, token.Claims) | ||
// } | ||
// if data.valid && err != nil { | ||
// t.Errorf("[%v] Error while verifying token: %v", data.name, err) | ||
// } | ||
// if !data.valid && err == nil { | ||
// t.Errorf("[%v] Invalid token passed validation", data.name) | ||
// } | ||
// } | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters