auth
164:cf1aef6eb81f Browse Files
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.
authcode.go authcode_postgres.go client.go client_postgres.go context.go profile.go token.go token_postgres.go
1.1 --- a/authcode.go Sun Apr 19 23:18:26 2015 -0400 1.2 +++ b/authcode.go Sat Apr 25 22:44:36 2015 -0400 1.3 @@ -49,6 +49,7 @@ 1.4 saveAuthorizationCode(authCode AuthorizationCode) error 1.5 deleteAuthorizationCode(code string) error 1.6 deleteAuthorizationCodesByProfileID(profileID uuid.ID) error 1.7 + deleteAuthorizationCodesByClientID(clientID uuid.ID) error 1.8 useAuthorizationCode(code string) error 1.9 } 1.10 1.11 @@ -102,6 +103,24 @@ 1.12 return nil 1.13 } 1.14 1.15 +func (m *memstore) deleteAuthorizationCodesByClientID(clientID uuid.ID) error { 1.16 + m.authCodeLock.Lock() 1.17 + defer m.authCodeLock.Unlock() 1.18 + var codes []string 1.19 + for _, code := range m.authCodes { 1.20 + if code.ClientID.Equal(clientID) { 1.21 + codes = append(codes, code.Code) 1.22 + } 1.23 + } 1.24 + if len(codes) < 1 { 1.25 + return ErrClientNotFound 1.26 + } 1.27 + for _, code := range codes { 1.28 + delete(m.authCodes, code) 1.29 + } 1.30 + return nil 1.31 +} 1.32 + 1.33 func (m *memstore) useAuthorizationCode(code string) error { 1.34 m.authCodeLock.Lock() 1.35 defer m.authCodeLock.Unlock()
2.1 --- a/authcode_postgres.go Sun Apr 19 23:18:26 2015 -0400 2.2 +++ b/authcode_postgres.go Sat Apr 25 22:44:36 2015 -0400 2.3 @@ -109,6 +109,30 @@ 2.4 return nil 2.5 } 2.6 2.7 +func (p *postgres) deleteAuthorizationCodesByClientIDSQL(clientID uuid.ID) *pan.Query { 2.8 + var authCode AuthorizationCode 2.9 + query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(authCode)) 2.10 + query.IncludeWhere() 2.11 + query.Include(pan.GetUnquotedColumn(authCode, "ClientID")+" = ?", clientID) 2.12 + return query.FlushExpressions(" ") 2.13 +} 2.14 + 2.15 +func (p *postgres) deleteAuthorizationCodesByClientID(clientID uuid.ID) error { 2.16 + query := p.deleteAuthorizationCodesByClientIDSQL(clientID) 2.17 + res, err := p.db.Exec(query.String(), query.Args...) 2.18 + if err != nil { 2.19 + return err 2.20 + } 2.21 + rows, err := res.RowsAffected() 2.22 + if err != nil { 2.23 + return err 2.24 + } 2.25 + if rows == 0 { 2.26 + return ErrClientNotFound 2.27 + } 2.28 + return nil 2.29 +} 2.30 + 2.31 func (p *postgres) useAuthorizationCodeSQL(code string) *pan.Query { 2.32 var authCode AuthorizationCode 2.33 query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(authCode)+" SET ")
3.1 --- a/client.go Sun Apr 19 23:18:26 2015 -0400 3.2 +++ b/client.go Sat Apr 25 22:44:36 2015 -0400 3.3 @@ -268,12 +268,14 @@ 3.4 saveClient(client Client) error 3.5 updateClient(id uuid.ID, change ClientChange) error 3.6 listClientsByOwner(ownerID uuid.ID, num, offset int) ([]Client, error) 3.7 + deleteClientsByOwner(ownerID uuid.ID) error 3.8 3.9 addEndpoints(endpoint []Endpoint) error 3.10 removeEndpoint(client, endpoint uuid.ID) error 3.11 getEndpoint(client, endpoint uuid.ID) (Endpoint, error) 3.12 checkEndpoint(client uuid.ID, endpoint string) (bool, error) 3.13 listEndpoints(client uuid.ID, num, offset int) ([]Endpoint, error) 3.14 + removeEndpointsByClientID(client uuid.ID) error 3.15 countEndpoints(client uuid.ID) (int64, error) 3.16 } 3.17 3.18 @@ -312,7 +314,7 @@ 3.19 3.20 func (m *memstore) listClientsByOwner(ownerID uuid.ID, num, offset int) ([]Client, error) { 3.21 ids := m.lookupClientsByProfileID(ownerID.String()) 3.22 - if len(ids) > num+offset { 3.23 + if len(ids) > num+offset && num > 0 { 3.24 ids = ids[offset : num+offset] 3.25 } else if len(ids) > offset { 3.26 ids = ids[offset:] 3.27 @@ -333,6 +335,21 @@ 3.28 return clients, nil 3.29 } 3.30 3.31 +func (m *memstore) deleteClientsByOwner(ownerID uuid.ID) error { 3.32 + ids := m.lookupClientsByProfileID(ownerID.String()) 3.33 + m.clientLock.Lock() 3.34 + defer m.clientLock.RUnlock() 3.35 + for _, id := range ids { 3.36 + client, ok := m.clients[id.String()] 3.37 + if !ok { 3.38 + continue 3.39 + } 3.40 + client.Deleted = true 3.41 + m.clients[id.String()] = client 3.42 + } 3.43 + return nil 3.44 +} 3.45 + 3.46 func (m *memstore) addEndpoints(endpoints []Endpoint) error { 3.47 m.endpointLock.Lock() 3.48 defer m.endpointLock.Unlock() 3.49 @@ -390,12 +407,34 @@ 3.50 return m.endpoints[client.String()], nil 3.51 } 3.52 3.53 +func (m *memstore) removeEndpointsByClientID(client uuid.ID) error { 3.54 + m.endpointLock.Lock() 3.55 + defer m.endpointLock.Unlock() 3.56 + delete(m.endpoints, client.String()) 3.57 + return nil 3.58 +} 3.59 + 3.60 func (m *memstore) countEndpoints(client uuid.ID) (int64, error) { 3.61 m.endpointLock.RLock() 3.62 defer m.endpointLock.RUnlock() 3.63 return int64(len(m.endpoints[client.String()])), nil 3.64 } 3.65 3.66 +func cleanUpAfterClientDeletion(client uuid.ID, context Context) { 3.67 + err := context.RemoveEndpointsByClientID(client) 3.68 + if err != nil { 3.69 + log.Printf("Error removing endpoints from client %s: %+v\n", client, err) 3.70 + } 3.71 + err = context.DeleteAuthorizationCodesByClientID(client) 3.72 + if err != nil { 3.73 + log.Printf("Error removing auth codes belonging to client %s: %+v\n", client, err) 3.74 + } 3.75 + err = context.RevokeTokensByClientID(client) 3.76 + if err != nil { 3.77 + log.Printf("Error revoking tokens belonging to client %s: %+v\n", client, err) 3.78 + } 3.79 +} 3.80 + 3.81 type newClientReq struct { 3.82 Name string `json:"name"` 3.83 Logo string `json:"logo"` 3.84 @@ -589,6 +628,9 @@ 3.85 if num > maxClientResponseSize { 3.86 errors = append(errors, requestError{Slug: requestErrOverflow, Param: "num"}) 3.87 } 3.88 + if num < 1 { 3.89 + errors = append(errors, requestError{Slug: requestErrInsufficient, Param: "num"}) 3.90 + } 3.91 } 3.92 if offsetStr != "" { 3.93 offset, err = strconv.Atoi(offsetStr) 3.94 @@ -806,8 +848,8 @@ 3.95 encode(w, r, http.StatusInternalServerError, response{Errors: errors}) 3.96 return 3.97 } 3.98 - // BUG(paddy): Client needs to clean up after itself, invalidating tokens, deleting unused grants, deleting endpoints 3.99 encode(w, r, http.StatusOK, response{Errors: errors}) 3.100 + go cleanUpAfterClientDeletion(id, c) 3.101 return 3.102 } 3.103
4.1 --- a/client_postgres.go Sun Apr 19 23:18:26 2015 -0400 4.2 +++ b/client_postgres.go Sat Apr 25 22:44:36 2015 -0400 4.3 @@ -95,7 +95,9 @@ 4.4 query := pan.New(pan.POSTGRES, "SELECT "+pan.QueryList(fields)+" FROM "+pan.GetTableName(client)) 4.5 query.IncludeWhere() 4.6 query.Include(pan.GetUnquotedColumn(client, "OwnerID")+" = ? AND "+pan.GetUnquotedColumn(client, "Deleted")+" = ?", ownerID, false) 4.7 - query.IncludeLimit(int64(num)) 4.8 + if num > 0 { 4.9 + query.IncludeLimit(int64(num)) 4.10 + } 4.11 query.IncludeOffset(int64(offset)) 4.12 return query.FlushExpressions(" ") 4.13 } 4.14 @@ -121,6 +123,21 @@ 4.15 return clients, nil 4.16 } 4.17 4.18 +func (p *postgres) deleteClientsByOwnerSQL(ownerID uuid.ID) *pan.Query { 4.19 + var client Client 4.20 + query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(client)+" SET") 4.21 + query.Include(pan.GetUnquotedColumn(client, "Deleted")+"= ?", true) 4.22 + query.IncludeWhere() 4.23 + query.Include(pan.GetUnquotedColumn(client, "OwnerID")+" = ?", ownerID) 4.24 + return query.FlushExpressions(" ") 4.25 +} 4.26 + 4.27 +func (p *postgres) deleteClientsByOwner(ownerID uuid.ID) error { 4.28 + query := p.deleteClientsByOwnerSQL(ownerID) 4.29 + _, err := p.db.Exec(query.String(), query.Args...) 4.30 + return err 4.31 +} 4.32 + 4.33 func (p *postgres) addEndpointsSQL(endpoints []Endpoint) *pan.Query { 4.34 fields, _ := pan.GetFields(endpoints[0]) 4.35 query := pan.New(pan.POSTGRES, "INSERT INTO "+pan.GetTableName(endpoints[0])) 4.36 @@ -170,6 +187,30 @@ 4.37 return nil 4.38 } 4.39 4.40 +func (p *postgres) removeEndpointsByClientIDSQL(client uuid.ID) *pan.Query { 4.41 + var e Endpoint 4.42 + query := pan.New(pan.POSTGRES, "DELETE FROM "+pan.GetTableName(e)) 4.43 + query.IncludeWhere() 4.44 + query.Include(pan.GetUnquotedColumn(e, "ClientID")+" = ?", client) 4.45 + return query.FlushExpressions(" ") 4.46 +} 4.47 + 4.48 +func (p *postgres) removeEndpointsByClientID(client uuid.ID) error { 4.49 + query := p.removeEndpointsByClientIDSQL(client) 4.50 + res, err := p.db.Exec(query.String(), query.Args...) 4.51 + if err != nil { 4.52 + return err 4.53 + } 4.54 + rows, err := res.RowsAffected() 4.55 + if err != nil { 4.56 + return err 4.57 + } 4.58 + if rows == 0 { 4.59 + return ErrClientNotFound 4.60 + } 4.61 + return nil 4.62 +} 4.63 + 4.64 func (p *postgres) getEndpointSQL(client, endpoint uuid.ID) *pan.Query { 4.65 var e Endpoint 4.66 fields, _ := pan.GetFields(e)
5.1 --- a/context.go Sun Apr 19 23:18:26 2015 -0400 5.2 +++ b/context.go Sat Apr 25 22:44:36 2015 -0400 5.3 @@ -100,6 +100,13 @@ 5.4 return c.clients.listClientsByOwner(ownerID, num, offset) 5.5 } 5.6 5.7 +func (c Context) DeleteClientsByOwner(ownerID uuid.ID) error { 5.8 + if c.clients == nil { 5.9 + return ErrNoClientStore 5.10 + } 5.11 + return c.clients.deleteClientsByOwner(ownerID) 5.12 +} 5.13 + 5.14 // AddEndpoints stores the specified Endpoints in the clientStore associated with the Context. 5.15 func (c Context) AddEndpoints(endpoints []Endpoint) error { 5.16 if c.clients == nil { 5.17 @@ -158,6 +165,13 @@ 5.18 return c.clients.listEndpoints(client, num, offset) 5.19 } 5.20 5.21 +func (c Context) RemoveEndpointsByClientID(client uuid.ID) error { 5.22 + if c.clients == nil { 5.23 + return ErrNoClientStore 5.24 + } 5.25 + return c.clients.removeEndpointsByClientID(client) 5.26 +} 5.27 + 5.28 // CountEndpoints returns the number of Endpoints the are associated with the Client specified by the 5.29 // passed ID in the clientStore associated with the Context. 5.30 func (c Context) CountEndpoints(client uuid.ID) (int64, error) { 5.31 @@ -202,6 +216,13 @@ 5.32 return c.authCodes.deleteAuthorizationCodesByProfileID(profileID) 5.33 } 5.34 5.35 +func (c Context) DeleteAuthorizationCodesByClientID(clientID uuid.ID) error { 5.36 + if c.authCodes == nil { 5.37 + return ErrNoAuthorizationCodeStore 5.38 + } 5.39 + return c.authCodes.deleteAuthorizationCodesByClientID(clientID) 5.40 +} 5.41 + 5.42 // UseAuthorizationCode marks the AuthorizationCode specified by the provided code as used in the authorizationCodeStore associated with 5.43 // the Context. Once an AuthorizationCode is marked as used, its Used property will be set to true when retrieved from the authorizationCodeStore. 5.44 func (c Context) UseAuthorizationCode(code string) error { 5.45 @@ -346,6 +367,13 @@ 5.46 return c.tokens.revokeTokensByProfileID(profileID) 5.47 } 5.48 5.49 +func (c Context) RevokeTokensByClientID(clientID uuid.ID) error { 5.50 + if c.tokens == nil { 5.51 + return ErrNoTokenStore 5.52 + } 5.53 + return c.tokens.revokeTokensByClientID(clientID) 5.54 +} 5.55 + 5.56 // GetTokensByProfileID returns a slice of up to num Tokens with a ProfileID that matches the specified 5.57 // profileID from the tokenStore associated with the Context, skipping offset Tokens. 5.58 func (c Context) GetTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error) {
6.1 --- a/profile.go Sun Apr 19 23:18:26 2015 -0400 6.2 +++ b/profile.go Sat Apr 25 22:44:36 2015 -0400 6.3 @@ -444,7 +444,17 @@ 6.4 if err != nil { 6.5 log.Printf("Error deleting authorization codes associated with profile %s: %+v\n", profile, err) 6.6 } 6.7 - // BUG(paddy): need to delete all clients associated with the Profile 6.8 + clients, err := context.ListClientsByOwner(profile, -1, 0) 6.9 + if err != nil { 6.10 + log.Printf("Error listing clients by profile %s: %+v\n", profile, err) 6.11 + } 6.12 + err = context.DeleteClientsByOwner(profile) 6.13 + if err != nil { 6.14 + log.Printf("Error deleting clients by profile %s: %+v\n", profile, err) 6.15 + } 6.16 + for _, client := range clients { 6.17 + cleanUpAfterClientDeletion(client.ID, context) 6.18 + } 6.19 } 6.20 6.21 // RegisterProfileHandlers adds handlers to the passed router to handle the profile endpoints, like registration and user retrieval.
7.1 --- a/token.go Sun Apr 19 23:18:26 2015 -0400 7.2 +++ b/token.go Sat Apr 25 22:44:36 2015 -0400 7.3 @@ -56,6 +56,7 @@ 7.4 revokeToken(token string, refresh bool) error 7.5 getTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error) 7.6 revokeTokensByProfileID(profileID uuid.ID) error 7.7 + revokeTokensByClientID(clientID uuid.ID) error 7.8 } 7.9 7.10 func (m *memstore) getToken(token string, refresh bool) (Token, error) { 7.11 @@ -128,7 +129,24 @@ 7.12 m.tokenLock.Lock() 7.13 defer m.tokenLock.Unlock() 7.14 for _, id := range ids { 7.15 - delete(m.tokens, id) 7.16 + token := m.tokens[id] 7.17 + token.Revoked = true 7.18 + token.RefreshRevoked = true 7.19 + m.tokens[id] = token 7.20 + } 7.21 + return nil 7.22 +} 7.23 + 7.24 +func (m *memstore) revokeTokensByClientID(clientID uuid.ID) error { 7.25 + m.tokenLock.Lock() 7.26 + defer m.tokenLock.Unlock() 7.27 + for id, token := range m.tokens { 7.28 + if !token.ClientID.Equal(clientID) { 7.29 + continue 7.30 + } 7.31 + token.Revoked = true 7.32 + token.RefreshRevoked = true 7.33 + m.tokens[id] = token 7.34 } 7.35 return nil 7.36 }
8.1 --- a/token_postgres.go Sun Apr 19 23:18:26 2015 -0400 8.2 +++ b/token_postgres.go Sat Apr 25 22:44:36 2015 -0400 8.3 @@ -123,6 +123,31 @@ 8.4 return nil 8.5 } 8.6 8.7 +func (p *postgres) revokeTokensByClientIDSQL(clientID uuid.ID) *pan.Query { 8.8 + var t Token 8.9 + query := pan.New(pan.POSTGRES, "UPDATE "+pan.GetTableName(t)+" SET ") 8.10 + query.Include(pan.GetUnquotedColumn(t, "Revoked")+" = ?", true) 8.11 + query.IncludeWhere() 8.12 + query.Include(pan.GetUnquotedColumn(t, "ClientID")+" = ?", clientID) 8.13 + return query.FlushExpressions(" ") 8.14 +} 8.15 + 8.16 +func (p *postgres) revokeTokensByClientID(clientID uuid.ID) error { 8.17 + query := p.revokeTokensByClientIDSQL(clientID) 8.18 + res, err := p.db.Exec(query.String(), query.Args...) 8.19 + if err != nil { 8.20 + return err 8.21 + } 8.22 + rows, err := res.RowsAffected() 8.23 + if err != nil { 8.24 + return err 8.25 + } 8.26 + if rows == 0 { 8.27 + return ErrClientNotFound 8.28 + } 8.29 + return nil 8.30 +} 8.31 + 8.32 func (p *postgres) getTokensByProfileIDSQL(profileID uuid.ID, num, offset int) *pan.Query { 8.33 var token Token 8.34 fields, _ := pan.GetFields(token)