forked from TykTechnologies/tyk
-
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.
Refactored to use middleware instead of one giant function
- Loading branch information
Martin
committed
Jul 9, 2014
1 parent
a839f1a
commit 9dd0fac
Showing
10 changed files
with
439 additions
and
107 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,53 @@ | ||
package main | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
"runtime/pprof" | ||
"fmt" | ||
"github.com/gorilla/context" | ||
) | ||
|
||
type ErrorHandler struct{ | ||
TykMiddleware | ||
} | ||
|
||
func (e ErrorHandler) HandleError(w http.ResponseWriter, r *http.Request, err string, err_code int) { | ||
if config.EnableAnalytics { | ||
t := time.Now() | ||
keyName := r.Header.Get(e.Spec.ApiDefinition.Auth.AuthHeaderName) | ||
version := e.Spec.getVersionFromRequest(r) | ||
if version == "" { | ||
version = "Non Versioned" | ||
} | ||
thisRecord := AnalyticsRecord{ | ||
r.Method, | ||
r.URL.Path, | ||
r.ContentLength, | ||
r.Header.Get("User-Agent"), | ||
t.Day(), | ||
t.Month(), | ||
t.Year(), | ||
t.Hour(), | ||
err_code, | ||
keyName, | ||
t, | ||
version, | ||
e.Spec.ApiDefinition.Name, | ||
e.Spec.ApiDefinition.ApiId, | ||
e.Spec.ApiDefinition.OrgId} | ||
analytics.RecordHit(thisRecord) | ||
} | ||
|
||
w.WriteHeader(err_code) | ||
w.Header().Add("Content-Type", "application/json") | ||
w.Header().Add("X-Generator", "tyk.io") | ||
thisError := ApiError{fmt.Sprintf("%s", err)} | ||
templates.ExecuteTemplate(w, "error.json", &thisError) | ||
if doMemoryProfile { | ||
pprof.WriteHeapProfile(prof_file) | ||
} | ||
|
||
// Clean up | ||
context.Clear(r) | ||
} |
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
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,66 @@ | ||
package main | ||
|
||
import "net/http" | ||
|
||
import ( | ||
"github.com/gorilla/context" | ||
"github.com/Sirupsen/logrus" | ||
) | ||
|
||
type AccessRightsCheck struct{ | ||
TykMiddleware | ||
} | ||
|
||
func (a AccessRightsCheck) New() func(http.Handler) http.Handler { | ||
aliceHandler := func(h http.Handler) http.Handler { | ||
thisHandler := func(w http.ResponseWriter, r *http.Request) { | ||
|
||
accessingVersion := a.Spec.getVersionFromRequest(r) | ||
thisSessionState := context.Get(r, SessionData).(SessionState) | ||
authHeaderValue := context.Get(r, AuthHeaderValue) | ||
|
||
// If there's nothing in our profile, we let them through to the next phase | ||
if len(thisSessionState.AccessRights) > 0 { | ||
// Otherwise, run auth checks | ||
versionList, apiExists := thisSessionState.AccessRights[a.Spec.ApiId] | ||
if !apiExists { | ||
log.WithFields(logrus.Fields{ | ||
"path": r.URL.Path, | ||
"origin": r.RemoteAddr, | ||
"key": authHeaderValue, | ||
}).Info("Attempted access to unauthorised API.") | ||
handler := ErrorHandler{a.TykMiddleware} | ||
handler.HandleError(w, r, "Access to this API has been disallowed", 403) | ||
return | ||
} | ||
|
||
// Find the version in their key access details | ||
found := false | ||
for _, vInfo := range versionList.Versions { | ||
if vInfo == accessingVersion { | ||
found = true | ||
break | ||
} | ||
} | ||
if !found { | ||
// Not found? Bounce | ||
log.WithFields(logrus.Fields{ | ||
"path": r.URL.Path, | ||
"origin": r.RemoteAddr, | ||
"key": authHeaderValue, | ||
}).Info("Attempted access to unauthorised API version.") | ||
handler := ErrorHandler{a.TykMiddleware} | ||
handler.HandleError(w, r, "Access to this API has been disallowed", 403) | ||
return | ||
} | ||
} | ||
|
||
// No gates failed, request is valid, carry on | ||
h.ServeHTTP(w, r) | ||
} | ||
|
||
return http.HandlerFunc(thisHandler) | ||
} | ||
|
||
return aliceHandler | ||
} |
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,57 @@ | ||
package main | ||
|
||
import "net/http" | ||
|
||
import ( | ||
"github.com/gorilla/context" | ||
"github.com/Sirupsen/logrus" | ||
) | ||
|
||
type KeyExists struct{ | ||
TykMiddleware | ||
} | ||
|
||
func (k KeyExists) New() func(http.Handler) http.Handler { | ||
aliceHandler := func(h http.Handler) http.Handler { | ||
thisHandler := func(w http.ResponseWriter, r *http.Request) { | ||
|
||
authHeaderValue := r.Header.Get(k.Spec.ApiDefinition.Auth.AuthHeaderName) | ||
if authHeaderValue == "" { | ||
// No header value, fail | ||
log.WithFields(logrus.Fields{ | ||
"path": r.URL.Path, | ||
"origin": r.RemoteAddr, | ||
}).Info("Attempted access with malformed header, no auth header found.") | ||
|
||
handler := ErrorHandler{k.TykMiddleware} | ||
handler.HandleError(w, r, "Authorisation field missing", 400) | ||
return | ||
} | ||
|
||
// Check if API key valid | ||
key_exists, thisSessionState := authManager.IsKeyAuthorised(authHeaderValue) | ||
if !key_exists { | ||
log.WithFields(logrus.Fields{ | ||
"path": r.URL.Path, | ||
"origin": r.RemoteAddr, | ||
"key": authHeaderValue, | ||
}).Info("Attempted access with non-existent key.") | ||
|
||
handler := ErrorHandler{k.TykMiddleware} | ||
handler.HandleError(w, r, "Key not authorised", 403) | ||
return | ||
} | ||
|
||
// Set session state on context, we will need it later | ||
context.Set(r, SessionData, thisSessionState) | ||
context.Set(r, AuthHeaderValue, authHeaderValue) | ||
|
||
// Request is valid, carry on | ||
h.ServeHTTP(w, r) | ||
} | ||
|
||
return http.HandlerFunc(thisHandler) | ||
} | ||
|
||
return aliceHandler | ||
} |
Oops, something went wrong.