From 569978b4f77afd416add44b8a5ea36a362d0bf79 Mon Sep 17 00:00:00 2001 From: Michael Desa Date: Wed, 1 Nov 2017 12:34:00 -0400 Subject: [PATCH] Add parseOrganizationID method --- server/auth.go | 3 +-- server/me.go | 3 +-- server/organizations.go | 10 +++++++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/server/auth.go b/server/auth.go index 67e4218cb4..d89cb83b2b 100644 --- a/server/auth.go +++ b/server/auth.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "net/http" - "strconv" "github.com/influxdata/chronograf" "github.com/influxdata/chronograf/oauth2" @@ -93,7 +92,7 @@ func AuthorizedUser( } // validate that the organization exists - orgID, err := strconv.ParseUint(p.Organization, 10, 64) + orgID, err := parseOrganizationID(p.Organization) if err != nil { log.Error("Failed to validate organization on context") Error(w, http.StatusUnauthorized, "User is not authorized", logger) diff --git a/server/me.go b/server/me.go index 9bc555ecfc..74f607b80e 100644 --- a/server/me.go +++ b/server/me.go @@ -4,7 +4,6 @@ import ( "encoding/json" "fmt" "net/http" - "strconv" "golang.org/x/net/context" @@ -116,7 +115,7 @@ func (s *Service) MeOrganization(auth oauth2.Authenticator) func(http.ResponseWr } // validate that the organization exists - orgID, err := strconv.ParseUint(req.Organization, 10, 64) + orgID, err := parseOrganizationID(req.Organization) if err != nil { Error(w, http.StatusInternalServerError, err.Error(), s.Logger) return diff --git a/server/organizations.go b/server/organizations.go index 1c3fc95a6e..58273131af 100644 --- a/server/organizations.go +++ b/server/organizations.go @@ -10,6 +10,10 @@ import ( "github.com/influxdata/chronograf" ) +func parseOrganizationID(id string) (uint64, error) { + return strconv.ParseUint(id, 10, 64) +} + type organizationRequest struct { Name string `json:"name"` } @@ -110,7 +114,7 @@ func (s *Service) OrganizationID(w http.ResponseWriter, r *http.Request) { ctx := r.Context() idStr := httprouter.GetParamFromContext(ctx, "id") - id, err := strconv.ParseUint(idStr, 10, 64) + id, err := parseOrganizationID(idStr) if err != nil { Error(w, http.StatusBadRequest, fmt.Sprintf("invalid organization id: %s", err.Error()), s.Logger) return @@ -140,7 +144,7 @@ func (s *Service) UpdateOrganization(w http.ResponseWriter, r *http.Request) { ctx := r.Context() idStr := httprouter.GetParamFromContext(ctx, "id") - id, err := strconv.ParseUint(idStr, 10, 64) + id, err := parseOrganizationID(idStr) if err != nil { Error(w, http.StatusBadRequest, fmt.Sprintf("invalid organization id: %s", err.Error()), s.Logger) return @@ -170,7 +174,7 @@ func (s *Service) UpdateOrganization(w http.ResponseWriter, r *http.Request) { func (s *Service) RemoveOrganization(w http.ResponseWriter, r *http.Request) { ctx := r.Context() idStr := httprouter.GetParamFromContext(ctx, "id") - id, err := strconv.ParseUint(idStr, 10, 64) + id, err := parseOrganizationID(idStr) if err != nil { Error(w, http.StatusBadRequest, fmt.Sprintf("invalid organization id: %s", err.Error()), s.Logger) return