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

Unified Diff: server/auth/signing/context.go

Issue 2043423004: Make HTTP middleware easier to use (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: gaemiddleware: add middleware func for WithProd 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
« no previous file with comments | « server/auth/openid/method_test.go ('k') | server/auth/signing/context_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: server/auth/signing/context.go
diff --git a/server/auth/signing/context.go b/server/auth/signing/context.go
index 9cb0f6ae8a28d840fcb7f24af9a5cd641365f39d..2c69f139c691a2f61b872807d3e49d99dfda047e 100644
--- a/server/auth/signing/context.go
+++ b/server/auth/signing/context.go
@@ -3,24 +3,22 @@
// that can be found in the LICENSE file.
package signing
import (
"encoding/json"
"errors"
"fmt"
"net/http"
- "github.com/julienschmidt/httprouter"
+ "github.com/luci/luci-go/server/router"
"golang.org/x/net/context"
-
- "github.com/luci/luci-go/server/middleware"
)
type contextKey int
// SetSigner injects Signer into the context.
func SetSigner(c context.Context, s Signer) context.Context {
return context.WithValue(c, contextKey(0), s)
}
// GetSigner extracts Signer from the context. Returns nil if no Signer is set.
@@ -36,42 +34,42 @@ func GetSigner(c context.Context) Signer {
func SignBytes(c context.Context, blob []byte) (keyName string, signature []byte, err error) {
if s := GetSigner(c); s != nil {
return s.SignBytes(c, blob)
}
return "", nil, errors.New("signature: no Signer in the context")
}
// InstallHandlers installs a handler that serves public certificates provided
// by the signer inside the base context. FetchCertificates is hitting this
// handler.
-func InstallHandlers(r *httprouter.Router, base middleware.Base) {
- r.GET("/auth/api/v1/server/certificates", base(certsHandler))
+func InstallHandlers(r *router.Router, base router.MiddlewareChain) {
+ r.GET("/auth/api/v1/server/certificates", base, certsHandler)
}
// certsHandler servers public certificates of the signer in the context.
-func certsHandler(c context.Context, rw http.ResponseWriter, r *http.Request, p httprouter.Params) {
+func certsHandler(c *router.Context) {
reply := func(code int, out interface{}) {
- rw.Header().Set("Content-Type", "application/json")
- rw.WriteHeader(code)
- json.NewEncoder(rw).Encode(out)
+ c.Writer.Header().Set("Content-Type", "application/json")
+ c.Writer.WriteHeader(code)
+ json.NewEncoder(c.Writer).Encode(out)
}
replyError := func(code int, msg string) {
errorReply := struct {
Error string `json:"error"`
}{msg}
reply(code, &errorReply)
}
- s := GetSigner(c)
+ s := GetSigner(c.Context)
if s == nil {
replyError(http.StatusNotFound, "No Signer instance available")
return
}
- certs, err := s.Certificates(c)
+ certs, err := s.Certificates(c.Context)
if err != nil {
replyError(http.StatusInternalServerError, fmt.Sprintf("Can't fetch certificates - %s", err))
} else {
reply(http.StatusOK, certs)
}
}
« no previous file with comments | « server/auth/openid/method_test.go ('k') | server/auth/signing/context_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698