diff --git a/http/api_handler.go b/http/api_handler.go index f82d4651bc..6da1a45985 100644 --- a/http/api_handler.go +++ b/http/api_handler.go @@ -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 diff --git a/http/user_service.go b/http/user_service.go index 1e74a458a5..d58945056e 100644 --- a/http/user_service.go +++ b/http/user_service.go @@ -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()