auth
144:93c758f57c69 Browse Files
Add a handler to remove a Client. Add a http.Handler that will allow us to remove a Client through the API.
1.1 --- a/client.go Sat Mar 07 23:06:52 2015 -0500 1.2 +++ b/client.go Mon Mar 16 22:36:12 2015 -0400 1.3 @@ -410,9 +410,9 @@ 1.4 r.Handle("/clients", wrap(context, ListClientsHandler)).Methods("GET") 1.5 r.Handle("/clients/{id}", wrap(context, GetClientHandler)).Methods("GET") 1.6 r.Handle("/clients/{id}", wrap(context, UpdateClientHandler)).Methods("PATCH") 1.7 - // BUG(paddy): We need to implement a handler to delete a client. Also, what should that do with the grants and tokens belonging to that client? 1.8 + r.Handle("/clients/{id}", wrap(context, RemoveClientHandler)).Methods("DELETE") 1.9 r.Handle("/clients/{id}/endpoints", wrap(context, AddEndpointsHandler)).Methods("POST") 1.10 - // BUG(paddy): We need to implement a handler to remove an endpoint from a client. 1.11 + r.Handle("/clients/{client_id}/endpoints/{id}", wrap(context, RemoveEndpointHandler)).Methods("DELETE") 1.12 r.Handle("/clients/{id}/endpoints", wrap(context, ListEndpointsHandler)).Methods("GET") 1.13 } 1.14 1.15 @@ -743,6 +743,69 @@ 1.16 return 1.17 } 1.18 1.19 +func RemoveClientHandler(w http.ResponseWriter, r *http.Request, c Context) { 1.20 + errors := []requestError{} 1.21 + vars := mux.Vars(r) 1.22 + if _, ok := vars["id"]; !ok { 1.23 + errors = append(errors, requestError{Slug: requestErrMissing, Param: "id"}) 1.24 + encode(w, r, http.StatusNotFound, response{Errors: errors}) 1.25 + return 1.26 + } 1.27 + id, err := uuid.Parse(vars["id"]) 1.28 + if err != nil { 1.29 + errors = append(errors, requestError{Slug: requestErrNotFound, Param: "id"}) 1.30 + } 1.31 + username, password, ok := r.BasicAuth() 1.32 + if !ok { 1.33 + errors = append(errors, requestError{Slug: requestErrAccessDenied}) 1.34 + encode(w, r, http.StatusUnauthorized, response{Errors: errors}) 1.35 + return 1.36 + } 1.37 + profile, err := authenticate(username, password, c) 1.38 + if err != nil { 1.39 + if isAuthError(err) { 1.40 + errors = append(errors, requestError{Slug: requestErrAccessDenied}) 1.41 + encode(w, r, http.StatusUnauthorized, response{Errors: errors}) 1.42 + } else { 1.43 + errors = append(errors, requestError{Slug: requestErrActOfGod}) 1.44 + encode(w, r, http.StatusInternalServerError, response{Errors: errors}) 1.45 + } 1.46 + return 1.47 + } 1.48 + client, err := c.GetClient(id) 1.49 + if err != nil { 1.50 + if err == ErrClientNotFound { 1.51 + errors = append(errors, requestError{Slug: requestErrNotFound}) 1.52 + encode(w, r, http.StatusNotFound, response{Errors: errors}) 1.53 + return 1.54 + } 1.55 + log.Println("Error retrieving client:", err) 1.56 + errors = append(errors, requestError{Slug: requestErrActOfGod}) 1.57 + encode(w, r, http.StatusInternalServerError, response{Errors: errors}) 1.58 + return 1.59 + } 1.60 + if !client.OwnerID.Equal(profile.ID) { 1.61 + errors = append(errors, requestError{Slug: requestErrAccessDenied}) 1.62 + encode(w, r, http.StatusForbidden, response{Errors: errors}) 1.63 + return 1.64 + } 1.65 + err = c.DeleteClient(id) 1.66 + if err != nil { 1.67 + if err == ErrClientNotFound { 1.68 + errors = append(errors, requestError{Slug: requestErrNotFound}) 1.69 + encode(w, r, http.StatusNotFound, response{Errors: errors}) 1.70 + return 1.71 + } 1.72 + log.Println("Error deleting client:", err) 1.73 + errors = append(errors, requestError{Slug: requestErrActOfGod}) 1.74 + encode(w, r, http.StatusInternalServerError, response{Errors: errors}) 1.75 + return 1.76 + } 1.77 + // BUG(paddy): Client needs to clean up after itself, invalidating tokens, deleting unused grants, deleting endpoints 1.78 + encode(w, r, http.StatusOK, response{Errors: errors}) 1.79 + return 1.80 +} 1.81 + 1.82 func AddEndpointsHandler(w http.ResponseWriter, r *http.Request, c Context) { 1.83 type addEndpointReq struct { 1.84 Endpoints []string `json:"endpoints"`