Skip to content

Commit

Permalink
Formatted, renamed sample middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Buhr committed Aug 14, 2014
1 parent 65694e4 commit 1c94a09
Show file tree
Hide file tree
Showing 13 changed files with 34 additions and 37 deletions.
12 changes: 6 additions & 6 deletions api_definition_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ type APIDefinition struct {
AllowedAuthorizeTypes []osin.AuthorizeRequestType `bson:"allowed_authorize_types" json:"allowed_authorize_types"`
AuthorizeLoginRedirect string `bson:"auth_login_redirect" json:"auth_login_redirect"`
} `bson:"oauth_meta" json:"oauth_meta"`
UseBasicAuth bool `bson:"use_basic_auth" json:"use_basic_auth"`
NotificationsDetails NotificationsManager `bson:"notifications" json:"notifications"`
EnableSignatureChecking bool `bson:"enable_signature_checking" json:"enable_signature_checking"`
VersionDefinition struct {
UseBasicAuth bool `bson:"use_basic_auth" json:"use_basic_auth"`
NotificationsDetails NotificationsManager `bson:"notifications" json:"notifications"`
EnableSignatureChecking bool `bson:"enable_signature_checking" json:"enable_signature_checking"`
VersionDefinition struct {
Location string `bson:"location" json:"location"`
Key string `bson:"key" json:"key"`
} `bson:"definition" json:"definition"`
VersionData struct {
VersionData struct {
NotVersioned bool `bson:"not_versioned" json:"not_versioned"`
Versions map[string]VersionInfo `bson:"versions" json:"versions"`
} `bson:"version_data" json:"version_data"`
Expand All @@ -43,7 +43,7 @@ type APIDefinition struct {
TargetURL string `bson:"target_url" json:"target_url"`
StripListenPath bool `bson:"strip_listen_path" json:"strip_listen_path"`
} `bson:"proxy" json:"proxy"`
Active bool `bson:"active" json:"active"`
Active bool `bson:"active" json:"active"`
RawData map[string]interface{} `bson:"raw_data,omitempty" json:"raw_data,omitempty"` // Not used in actual configuration, loaded by config for plugable arc
}

Expand Down
11 changes: 4 additions & 7 deletions api_definition_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package main

import (
"testing"
"net/url"
"net/http"
"encoding/json"
"io/ioutil"
"net/http"
"net/url"
"testing"
)

var sampleDefiniton string = `
Expand Down Expand Up @@ -125,7 +125,6 @@ var nonExpiringMultiDef string = `
`


func createDefinitionFromString(defStr string) APISpec {
var thisLoader = APIDefinitionLoader{}

Expand All @@ -147,7 +146,6 @@ func writeDefToFile(configStruct APIDefinition) {
}
}


func TestExpiredRequest(t *testing.T) {
uri := "/v1/bananaphone"
method := "GET"
Expand Down Expand Up @@ -186,7 +184,7 @@ func TestNotVersioned(t *testing.T) {
thisSpec := createDefinitionFromString(nonExpiringDef)
thisSpec.VersionData.NotVersioned = true

// writeDefToFile(thisSpec.APIDefinition)
// writeDefToFile(thisSpec.APIDefinition)

ok, status := thisSpec.IsRequestValid(req)
if ok != true {
Expand Down Expand Up @@ -376,7 +374,6 @@ func TestIgnored(t *testing.T) {
}
}


func TestBlacklistLinksMulti(t *testing.T) {
uri := "v1/disallowed/blacklist/literal"
method := "GET"
Expand Down
4 changes: 1 addition & 3 deletions gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package main

import (
"encoding/json"
"github.com/justinas/alice"
"net/http"
"net/http/httptest"
"net/http/httputil"
"net/url"
"testing"
"time"
"github.com/justinas/alice"
)

func createThrottledSession() SessionState {
Expand Down Expand Up @@ -102,7 +102,6 @@ func createNonVersionedDefinition() APISpec {

}


func TestThrottling(t *testing.T) {
spec := createNonVersionedDefinition()
thisSession := createThrottledSession()
Expand All @@ -122,7 +121,6 @@ func TestThrottling(t *testing.T) {
chain := getChain(spec)
chain.ServeHTTP(recorder, req)


if recorder.Code != 200 {
t.Error("Initial request failed with non-200 code: \n", recorder.Code)
}
Expand Down
2 changes: 1 addition & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import ()
type TykMiddlewareImplementation interface {
New()
GetConfig() (interface{}, error)
ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) // Handles request
ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) // Handles request
}

// Generic middleware caller to make extension easier
Expand Down
5 changes: 2 additions & 3 deletions middleware_access_rights.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import "net/http"

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

// AccessRightsCheck is a midleware that will check if the key bing used to access the API has
Expand All @@ -24,7 +24,7 @@ func (a *AccessRightsCheck) GetConfig() (interface{}, error) {
}

// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
func (a *AccessRightsCheck) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
func (a *AccessRightsCheck) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
accessingVersion := a.Spec.getVersionFromRequest(r)
thisSessionState := context.Get(r, SessionData).(SessionState)
authHeaderValue := context.Get(r, AuthHeaderValue)
Expand Down Expand Up @@ -75,7 +75,6 @@ func (a *AccessRightsCheck) ProcessRequest(w http.ResponseWriter, r *http.Reques
return nil, 200
}


//// New creates a new HttpHandler for the alice middleware package
//func (a AccessRightsCheck) New() func(http.Handler) http.Handler {
// aliceHandler := func(h http.Handler) http.Handler {
Expand Down
4 changes: 2 additions & 2 deletions middleware_auth_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package main
import "net/http"

import (
"errors"
"github.com/Sirupsen/logrus"
"github.com/gorilla/context"
"github.com/mitchellh/mapstructure"
"errors"
)

// KeyExists will check if the key being used to access the API is in the request data,
Expand Down Expand Up @@ -36,7 +36,7 @@ func (k *AuthKey) GetConfig() (interface{}, error) {
return thisModuleConfig, nil
}

func (k *AuthKey) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
func (k *AuthKey) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
var thisConfig AuthKeyConfiguration
thisConfig = configuration.(AuthKeyConfiguration)

Expand Down
4 changes: 2 additions & 2 deletions middleware_basic_auth_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import "net/http"

import (
"encoding/base64"
"errors"
"github.com/Sirupsen/logrus"
"github.com/gorilla/context"
"strings"
"errors"
)

// BasicAuthKeyIsValid uses a username instead of
Expand All @@ -24,7 +24,7 @@ func (k *BasicAuthKeyIsValid) GetConfig() (interface{}, error) {
}

// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
func (k *BasicAuthKeyIsValid) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
func (k *BasicAuthKeyIsValid) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
authHeaderValue := r.Header.Get("Authorization")
if authHeaderValue == "" {
// No header value, fail
Expand Down
4 changes: 2 additions & 2 deletions middleware_check_HMAC_signature.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import (
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"errors"
"github.com/Sirupsen/logrus"
"github.com/gorilla/context"
"math"
"net/url"
"sort"
"strings"
"time"
"errors"
)

// Test key: 53ac07777cbb8c2d530000021a42331a43bd45555d5c923bdb36fc8a
Expand Down Expand Up @@ -45,7 +45,7 @@ func (hm *HMACMiddleware) GetConfig() (interface{}, error) {
}

// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
func (hm *HMACMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
func (hm *HMACMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
log.Debug("HMAC middleware activated")

authHeaderValue := r.Header.Get("Authorization")
Expand Down
4 changes: 2 additions & 2 deletions middleware_key_expired_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import "net/http"

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

// KeyExpired middleware will check if the requesting key is expired or not. It makes use of the authManager to do so.
Expand All @@ -22,7 +22,7 @@ func (k *KeyExpired) GetConfig() (interface{}, error) {
}

// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
func (k *KeyExpired) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
func (k *KeyExpired) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
thisSessionState := context.Get(r, SessionData).(SessionState)
authHeaderValue := context.Get(r, AuthHeaderValue).(string)
keyExpired := authManager.IsKeyExpired(&thisSessionState)
Expand Down
6 changes: 3 additions & 3 deletions middleware_oauth2_key_exists.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package main
import "net/http"

import (
"errors"
"github.com/Sirupsen/logrus"
"github.com/gorilla/context"
"strings"
"errors"
)

// Oauth2KeyExists will check if the key being used to access the API is in the request data,
Expand All @@ -24,13 +24,13 @@ func (k *Oauth2KeyExists) GetConfig() (interface{}, error) {
}

// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
func (k *Oauth2KeyExists) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
func (k *Oauth2KeyExists) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {

// We're using OAuth, start checking for access keys
authHeaderValue := r.Header.Get("Authorization")
parts := strings.Split(authHeaderValue, " ")
if len(parts) < 2 {
log.WithFields(logrus.Fields{
log.WithFields(logrus.Fields{
"path": r.URL.Path,
"origin": r.RemoteAddr,
}).Info("Attempted access with malformed header, no auth header found.")
Expand Down
6 changes: 3 additions & 3 deletions middleware_rate_limiting.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import "net/http"

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

// RateLimitAndQuotaCheck will check the incomming request and key whether it is within it's quota and
Expand All @@ -23,7 +23,7 @@ func (k *RateLimitAndQuotaCheck) GetConfig() (interface{}, error) {
}

// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
func (k *RateLimitAndQuotaCheck) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
func (k *RateLimitAndQuotaCheck) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
sessionLimiter := SessionLimiter{}
thisSessionState := context.Get(r, SessionData).(SessionState)
authHeaderValue := context.Get(r, AuthHeaderValue).(string)
Expand Down Expand Up @@ -51,7 +51,7 @@ func (k *RateLimitAndQuotaCheck) ProcessRequest(w http.ResponseWriter, r *http.R
"origin": r.RemoteAddr,
"key": authHeaderValue,
}).Info("Key quota limit exceeded.")
return errors.New("Quota exceeded"), 403
return errors.New("Quota exceeded"), 403
}
// Other reason? Still not allowed
return errors.New("Access denied"), 403
Expand Down
7 changes: 5 additions & 2 deletions middleware_sample.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package main

import (
"net/http"
"errors"
"github.com/mitchellh/mapstructure"
"net/http"
)

// It's very easy to create custom middleware
// TODO: Write the docs around this

// ModifiedMiddleware is a sample custom middleware component, must inherit TykMiddleware
// so you have access to spec and definition data
type ModifiedMiddleware struct {
Expand Down Expand Up @@ -33,7 +36,7 @@ func (m *ModifiedMiddleware) GetConfig() (interface{}, error) {
}

// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
func (m *ModifiedMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
func (m *ModifiedMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
var thisConfig ModifiedMiddlewareConfig
thisConfig = configuration.(ModifiedMiddlewareConfig)
log.Info("Custom configuration: ", thisConfig.CustomConfigVar)
Expand Down
2 changes: 1 addition & 1 deletion middleware_version_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (v *VersionCheck) GetConfig() (interface{}, error) {
}

// ProcessRequest will run any checks on the request on the way through the system, return an error to have the chain fail
func (v *VersionCheck) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
func (v *VersionCheck) ProcessRequest(w http.ResponseWriter, r *http.Request, configuration interface{}) (error, int) {
// Check versioning, blacklist, whitelist and ignored status
requestValid, stat := v.TykMiddleware.Spec.IsRequestValid(r)
if requestValid == false {
Expand Down

0 comments on commit 1c94a09

Please sign in to comment.