a few examples and some documentation cleanup

This commit is contained in:
Dave Grijalva 2016-06-06 17:45:30 -07:00
parent f93fcfd3f9
commit 5eef21b7ed
3 changed files with 51 additions and 13 deletions

View File

@ -0,0 +1,32 @@
package request
import (
"fmt"
"net/url"
)
const (
exampleTokenA = "A"
)
func ExampleHeaderExtractor() {
req := makeExampleRequest("GET", "/", map[string]string{"Token": exampleTokenA}, nil)
tokenString, err := HeaderExtractor{"Token"}.ExtractToken(req)
if err == nil {
fmt.Println(tokenString)
} else {
fmt.Println(err)
}
//Output: A
}
func ExampleArgumentExtractor() {
req := makeExampleRequest("GET", "/", nil, url.Values{"token": {extractorTestTokenA}})
tokenString, err := ArgumentExtractor{"token"}.ExtractToken(req)
if err == nil {
fmt.Println(tokenString)
} else {
fmt.Println(err)
}
//Output: A
}

View File

@ -67,10 +67,7 @@ func TestExtractor(t *testing.T) {
// Bearer token request // Bearer token request
for _, data := range extractorTestData { for _, data := range extractorTestData {
// Make request from test struct // Make request from test struct
r, _ := http.NewRequest("GET", fmt.Sprintf("/?%v", data.query.Encode()), nil) r := makeExampleRequest("GET", "/", data.headers, data.query)
for k, v := range data.headers {
r.Header.Set(k, v)
}
// Test extractor // Test extractor
token, err := data.extractor.ExtractToken(r) token, err := data.extractor.ExtractToken(r)
@ -84,3 +81,11 @@ func TestExtractor(t *testing.T) {
} }
} }
} }
func makeExampleRequest(method, path string, headers map[string]string, urlArgs url.Values) *http.Request {
r, _ := http.NewRequest(method, fmt.Sprintf("%v?%v", path, urlArgs.Encode()), nil)
for k, v := range headers {
r.Header.Set(k, v)
}
return r
}

View File

@ -4,24 +4,25 @@ import (
"strings" "strings"
) )
// Extract bearer token from Authorization header // Strips 'Bearer ' prefix from bearer token string
// Uses PostExtractionFilter to strip "Bearer " prefix from header func stripBearerPrefixFromTokenString(tok string) (string, error) {
var AuthorizationHeaderExtractor = &PostExtractionFilter{
HeaderExtractor{"Authorization"},
func(tok string) (string, error) {
// Should be a bearer token // Should be a bearer token
if len(tok) > 6 && strings.ToUpper(tok[0:7]) == "BEARER " { if len(tok) > 6 && strings.ToUpper(tok[0:7]) == "BEARER " {
return tok[7:], nil return tok[7:], nil
} }
return tok, nil return tok, nil
}, }
// Extract bearer token from Authorization header
// Uses PostExtractionFilter to strip "Bearer " prefix from header
var AuthorizationHeaderExtractor = &PostExtractionFilter{
HeaderExtractor{"Authorization"},
stripBearerPrefixFromTokenString,
} }
// Extractor for OAuth2 access tokens. Looks in 'Authorization' // Extractor for OAuth2 access tokens. Looks in 'Authorization'
// header then 'access_token' argument for a token. // header then 'access_token' argument for a token.
var OAuth2Extractor = &MultiExtractor{ var OAuth2Extractor = &MultiExtractor{
// Look for authorization token first
AuthorizationHeaderExtractor, AuthorizationHeaderExtractor,
// Extract access_token from form or GET argument
ArgumentExtractor{"access_token"}, ArgumentExtractor{"access_token"},
} }