forked from mirror/jwt
WIP on migrating request parsing stuff
This commit is contained in:
parent
f164e17f59
commit
e0e3b433f5
|
@ -5,7 +5,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/dgrijalva/jwt-go"
|
"github.com/dgrijalva/jwt-go"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -192,38 +191,6 @@ func TestParser_Parse(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helper method for benchmarking various methods
|
// Helper method for benchmarking various methods
|
||||||
func benchmarkSigning(b *testing.B, method jwt.SigningMethod, key interface{}) {
|
func benchmarkSigning(b *testing.B, method jwt.SigningMethod, key interface{}) {
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -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)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
25
token.go
25
token.go
|
@ -3,7 +3,6 @@ package jwt
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
@ -87,30 +86,6 @@ func Parse(tokenString string, keyFunc Keyfunc) (*Token, error) {
|
||||||
return new(Parser).Parse(tokenString, keyFunc)
|
return new(Parser).Parse(tokenString, keyFunc)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 Keyfunc) (token *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 Parse(ah[7:], keyFunc)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Look for "access_token" parameter
|
|
||||||
req.ParseMultipartForm(10e6)
|
|
||||||
if tokStr := req.Form.Get("access_token"); tokStr != "" {
|
|
||||||
return Parse(tokStr, keyFunc)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil, ErrNoTokenInRequest
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// Encode JWT specific base64url encoding with padding stripped
|
// Encode JWT specific base64url encoding with padding stripped
|
||||||
func EncodeSegment(seg []byte) string {
|
func EncodeSegment(seg []byte) string {
|
||||||
return strings.TrimRight(base64.URLEncoding.EncodeToString(seg), "=")
|
return strings.TrimRight(base64.URLEncoding.EncodeToString(seg), "=")
|
||||||
|
|
Loading…
Reference in New Issue