Skip to content

Commit

Permalink
Should be addressed now, with tests. Might have broken more things.
Browse files Browse the repository at this point in the history
  • Loading branch information
lonelycode committed Jul 10, 2015
1 parent 771db79 commit 6cde30c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
40 changes: 29 additions & 11 deletions gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,22 @@ func createVersionedSession() SessionState {
return thisSession
}

func createParamAuthSession() SessionState {
var thisSession SessionState
thisSession.Rate = 10000
thisSession.Allowance = thisSession.Rate
thisSession.LastCheck = time.Now().Unix()
thisSession.Per = 60
thisSession.Expires = -1
thisSession.QuotaRenewalRate = 300 // 5 minutes
thisSession.QuotaRenews = time.Now().Unix()
thisSession.QuotaRemaining = 10
thisSession.QuotaMax = -1
thisSession.AccessRights = map[string]AccessDefinition{"9992": AccessDefinition{APIName: "Tyk Test API", APIID: "9992", Versions: []string{"default"}}}

return thisSession
}

func createStandardSession() SessionState {
var thisSession SessionState
thisSession.Rate = 10000
Expand All @@ -109,7 +125,7 @@ func getChain(spec APISpec) http.Handler {
healthStore := &RedisStorageManager{KeyPrefix: "apihealth."}
orgStore := &RedisStorageManager{KeyPrefix: "orgKey."}
spec.Init(&redisStore, &redisStore, healthStore, orgStore)
remote, _ := url.Parse("http://lonelycode.com/")
remote, _ := url.Parse(spec.Proxy.TargetURL)
proxy := TykNewSingleHostReverseProxy(remote, &spec)
proxyHandler := http.HandlerFunc(ProxyHandler(proxy, &spec))
tykMiddleware := &TykMiddleware{&spec, proxy}
Expand Down Expand Up @@ -244,7 +260,7 @@ var PathBasedDefinition string = `
{
"name": "Tyk Test API",
"api_id": "9991",
"api_id": "9992",
"org_id": "default",
"definition": {
"location": "header",
Expand All @@ -270,9 +286,9 @@ var PathBasedDefinition string = `
},
"event_handlers": {},
"proxy": {
"listen_path": "/v1",
"target_url": "http://httbin.org",
"strip_listen_path": false
"listen_path": "/pathBased/",
"target_url": "http://httpbin.org/",
"strip_listen_path": true
}
}
Expand Down Expand Up @@ -451,9 +467,9 @@ func TestParambasedAuth(t *testing.T) {
healthStore := &RedisStorageManager{KeyPrefix: "apihealth."}
orgStore := &RedisStorageManager{KeyPrefix: "orgKey."}
spec.Init(&redisStore, &redisStore, healthStore, orgStore)
thisSession := createVersionedSession()
thisSession := createParamAuthSession()
spec.SessionManager.UpdateSession("54321", thisSession, 60)
uri := "/post?authorization=54321"
uri := "/pathBased/post?authorization=54321"
method := "POST"

form := url.Values{}
Expand All @@ -475,6 +491,8 @@ func TestParambasedAuth(t *testing.T) {

if recorder.Code != 200 {
t.Error("Initial request failed with non-200 code: ", recorder.Code)
log.Error("URI: ", uri)
log.Error("Proxy To:", spec.Proxy.TargetURL)
t.Error(recorder.Body)
}

Expand All @@ -487,19 +505,19 @@ func TestParambasedAuth(t *testing.T) {
log.Error("JSON decoding failed")
}

if dat["args"].(map[string]string)["authorization"] != "54321" {
if dat["args"].(map[string]interface{})["authorization"].(string) != "54321" {
t.Error("Request params did not arrive")
}

if dat["form"].(map[string]string)["foo"] != "swiggetty" {
if dat["form"].(map[string]interface{})["foo"].(string) != "swiggetty" {
t.Error("Form param 1 did not arrive")
}

if dat["form"].(map[string]string)["bar"] != "swoggetty" {
if dat["form"].(map[string]interface{})["bar"].(string) != "swoggetty" {
t.Error("Form param 2 did not arrive")
}

if dat["form"].(map[string]string)["baz"] != "swoogetty" {
if dat["form"].(map[string]interface{})["baz"].(string) != "swoogetty" {
t.Error("Form param 3 did not arrive")
}

Expand Down
24 changes: 23 additions & 1 deletion middleware_auth_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package main
import "net/http"

import (
"bytes"
"errors"
"github.com/Sirupsen/logrus"
"github.com/gorilla/context"
"io"
"io/ioutil"
)

// KeyExists will check if the key being used to access the API is in the request data,
Expand All @@ -21,12 +24,31 @@ func (k *AuthKey) GetConfig() (interface{}, error) {
return k.TykMiddleware.Spec.APIDefinition.Auth, nil
}

func (k *AuthKey) copyResponse(dst io.Writer, src io.Reader) {
io.Copy(dst, src)
}

func (k *AuthKey) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
thisConfig := k.TykMiddleware.Spec.APIDefinition.Auth

authHeaderValue := r.Header.Get(thisConfig.AuthHeaderName)
if thisConfig.UseParam {
authHeaderValue = r.FormValue(thisConfig.AuthHeaderName)
tempRes := new(http.Request)
*tempRes = *r

defer r.Body.Close()

// Buffer body data
var bodyBuffer bytes.Buffer
bodyBuffer2 := new(bytes.Buffer)

k.copyResponse(&bodyBuffer, r.Body)
*bodyBuffer2 = bodyBuffer

// Create new ReadClosers so we can split output
r.Body = ioutil.NopCloser(&bodyBuffer)
tempRes.Body = ioutil.NopCloser(bodyBuffer2)
authHeaderValue = tempRes.FormValue(thisConfig.AuthHeaderName)
}

if authHeaderValue == "" {
Expand Down
1 change: 1 addition & 0 deletions tyk_reverse_proxy_clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func (p *ReverseProxy) WrappedServeHTTP(rw http.ResponseWriter, req *http.Reques
sessVal := context.Get(req, SessionData)

outreq := new(http.Request)
log.Debug("UPSTREAM REQUEST URL: ", req.URL)
*outreq = *req // includes shallow copies of maps, but okay

p.Director(outreq)
Expand Down

0 comments on commit 6cde30c

Please sign in to comment.