Start to support deleting profiles through the API.
Create a removeLoginsByProfile method on the profileStore, to allow an easy way
to bulk-delete logins associated with a Profile after the Profile has been
deleted.
Create postgres and memstore implementations of the removeLoginsByProfile
method.
Create a cleanUpAfterProfileDeletion helper method that will clean up the child
objects of a Profile (its Sessions, Tokens, Clients, etc.). The intended usage
is to call this in a goroutine after a Profile has been deleted, to try and get
things back in order.
Detect when the UpdateProfileHandler API is used to set the Deleted flag of a
Profile to true, and clean up after the Profile when that's the case.
Add a DeleteProfileHandler API endpoint that is a shortcut to setting the
Deleted flag of a Profile to true and cleaning up after the Profile.
The problem with our approach thus far is that some of it is reversible and some
is not. If a Profile is maliciously/accidentally deleted, it's simple enough to
use the API as a superuser to restore the Profile. But doing that will not (and
cannot) restore the Logins associated with that Profile, for example. While it
would be nice to add a Deleted flag to our Logins that we could simply toggle,
that would wreak havoc with our database constraints and ensuring uniqueness of
Login values. I still don't have a solution for this, outside the superuser
manually restoring a Login for the Profile, after which the user can
authenticate themselves and add more Logins as desired. But there has to be a
better way.
I suppose since the passphrase is being stored with the Profile and not the
Login, we could offer an endpoint that would automate this, but... well, that
would be tricky. It would require the user remembering their Profile ID, and
let's be honest, nobody's going to remember a UUID.
Maybe such an endpoint would help from a customer service standpoint: we
identify their Profile manually, then send them to /profiles/ID/restorelogin or
something, and that lets them add a Login back to the Profile.
I'll figure it out later. For now, we know we at least have enough information
to identify a user is who they say they are and resolve the situation manually.
8 "bitbucket.org/ww/goautoneg"
12 requestErrAccessDenied = "access_denied"
13 requestErrInsufficient = "insufficient"
14 requestErrOverflow = "overflow"
15 requestErrInvalidValue = "invalid_value"
16 requestErrInvalidFormat = "invalid_format"
17 requestErrMissing = "missing"
18 requestErrNotFound = "not_found"
19 requestErrConflict = "conflict"
20 requestErrActOfGod = "act_of_god"
24 actOfGodResponse = response{Errors: []requestError{requestError{Slug: requestErrActOfGod}}}
25 invalidFormatResponse = response{Errors: []requestError{requestError{Slug: requestErrInvalidFormat, Field: "/"}}}
27 encoders = []string{"application/json"}
30 type response struct {
31 Errors []requestError `json:"errors,omitempty"`
32 Logins []Login `json:"logins,omitempty"`
33 Profiles []Profile `json:"profiles,omitempty"`
34 Clients []Client `json:"clients,omitempty"`
35 Endpoints []Endpoint `json:"endpoints,omitempty"`
36 Sessions []Session `json:"sessions,omitempty"`
39 type requestError struct {
40 Slug string `json:"error,omitempty"`
41 Field string `json:"field,omitempty"`
42 Param string `json:"param,omitempty"`
43 Header string `json:"header,omitempty"`
46 func negotiate(h http.Handler) http.Handler {
47 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
48 if r.Header.Get("Accept") != "" {
49 contentType := goautoneg.Negotiate(r.Header.Get("Accept"), encoders)
50 if contentType == "" {
51 w.WriteHeader(http.StatusNotAcceptable)
52 w.Write([]byte("Unsupported content type requested: " + r.Header.Get("Accept")))
60 func encode(w http.ResponseWriter, r *http.Request, status int, resp response) {
61 contentType := goautoneg.Negotiate(r.Header.Get("Accept"), encoders)
62 w.Header().Set("content-type", contentType)
66 case "application/json":
67 enc := json.NewEncoder(w)
68 err = enc.Encode(resp)
70 enc := json.NewEncoder(w)
71 err = enc.Encode(resp)
78 func decode(r *http.Request, target interface{}) error {
80 switch r.Header.Get("Content-Type") {
81 case "application/json":
82 dec := json.NewDecoder(r.Body)
83 return dec.Decode(target)
85 dec := json.NewDecoder(r.Body)
86 return dec.Decode(target)
90 func wrap(context Context, f func(w http.ResponseWriter, r *http.Request, context Context)) http.Handler {
91 return negotiate(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {