| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package auth | 5 package auth |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "encoding/json" | 8 "encoding/json" |
| 9 "net/http" | 9 "net/http" |
| 10 | 10 |
| 11 "github.com/julienschmidt/httprouter" | |
| 12 "golang.org/x/net/context" | |
| 13 | |
| 14 "github.com/luci/luci-go/common/logging" | 11 "github.com/luci/luci-go/common/logging" |
| 15 » "github.com/luci/luci-go/server/middleware" | 12 » "github.com/luci/luci-go/server/router" |
| 16 ) | 13 ) |
| 17 | 14 |
| 18 // InstallHandlers installs HTTP handlers that return information useful for | 15 // InstallHandlers installs HTTP handlers that return information useful for |
| 19 // debugging authentication issues. | 16 // debugging authentication issues. |
| 20 // | 17 // |
| 21 // This is optional. If you using appengine/gaeauth/server, these handlers are | 18 // This is optional. If you using appengine/gaeauth/server, these handlers are |
| 22 // already installed. | 19 // already installed. |
| 23 func InstallHandlers(r *httprouter.Router, base middleware.Base) { | 20 func InstallHandlers(r *router.Router, base router.MiddlewareChain) { |
| 24 » r.GET("/auth/api/v1/accounts/self", base(Authenticate(accountsSelfHandle
r))) | 21 » mc := append(base, Authenticate) |
| 22 » r.GET("/auth/api/v1/accounts/self", mc, accountsSelfHandler) |
| 25 } | 23 } |
| 26 | 24 |
| 27 // accountsSelfHandler returns JSON with information about the caller. | 25 // accountsSelfHandler returns JSON with information about the caller. |
| 28 // | 26 // |
| 29 // It can be used to verify callers' IP, access tokens, etc. | 27 // It can be used to verify callers' IP, access tokens, etc. |
| 30 func accountsSelfHandler(c context.Context, rw http.ResponseWriter, r *http.Requ
est, p httprouter.Params) { | 28 func accountsSelfHandler(c *router.Context) { |
| 31 var reply struct { | 29 var reply struct { |
| 32 Error string `json:"error,omitempty"` | 30 Error string `json:"error,omitempty"` |
| 33 Identity string `json:"identity,omitempty"` | 31 Identity string `json:"identity,omitempty"` |
| 34 IP string `json:"ip,omitempty"` | 32 IP string `json:"ip,omitempty"` |
| 35 } | 33 } |
| 36 | 34 |
| 37 » state := GetState(c) | 35 » state := GetState(c.Context) |
| 38 if state == nil { | 36 if state == nil { |
| 39 reply.Error = "Auth state is not available, application is proba
bly using auth library wrong." | 37 reply.Error = "Auth state is not available, application is proba
bly using auth library wrong." |
| 40 } else { | 38 } else { |
| 41 reply.Identity = string(state.User().Identity) | 39 reply.Identity = string(state.User().Identity) |
| 42 reply.IP = state.PeerIP().String() | 40 reply.IP = state.PeerIP().String() |
| 43 } | 41 } |
| 44 | 42 |
| 45 » rw.Header().Set("Content-Type", "application/json; charset=utf-8") | 43 » c.Writer.Header().Set("Content-Type", "application/json; charset=utf-8") |
| 46 if reply.Error == "" { | 44 if reply.Error == "" { |
| 47 » » rw.WriteHeader(http.StatusOK) | 45 » » c.Writer.WriteHeader(http.StatusOK) |
| 48 } else { | 46 } else { |
| 49 » » rw.WriteHeader(http.StatusNotImplemented) | 47 » » c.Writer.WriteHeader(http.StatusNotImplemented) |
| 50 » » logging.Errorf(c, "HTTP 501 - %s", reply.Error) | 48 » » logging.Errorf(c.Context, "HTTP 501 - %s", reply.Error) |
| 51 } | 49 } |
| 52 » json.NewEncoder(rw).Encode(&reply) | 50 » json.NewEncoder(c.Writer).Encode(&reply) |
| 53 } | 51 } |
| OLD | NEW |