Skip to content

Commit

Permalink
fix race condition in httpauth where the incorrect handler could be c…
Browse files Browse the repository at this point in the history
…alled for some calls
  • Loading branch information
ntbosscher committed Nov 5, 2020
1 parent 94022b2 commit a8d40bc
Showing 1 changed file with 17 additions and 11 deletions.
28 changes: 17 additions & 11 deletions auth/httpauth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ func (c Config) getAccessTokenCookieName() string {
return strs.Coalesce(c.AccessTokenCookieName, "token")
}

const defaultLoginEndpoint = "/api/auth/#"
const defaultRefreshEndpoint = "/api/auth/refresh"
const defaultLogoutEndpoint = "/api/auth/logout"
const defaultRegisterEndpoint = "/api/auth/register"

func Setup(router *res.Router, config Config) *AuthRouter {
loginPath := strs.Coalesce(config.LoginPath, defaultLoginEndpoint)
router.Post(loginPath, loginHandler(&config))
Expand Down Expand Up @@ -134,11 +139,10 @@ func Setup(router *res.Router, config Config) *AuthRouter {
oauth.Setup(router, config.OAuth, sessionSetter)
}

server := middleware(config)
server := newServer(config)

router.Use(func(h http.Handler) http.Handler {
server.next = h
return server
router.Use(func(handler http.Handler) http.Handler {
return cloneServer(server, handler)
})

return &AuthRouter{
Expand All @@ -148,7 +152,14 @@ func Setup(router *res.Router, config Config) *AuthRouter {
}
}

func middleware(config Config) *server {
func cloneServer(src *server, next http.Handler) *server {
clone := &server{}
*clone = *src
clone.next = next
return clone
}

func newServer(config Config) *server {

if config.CredentialChecker == nil {
log.Fatal("github.com/ntbosscher/gobase/auth/authhttp.Middleware(config): config requires CredentialChecker")
Expand All @@ -163,18 +174,13 @@ func middleware(config Config) *server {
}

type server struct {
next http.Handler
next http.Handler
perRequestFilter PerRequestFilter
ignoreRoutesWithPrefixes []string
ignoreRoutes []string
authHandler func(request *res.Request) (res.Responder, context.Context)
}

const defaultLoginEndpoint = "/api/auth/#"
const defaultRefreshEndpoint = "/api/auth/refresh"
const defaultLogoutEndpoint = "/api/auth/logout"
const defaultRegisterEndpoint = "/api/auth/register"

func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {

ignoredRoute := false
Expand Down

0 comments on commit a8d40bc

Please # to comment.