Skip to content

Commit

Permalink
Simplify ID extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
buger committed Jun 13, 2018
1 parent 5ca7819 commit 47b8d60
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
8 changes: 2 additions & 6 deletions coprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,8 @@ func (m *CoProcessMiddleware) ProcessRequest(w http.ResponseWriter, r *http.Requ
for k, v := range returnObject.Metadata {
returnedSession.MetaData[k] = string(v)
}
if extractor == nil {
ctxSetSession(r, returnedSession, sessionID, true)
} else {
// The CP middleware did setup a session, we should pass it to the ID extractor (caching):
extractor.PostProcess(r, returnedSession, sessionID)
}

ctxSetSession(r, returnedSession, sessionID, true)
}

return nil, 200
Expand Down
13 changes: 3 additions & 10 deletions coprocess_id_extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,11 @@ import (

"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/regexp"
"github.com/TykTechnologies/tyk/user"
)

// IdExtractor is the base interface for an ID extractor.
type IdExtractor interface {
ExtractAndCheck(*http.Request) (string, ReturnOverrides)
PostProcess(*http.Request, *user.SessionState, string)
GenerateSessionID(string, BaseMiddleware) string
}

Expand All @@ -40,11 +38,6 @@ func (e *BaseExtractor) ExtractAndCheck(r *http.Request) (sessionID string, retu
return "", ReturnOverrides{ResponseCode: 403, ResponseError: "Key not authorised"}
}

// PostProcess sets context variables and updates the storage.
func (e *BaseExtractor) PostProcess(r *http.Request, session *user.SessionState, sessionID string) {
ctxSetSession(r, session, sessionID, true)
}

// ExtractHeader is used when a HeaderSource is specified.
func (e *BaseExtractor) ExtractHeader(r *http.Request) (headerValue string, err error) {
headerName := e.Config.ExtractorConfig["header_name"].(string)
Expand Down Expand Up @@ -146,7 +139,7 @@ func (e *ValueExtractor) ExtractAndCheck(r *http.Request) (sessionID string, ret

if keyExists {
if previousSession.IdExtractorDeadline > time.Now().Unix() {
e.PostProcess(r, &previousSession, sessionID)
ctxSetSession(r, &previousSession, sessionID, true)
returnOverrides = ReturnOverrides{
ResponseCode: 200,
}
Expand Down Expand Up @@ -219,7 +212,7 @@ func (e *RegexExtractor) ExtractAndCheck(r *http.Request) (SessionID string, ret

if keyExists {
if previousSession.IdExtractorDeadline > time.Now().Unix() {
e.PostProcess(r, &previousSession, SessionID)
ctxSetSession(r, &previousSession, SessionID, true)
returnOverrides = ReturnOverrides{
ResponseCode: 200,
}
Expand Down Expand Up @@ -294,7 +287,7 @@ func (e *XPathExtractor) ExtractAndCheck(r *http.Request) (SessionID string, ret
previousSession, keyExists := e.BaseMid.CheckSessionAndIdentityForValidKey(SessionID, r)
if keyExists {
if previousSession.IdExtractorDeadline > time.Now().Unix() {
e.PostProcess(r, &previousSession, SessionID)
ctxSetSession(r, &previousSession, SessionID, true)
returnOverrides = ReturnOverrides{
ResponseCode: 200,
}
Expand Down

0 comments on commit 47b8d60

Please sign in to comment.