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"
7 "github.com/secondbit/pan"
10 func (t Token) GetSQLTableName() string {
14 func (p *postgres) getTokenSQL(token string, refresh bool) *pan.Query {
16 fields, _ := pan.GetFields(t)
17 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(t))
20 query.Include(pan.GetUnquotedColumn(t, "AccessToken")+" = ?", token)
22 query.Include(pan.GetUnquotedColumn(t, "RefreshToken")+" = ?", token)
24 return query.FlushExpressions(" ")
27 func (p *postgres) getToken(token string, refresh bool) (Token, error) {
28 query := p.getTokenSQL(token, refresh)
29 rows, err := p.db.Query(query.String(), query.Args...)
36 err := pan.Unmarshal(rows, &t)
42 if err = rows.Err(); err != nil {
46 return t, ErrTokenNotFound
51 func (p *postgres) saveTokenSQL(token Token) *pan.Query {
52 fields, values := pan.GetFields(token)
53 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(token))
54 query.Include("(" + pan.QueryList(fields) + ")")
55 query.Include("VALUES")
56 query.Include("("+pan.VariableList(len(values))+")", values...)
57 return query.FlushExpressions(" ")
60 func (p *postgres) saveToken(token Token) error {
61 query := p.saveTokenSQL(token)
62 _, err := p.db.Exec(query.String(), query.Args...)
63 if e, ok := err.(*pq.Error); ok && e.Constraint == "tokens_pkey" {
64 err = ErrTokenAlreadyExists
66 if err != nil || len(token.Scopes) < 1 {
72 func (p *postgres) revokeTokenSQL(token string, refresh bool) *pan.Query {
74 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(t)+" SET ")
75 query.Include(pan.GetUnquotedColumn(t, "Revoked")+" = ?", true)
78 query.Include(pan.GetUnquotedColumn(t, "AccessToken")+" = ?", token)
80 query.Include(pan.GetUnquotedColumn(t, "RefreshToken")+" = ?", token)
82 return query.FlushExpressions(" ")
85 func (p *postgres) revokeToken(token string, refresh bool) error {
86 query := p.revokeTokenSQL(token, refresh)
87 res, err := p.db.Exec(query.String(), query.Args...)
91 rows, err := res.RowsAffected()
96 return ErrTokenNotFound
101 func (p *postgres) revokeTokensByProfileIDSQL(profileID uuid.ID) *pan.Query {
103 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(t)+" SET ")
104 query.Include(pan.GetUnquotedColumn(t, "Revoked")+" = ?", true)
106 query.Include(pan.GetUnquotedColumn(t, "ProfileID")+" = ?", profileID)
107 return query.FlushExpressions(" ")
110 func (p *postgres) revokeTokensByProfileID(profileID uuid.ID) error {
111 query := p.revokeTokensByProfileIDSQL(profileID)
112 res, err := p.db.Exec(query.String(), query.Args...)
116 rows, err := res.RowsAffected()
121 return ErrProfileNotFound
126 func (p *postgres) revokeTokensByClientIDSQL(clientID uuid.ID) *pan.Query {
128 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(t)+" SET ")
129 query.Include(pan.GetUnquotedColumn(t, "Revoked")+" = ?", true)
131 query.Include(pan.GetUnquotedColumn(t, "ClientID")+" = ?", clientID)
132 return query.FlushExpressions(" ")
135 func (p *postgres) revokeTokensByClientID(clientID uuid.ID) error {
136 query := p.revokeTokensByClientIDSQL(clientID)
137 res, err := p.db.Exec(query.String(), query.Args...)
141 rows, err := res.RowsAffected()
146 return ErrClientNotFound
151 func (p *postgres) getTokensByProfileIDSQL(profileID uuid.ID, num, offset int) *pan.Query {
153 fields, _ := pan.GetFields(token)
154 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(token))
156 query.Include(pan.GetUnquotedColumn(token, "ProfileID")+" = ?", profileID)
157 query.IncludeLimit(int64(num))
158 query.IncludeOffset(int64(offset))
159 return query.FlushExpressions(" ")
162 func (p *postgres) getTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error) {
163 query := p.getTokensByProfileIDSQL(profileID, num, offset)
164 rows, err := p.db.Query(query.String(), query.Args...)
166 return []Token{}, err
169 var tokenIDs []string
172 err = pan.Unmarshal(rows, &token)
176 tokens = append(tokens, token)
177 tokenIDs = append(tokenIDs, token.AccessToken)
179 if err = rows.Err(); err != nil {