Add a me endpoint for chronograf
Returns the currently signed in user from session or tokenpull/10616/head
parent
ad74678200
commit
d65008d24b
|
@ -200,6 +200,11 @@ func (h *APIHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
if strings.HasPrefix(r.URL.Path, "/api/v2/me") {
|
||||
h.UserHandler.ServeHTTP(w, r)
|
||||
return
|
||||
}
|
||||
|
||||
if strings.HasPrefix(r.URL.Path, "/api/v2/orgs") {
|
||||
h.OrgHandler.ServeHTTP(w, r)
|
||||
return
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"path"
|
||||
|
||||
"github.com/influxdata/platform"
|
||||
platcontext "github.com/influxdata/platform/context"
|
||||
kerrors "github.com/influxdata/platform/kit/errors"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
)
|
||||
|
@ -26,6 +27,7 @@ func NewUserHandler() *UserHandler {
|
|||
}
|
||||
|
||||
h.HandlerFunc("POST", "/api/v2/users", h.handlePostUser)
|
||||
h.HandlerFunc("GET", "/api/v2/me", h.handleGetMe)
|
||||
h.HandlerFunc("GET", "/api/v2/users", h.handleGetUsers)
|
||||
h.HandlerFunc("GET", "/api/v2/users/:id", h.handleGetUser)
|
||||
h.HandlerFunc("PATCH", "/api/v2/users/:id", h.handlePatchUser)
|
||||
|
@ -69,6 +71,36 @@ func decodePostUserRequest(ctx context.Context, r *http.Request) (*postUserReque
|
|||
}, nil
|
||||
}
|
||||
|
||||
// handleGetUser is the HTTP handler for the GET /api/v2/me.
|
||||
func (h *UserHandler) handleGetMe(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
a, err := platcontext.GetAuthorizer(ctx)
|
||||
if err != nil {
|
||||
EncodeError(ctx, err, w)
|
||||
return
|
||||
}
|
||||
|
||||
id := []byte{}
|
||||
switch s := a.(type) {
|
||||
case *platform.Session:
|
||||
id = s.UserID
|
||||
case *platform.Authorization:
|
||||
id = s.UserID
|
||||
}
|
||||
|
||||
b, err := h.UserService.FindUserByID(ctx, id)
|
||||
if err != nil {
|
||||
EncodeError(ctx, err, w)
|
||||
return
|
||||
}
|
||||
|
||||
if err := encodeResponse(ctx, w, http.StatusOK, b); err != nil {
|
||||
EncodeError(ctx, err, w)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// handleGetUser is the HTTP handler for the GET /api/v2/users/:id route.
|
||||
func (h *UserHandler) handleGetUser(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
|
Loading…
Reference in New Issue