Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(556)

Unified Diff: server/auth/context.go

Issue 2043423004: Make HTTP middleware easier to use (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Update tests Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: server/auth/context.go
diff --git a/server/auth/context.go b/server/auth/context.go
index 035d59fb1472ae58d5c8b58da3e21409c92e40fe..5e84fd292944bc799f82d9fdcf13f7d830351f0e 100644
--- a/server/auth/context.go
+++ b/server/auth/context.go
@@ -8,14 +8,13 @@ import (
"fmt"
"net/http"
- "github.com/julienschmidt/httprouter"
"golang.org/x/net/context"
"github.com/luci/luci-go/common/errors"
"github.com/luci/luci-go/common/logging"
"github.com/luci/luci-go/server/auth/identity"
- "github.com/luci/luci-go/server/middleware"
+ "github.com/luci/luci-go/server/router"
)
type authenticatorKey int
@@ -37,9 +36,9 @@ func GetAuthenticator(c context.Context) Authenticator {
}
// Use is a middleware that simply puts given Authenticator into the context.
-func Use(h middleware.Handler, a Authenticator) middleware.Handler {
- return func(c context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
- h(SetAuthenticator(c, a), rw, r, p)
+func Use(a Authenticator) router.Handler {
+ return func(c *router.Context) {
+ c.Context = SetAuthenticator(c.Context, a)
}
}
@@ -59,21 +58,24 @@ func LogoutURL(c context.Context, dest string) (string, error) {
// Authenticate returns a wrapper around middleware.Handler that performs
// authentication (using Authenticator in the context) and calls `h`.
-func Authenticate(h middleware.Handler) middleware.Handler {
- return func(c context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
- a := GetAuthenticator(c)
+func Authenticate() router.Handler {
+ return func(c *router.Context) {
+ a := GetAuthenticator(c.Context)
if a == nil {
- replyError(c, rw, 500, "Authentication middleware is not configured")
+ replyError(c.Context, c.Writer, 500, "Authentication middleware is not configured")
+ c.Abort()
return
}
- ctx, err := a.Authenticate(c, r)
+ ctx, err := a.Authenticate(c.Context, c.Request)
switch {
case errors.IsTransient(err):
- replyError(c, rw, 500, fmt.Sprintf("Transient error during authentication - %s", err))
+ replyError(c.Context, c.Writer, 500, fmt.Sprintf("Transient error during authentication - %s", err))
+ c.Abort()
case err != nil:
- replyError(c, rw, 401, fmt.Sprintf("Authentication error - %s", err))
+ replyError(c.Context, c.Writer, 401, fmt.Sprintf("Authentication error - %s", err))
+ c.Abort()
default:
- h(ctx, rw, r, p)
+ c.Context = ctx
}
}
}
@@ -81,44 +83,48 @@ func Authenticate(h middleware.Handler) middleware.Handler {
// Autologin is a middleware that redirects the user to login page if the user
// is not signed in yet or authentication methods do not recognize user
// credentials. Uses Authenticator instance in the context.
-func Autologin(h middleware.Handler) middleware.Handler {
- return func(c context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
- a := GetAuthenticator(c)
+func Autologin() router.Handler {
+ return func(c *router.Context) {
+ a := GetAuthenticator(c.Context)
if a == nil {
- replyError(c, rw, 500, "Authentication middleware is not configured")
+ replyError(c.Context, c.Writer, 500, "Authentication middleware is not configured")
+ c.Abort()
return
}
- ctx, err := a.Authenticate(c, r)
+ ctx, err := a.Authenticate(c.Context, c.Request)
switch {
case errors.IsTransient(err):
- replyError(c, rw, 500, fmt.Sprintf("Transient error during authentication - %s", err))
+ replyError(c.Context, c.Writer, 500, fmt.Sprintf("Transient error during authentication - %s", err))
+ c.Abort()
case err != nil:
- replyError(c, rw, 401, fmt.Sprintf("Authentication error - %s", err))
+ replyError(c.Context, c.Writer, 401, fmt.Sprintf("Authentication error - %s", err))
+ c.Abort()
case CurrentIdentity(ctx).Kind() == identity.Anonymous:
- dest := r.RequestURI
+ dest := c.Request.RequestURI
if dest == "" {
// Make r.URL relative.
- destURL := *r.URL
+ destURL := *c.Request.URL
destURL.Host = ""
destURL.Scheme = ""
dest = destURL.String()
}
- url, err := a.LoginURL(c, dest)
+ url, err := a.LoginURL(c.Context, dest)
if err != nil {
if errors.IsTransient(err) {
- replyError(c, rw, 500, fmt.Sprintf("Transient error during authentication - %s", err))
+ replyError(c.Context, c.Writer, 500, fmt.Sprintf("Transient error during authentication - %s", err))
} else {
- replyError(c, rw, 401, fmt.Sprintf("Authentication error - %s", err))
+ replyError(c.Context, c.Writer, 401, fmt.Sprintf("Authentication error - %s", err))
}
+ c.Abort()
return
}
- http.Redirect(rw, r, url, 302)
+ http.Redirect(c.Writer, c.Request, url, 302)
default:
- h(ctx, rw, r, p)
+ c.Context = ctx
}
}
}

Powered by Google App Engine
This is Rietveld 408576698