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.
4 "code.secondbit.org/uuid.hg"
6 "github.com/secondbit/pan"
9 func (ac AuthorizationCode) GetSQLTableName() string {
10 return "authorization_codes"
13 func (p *postgres) getAuthorizationCodeSQL(code string) *pan.Query {
14 var ac AuthorizationCode
15 fields, _ := pan.GetFields(ac)
16 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(ac))
18 query.Include(pan.GetUnquotedColumn(ac, "Code")+" = ?", code)
19 return query.FlushExpressions(" ")
22 func (p *postgres) getAuthorizationCode(code string) (AuthorizationCode, error) {
23 query := p.getAuthorizationCodeSQL(code)
24 rows, err := p.db.Query(query.String(), query.Args...)
26 return AuthorizationCode{}, err
28 var ac AuthorizationCode
31 err := pan.Unmarshal(rows, &ac)
37 if err = rows.Err(); err != nil {
41 return ac, ErrAuthorizationCodeNotFound
46 func (p *postgres) saveAuthorizationCodeSQL(authCode AuthorizationCode) *pan.Query {
47 fields, values := pan.GetFields(authCode)
48 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(authCode))
49 query.Include("(" + pan.QueryList(fields) + ")")
50 query.Include("VALUES")
51 query.Include("("+pan.VariableList(len(values))+")", values...)
52 return query.FlushExpressions(" ")
55 func (p *postgres) saveAuthorizationCode(authCode AuthorizationCode) error {
56 query := p.saveAuthorizationCodeSQL(authCode)
57 _, err := p.db.Exec(query.String(), query.Args...)
58 if e, ok := err.(*pq.Error); ok && e.Constraint == "authorization_codes_pkey" {
59 err = ErrAuthorizationCodeAlreadyExists
64 func (p *postgres) deleteAuthorizationCodeSQL(code string) *pan.Query {
65 var authCode AuthorizationCode
66 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(authCode))
68 query.Include(pan.GetUnquotedColumn(authCode, "Code")+" = ?", code)
69 return query.FlushExpressions(" ")
72 func (p *postgres) deleteAuthorizationCode(code string) error {
73 query := p.deleteAuthorizationCodeSQL(code)
74 res, err := p.db.Exec(query.String(), query.Args...)
78 rows, err := res.RowsAffected()
83 return ErrAuthorizationCodeNotFound
88 func (p *postgres) deleteAuthorizationCodesByProfileIDSQL(profileID uuid.ID) *pan.Query {
89 var authCode AuthorizationCode
90 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(authCode))
92 query.Include(pan.GetUnquotedColumn(authCode, "ProfileID")+" = ?", profileID)
93 return query.FlushExpressions(" ")
96 func (p *postgres) deleteAuthorizationCodesByProfileID(profileID uuid.ID) error {
97 query := p.deleteAuthorizationCodesByProfileIDSQL(profileID)
98 res, err := p.db.Exec(query.String(), query.Args...)
102 rows, err := res.RowsAffected()
107 return ErrProfileNotFound
112 func (p *postgres) deleteAuthorizationCodesByClientIDSQL(clientID uuid.ID) *pan.Query {
113 var authCode AuthorizationCode
114 query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(authCode))
116 query.Include(pan.GetUnquotedColumn(authCode, "ClientID")+" = ?", clientID)
117 return query.FlushExpressions(" ")
120 func (p *postgres) deleteAuthorizationCodesByClientID(clientID uuid.ID) error {
121 query := p.deleteAuthorizationCodesByClientIDSQL(clientID)
122 res, err := p.db.Exec(query.String(), query.Args...)
126 rows, err := res.RowsAffected()
131 return ErrClientNotFound
136 func (p *postgres) useAuthorizationCodeSQL(code string) *pan.Query {
137 var authCode AuthorizationCode
138 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(authCode)+" SET ")
139 query.Include(pan.GetUnquotedColumn(authCode, "Used")+" = ?", true)
141 query.Include(pan.GetUnquotedColumn(authCode, "Code")+" = ?", code)
142 return query.FlushExpressions(" ")
145 func (p *postgres) useAuthorizationCode(code string) error {
146 query := p.useAuthorizationCodeSQL(code)
147 res, err := p.db.Exec(query.String(), query.Args...)
151 rows, err := res.RowsAffected()
156 return ErrAuthorizationCodeNotFound