Clean up after Client deletion, finish cleaning up after Profile deletion.
6f473576c6ae started cleaning up after Profiles when they're deleted, but
didn't clean up the Clients created by that Profile. This fixes that, and also
fixes a BUG note about cleaning up after a Client when it's deleted.
Extend the authorizationCodeStore to have a deleteAuthorizationCodesByClientID
method that will delete the AuthorizationCodes that have been granted by the
Client specified by the passed ID. We also implemented this in memstore and
postgres, so tests continue to pass.
Extend the clientStore to have a deleteClientsByOwner method that will delete
the Clients that were created by the Profile specified by the passed ID. We also
implemented this in memstore and postgres, so tests continue to pass.
Extend the clientStore to have a removeEndpointsByClientID method that will
delete the Endpoints that belong(ed) to a the Client specified by the passed ID.
We also implemented this in memstore and postgres, so tests continue to pass.
Extend the tokenStore to have a revokeTokensByClientID method that will revoke
all the Tokens that were granted to the Client specified by the passed ID. We
also implemented this in memstore and postgres, so tests continue to pass.
When listing Clients by their owner, allow setting the num argument (which
controls how many to return) to 0 or lower, and using that to signal "return all
Clients belonging to this owner", instead of paging. This is useful when
deleting the Clients belonging to a Profile as part of the cleanup after
deleting the Profile.
Create a cleanUpAfterClientDeletion helper function that will delete the
Endpoints and AuthorizationCodes belonging to a Client, and revoke the Tokens
belonging to a Client, as part of cleaning up after a Client has been deleted.
Add a check in the handler for listing Clients owned by a Profile to disallow
the num argument to be lower than 1, because the API should be forced to page.
Call our cleanUpAfterClientDeletion once the Client has been deleted in the
appropriate handler.
Fill out our Context with new methods to wrap all the new methods we're adding
to our *Stores.
In cleanUpAfterProfileDeletion, obtain a list of clients belonging to the owner,
use our new DeleteClientsByOwner method to remove all of them, and then use the
list to run our new cleanUpAfterClientDeletion function to clear away the final
remnants of a Profile when it's deleted.
6 "code.secondbit.org/uuid.hg"
8 "github.com/secondbit/pan"
11 func (p Profile) GetSQLTableName() string {
15 func (l Login) GetSQLTableName() string {
19 func (p *postgres) getProfileByIDSQL(id uuid.ID) *pan.Query {
21 fields, _ := pan.GetFields(profile)
22 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(profile))
24 query.Include(pan.GetUnquotedColumn(profile, "ID")+" = ?", id)
25 return query.FlushExpressions(" ")
28 func (p *postgres) getProfileByID(id uuid.ID) (Profile, error) {
29 query := p.getProfileByIDSQL(id)
30 rows, err := p.db.Query(query.String(), query.Args...)
37 err := pan.Unmarshal(rows, &profile)
43 if err := rows.Err(); err != nil {
47 return profile, ErrProfileNotFound
52 func (p *postgres) getProfileByLoginSQL(value string) *pan.Query {
55 fields, _ := pan.GetUnquotedAbsoluteFields(profile)
56 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(profile))
57 query.Include("INNER JOIN " + pan.GetTableName(login))
58 query.Include("ON " + pan.GetUnquotedAbsoluteColumn(profile, "ID") + " = " + pan.GetUnquotedAbsoluteColumn(login, "ProfileID"))
60 query.Include(pan.GetUnquotedAbsoluteColumn(login, "Value")+" = ?", value)
61 return query.FlushExpressions(" ")
64 func (p *postgres) getProfileByLogin(value string) (Profile, error) {
65 query := p.getProfileByLoginSQL(value)
66 rows, err := p.db.Query(query.String(), query.Args...)
73 err := pan.Unmarshal(rows, &profile)
79 if err := rows.Err(); err != nil {
83 return profile, ErrProfileNotFound
88 func (p *postgres) saveProfileSQL(profile Profile) *pan.Query {
89 fields, values := pan.GetFields(profile)
90 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(profile))
91 query.Include("(" + pan.QueryList(fields) + ")")
92 query.Include("VALUES")
93 query.Include("("+pan.VariableList(len(values))+")", values...)
94 return query.FlushExpressions(" ")
97 func (p *postgres) saveProfile(profile Profile) error {
98 query := p.saveProfileSQL(profile)
99 _, err := p.db.Exec(query.String(), query.Args...)
100 if e, ok := err.(*pq.Error); ok && e.Constraint == "profiles_pkey" {
101 err = ErrProfileAlreadyExists
106 func (p *postgres) updateProfileSQL(id uuid.ID, change ProfileChange) *pan.Query {
108 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(profile)+" SET ")
109 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Name")+" = ?", change.Name)
110 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Passphrase")+" = ?", change.Passphrase)
111 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Iterations")+" = ?", change.Iterations)
112 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Salt")+" = ?", change.Salt)
113 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "PassphraseScheme")+" = ?", change.PassphraseScheme)
114 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Compromised")+" = ?", change.Compromised)
115 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "LockedUntil")+" = ?", change.LockedUntil)
116 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "PassphraseReset")+" = ?", change.PassphraseReset)
117 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "PassphraseResetCreated")+" = ?", change.PassphraseResetCreated)
118 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "LastSeen")+" = ?", change.LastSeen)
119 query.FlushExpressions(", ")
121 query.Include(pan.GetUnquotedColumn(profile, "ID")+" = ?", id)
122 return query.FlushExpressions(" ")
125 func (p *postgres) updateProfile(id uuid.ID, change ProfileChange) error {
129 query := p.updateProfileSQL(id, change)
130 _, err := p.db.Exec(query.String(), query.Args...)
134 func (p *postgres) updateProfilesSQL(ids []uuid.ID, change BulkProfileChange) *pan.Query {
139 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(profile)+" SET ")
140 query.IncludeIfNotNil(pan.GetUnquotedColumn(profile, "Compromised")+" = ?", change.Compromised)
141 query.FlushExpressions(", ")
143 intids := make([]interface{}, len(ids))
144 for pos, id := range ids {
147 query.Include(pan.GetUnquotedColumn(profile, "ID")+" IN ("+pan.VariableList(len(ids))+")", intids...)
148 return query.FlushExpressions(" ")
151 func (p *postgres) updateProfiles(ids []uuid.ID, change BulkProfileChange) error {
152 query := p.updateProfilesSQL(ids, change)
153 _, err := p.db.Exec(query.String(), query.Args...)
157 func (p *postgres) deleteProfileSQL(id uuid.ID) *pan.Query {
159 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(profile))
161 query.Include(pan.GetUnquotedColumn(profile, "ID")+" = ?", id)
162 return query.FlushExpressions(" ")
165 func (p *postgres) deleteProfile(id uuid.ID) error {
166 query := p.deleteProfileSQL(id)
167 res, err := p.db.Exec(query.String(), query.Args...)
171 rows, err := res.RowsAffected()
176 return ErrProfileNotFound
181 func (p *postgres) addLoginSQL(login Login) *pan.Query {
182 fields, values := pan.GetFields(login)
183 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(login))
184 query.Include("(" + pan.QueryList(fields) + ")")
185 query.Include("VALUES")
186 query.Include("("+pan.VariableList(len(values))+")", values...)
187 return query.FlushExpressions(" ")
190 func (p *postgres) addLogin(login Login) error {
191 query := p.addLoginSQL(login)
192 _, err := p.db.Exec(query.String(), query.Args...)
193 if e, ok := err.(*pq.Error); ok && e.Constraint == "logins_pkey" {
194 return ErrLoginAlreadyExists
199 func (p *postgres) removeLoginsByProfileSQL(profile uuid.ID) *pan.Query {
201 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(login))
203 query.Include(pan.GetUnquotedColumn(login, "ProfileID")+" = ?", profile)
204 return query.FlushExpressions(" ")
207 func (p *postgres) removeLoginsByProfile(profile uuid.ID) error {
208 query := p.removeLoginsByProfileSQL(profile)
209 res, err := p.db.Exec(query.String(), query.Args...)
213 rows, err := res.RowsAffected()
218 return ErrProfileNotFound
223 func (p *postgres) removeLoginSQL(value string, profile uuid.ID) *pan.Query {
225 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(login))
227 query.Include(pan.GetUnquotedColumn(login, "Value")+" = ? AND "+pan.GetUnquotedColumn(login, "ProfileID")+" = ?", value, profile)
228 return query.FlushExpressions(" ")
231 func (p *postgres) removeLogin(value string, profile uuid.ID) error {
232 query := p.removeLoginSQL(value, profile)
233 res, err := p.db.Exec(query.String(), query.Args...)
237 rows, err := res.RowsAffected()
242 return ErrLoginNotFound
247 func (p *postgres) recordLoginUseSQL(value string, when time.Time) *pan.Query {
249 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(login)+" SET ")
250 query.Include(pan.GetUnquotedColumn(login, "LastUsed")+" = ?", when)
252 query.Include(pan.GetUnquotedColumn(login, "Value")+" = ?", value)
253 return query.FlushExpressions(" ")
256 func (p *postgres) recordLoginUse(value string, when time.Time) error {
257 query := p.recordLoginUseSQL(value, when)
258 _, err := p.db.Exec(query.String(), query.Args...)
262 func (p *postgres) listLoginsSQL(profile uuid.ID, num, offset int) *pan.Query {
264 fields, _ := pan.GetFields(login)
265 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(login))
267 query.Include(pan.GetUnquotedColumn(login, "ProfileID")+" = ?", profile)
268 query.IncludeLimit(int64(num))
269 query.IncludeOffset(int64(offset))
270 return query.FlushExpressions(" ")
273 func (p *postgres) listLogins(profile uuid.ID, num, offset int) ([]Login, error) {
274 query := p.listLoginsSQL(profile, num, offset)
275 rows, err := p.db.Query(query.String(), query.Args...)
277 return []Login{}, err
282 err = pan.Unmarshal(rows, &login)
286 logins = append(logins, login)
288 if err := rows.Err(); err != nil {