auth
auth/profile.go
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.
1.1 --- a/profile.go Sat Apr 11 15:37:41 2015 -0400 1.2 +++ b/profile.go Sat Apr 11 17:23:15 2015 -0400 1.3 @@ -265,6 +265,7 @@ 1.4 1.5 addLogin(login Login) error 1.6 removeLogin(value string, profile uuid.ID) error 1.7 + removeLoginsByProfile(profile uuid.ID) error 1.8 recordLoginUse(value string, when time.Time) error 1.9 listLogins(profile uuid.ID, num, offset int) ([]Login, error) 1.10 } 1.11 @@ -375,6 +376,20 @@ 1.12 return nil 1.13 } 1.14 1.15 +func (m *memstore) removeLoginsByProfile(profile uuid.ID) error { 1.16 + m.loginLock.Lock() 1.17 + defer m.loginLock.Unlock() 1.18 + logins, ok := m.profileLoginLookup[profile.String()] 1.19 + if !ok { 1.20 + return ErrProfileNotFound 1.21 + } 1.22 + delete(m.profileLoginLookup, profile.String()) 1.23 + for _, login := range logins { 1.24 + delete(m.logins, login) 1.25 + } 1.26 + return nil 1.27 +} 1.28 + 1.29 func (m *memstore) recordLoginUse(value string, when time.Time) error { 1.30 m.loginLock.Lock() 1.31 defer m.loginLock.Unlock() 1.32 @@ -412,12 +427,22 @@ 1.33 return logins, nil 1.34 } 1.35 1.36 +func cleanUpAfterProfileDeletion(profile uuid.ID, context Context) { 1.37 + err := context.RemoveLoginsByProfile(profile) 1.38 + if err != nil { 1.39 + log.Printf("Error removing logins from profile %s: %+v\n", profile, err) 1.40 + } 1.41 + // BUG(paddy): need to terminate all sessions associated with the Profile 1.42 + // BUG(paddy): need to invalidate all tokens associated with the Profile 1.43 + // BUG(paddy): need to delete all the grants associated with the Profile 1.44 +} 1.45 + 1.46 // RegisterProfileHandlers adds handlers to the passed router to handle the profile endpoints, like registration and user retrieval. 1.47 func RegisterProfileHandlers(r *mux.Router, context Context) { 1.48 r.Handle("/profiles", wrap(context, CreateProfileHandler)).Methods("POST") 1.49 // BUG(paddy): We need to implement a handler that will return information about a profile or set of profiles. 1.50 r.Handle("/profiles/{id}", wrap(context, UpdateProfileHandler)).Methods("PATCH") 1.51 - // BUG(paddy): We need to implement a handler that will delete a profile. What happens to clients/tokens/grants/sessions when a profile is deleted? 1.52 + r.Handle("/profiles/{id}", wrap(context, DeleteProfileHandler)).Methods("DELETE") 1.53 // BUG(paddy): We need to implement a handler that will add a login to a profile. 1.54 // BUG(paddy): We need to implement a handler that will remove a login from a profile. What happens to sessions created with that login? 1.55 // BUG(paddy): We need to implement a handler that will list the logins attached to a profile. 1.56 @@ -605,7 +630,64 @@ 1.57 encode(w, r, http.StatusInternalServerError, actOfGodResponse) 1.58 return 1.59 } 1.60 + if !profile.Deleted && req.Deleted != nil && *req.Deleted { 1.61 + go cleanUpAfterProfileDeletion(profile.ID, context) 1.62 + } 1.63 profile.ApplyChange(req) 1.64 encode(w, r, http.StatusOK, response{Profiles: []Profile{profile}}) 1.65 return 1.66 } 1.67 + 1.68 +func DeleteProfileHandler(w http.ResponseWriter, r *http.Request, context Context) { 1.69 + errors := []requestError{} 1.70 + vars := mux.Vars(r) 1.71 + if vars["id"] == "" { 1.72 + errors = append(errors, requestError{Slug: requestErrMissing, Param: "id"}) 1.73 + encode(w, r, http.StatusBadRequest, response{Errors: errors}) 1.74 + return 1.75 + } 1.76 + id, err := uuid.Parse(vars["id"]) 1.77 + if err != nil { 1.78 + errors = append(errors, requestError{Slug: requestErrAccessDenied}) 1.79 + encode(w, r, http.StatusBadRequest, response{Errors: errors}) 1.80 + return 1.81 + } 1.82 + username, password, ok := r.BasicAuth() 1.83 + if !ok { 1.84 + errors = append(errors, requestError{Slug: requestErrAccessDenied}) 1.85 + encode(w, r, http.StatusUnauthorized, response{Errors: errors}) 1.86 + return 1.87 + } 1.88 + profile, err := authenticate(username, password, context) 1.89 + if err != nil { 1.90 + if isAuthError(err) { 1.91 + errors = append(errors, requestError{Slug: requestErrAccessDenied}) 1.92 + encode(w, r, http.StatusUnauthorized, response{Errors: errors}) 1.93 + } else { 1.94 + errors = append(errors, requestError{Slug: requestErrActOfGod}) 1.95 + encode(w, r, http.StatusInternalServerError, response{Errors: errors}) 1.96 + } 1.97 + return 1.98 + } 1.99 + if !profile.ID.Equal(id) { 1.100 + errors = append(errors, requestError{Slug: requestErrAccessDenied}) 1.101 + encode(w, r, http.StatusForbidden, response{Errors: errors}) 1.102 + return 1.103 + } 1.104 + var change ProfileChange 1.105 + deleted := true 1.106 + change.Deleted = &deleted 1.107 + err = context.UpdateProfile(id, change) 1.108 + if err != nil { 1.109 + if err == ErrProfileNotFound { 1.110 + errors = append(errors, requestError{Slug: requestErrNotFound}) 1.111 + encode(w, r, http.StatusNotFound, response{Errors: errors}) 1.112 + return 1.113 + } 1.114 + encode(w, r, http.StatusInternalServerError, actOfGodResponse) 1.115 + return 1.116 + } 1.117 + profile.ApplyChange(change) 1.118 + encode(w, r, http.StatusOK, response{Profiles: []Profile{profile}}) 1.119 + go cleanUpAfterProfileDeletion(profile.ID, context) 1.120 +}