auth

Paddy 2015-04-25 Parent:73e12d5a1124

164:cf1aef6eb81f Go to Latest

auth/scope.go

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.

History
1 package auth
3 import (
4 "errors"
5 "sort"
6 )
8 var (
9 ErrNoScopeStore = errors.New("scopeStore not set in Context")
10 ErrScopeNotFound = errors.New("scope not found")
11 ErrScopeAlreadyExists = errors.New("scope already exists")
12 )
14 // Scope represents a limit on the access that a grant provides.
15 type Scope struct {
16 ID string
17 Name string
18 Description string
19 }
21 func (s *Scope) ApplyChange(change ScopeChange) {
22 if change.Name != nil {
23 s.Name = *change.Name
24 }
25 if change.Description != nil {
26 s.Description = *change.Description
27 }
28 }
30 type Scopes []Scope
32 func (s Scopes) Len() int {
33 return len(s)
34 }
36 func (s Scopes) Swap(i, j int) {
37 s[i], s[j] = s[j], s[i]
38 }
40 func (s Scopes) Less(i, j int) bool {
41 return s[i].ID < s[j].ID
42 }
44 func (s Scopes) Strings() []string {
45 res := make([]string, len(s))
46 for pos, scope := range s {
47 res[pos] = scope.ID
48 }
49 return res
50 }
52 func stringsToScopes(s []string) Scopes {
53 res := make(Scopes, len(s))
54 for pos, scope := range s {
55 res[pos] = Scope{ID: scope}
56 }
57 return res
58 }
60 // ScopeChange represents a change to a Scope.
61 type ScopeChange struct {
62 Name *string
63 Description *string
64 }
66 func (s ScopeChange) Empty() bool {
67 return s.Name == nil && s.Description == nil
68 }
70 type scopeStore interface {
71 createScopes(scopes []Scope) error
72 getScopes(ids []string) ([]Scope, error)
73 updateScope(id string, change ScopeChange) error
74 removeScopes(ids []string) error
75 listScopes() ([]Scope, error)
76 }
78 func (m *memstore) createScopes(scopes []Scope) error {
79 m.scopeLock.Lock()
80 defer m.scopeLock.Unlock()
82 for _, scope := range scopes {
83 if _, ok := m.scopes[scope.ID]; ok {
84 return ErrScopeAlreadyExists
85 }
86 }
87 for _, scope := range scopes {
88 m.scopes[scope.ID] = scope
89 }
90 return nil
91 }
93 func (m *memstore) getScopes(ids []string) ([]Scope, error) {
94 m.scopeLock.RLock()
95 defer m.scopeLock.RUnlock()
97 scopes := []Scope{}
98 for _, id := range ids {
99 scope, ok := m.scopes[id]
100 if !ok {
101 continue
102 }
103 scopes = append(scopes, scope)
104 }
105 sorted := Scopes(scopes)
106 sort.Sort(sorted)
107 scopes = sorted
108 return scopes, nil
109 }
111 func (m *memstore) updateScope(id string, change ScopeChange) error {
112 m.scopeLock.Lock()
113 defer m.scopeLock.Unlock()
115 scope, ok := m.scopes[id]
116 if !ok {
117 return ErrScopeNotFound
118 }
119 scope.ApplyChange(change)
120 m.scopes[id] = scope
121 return nil
122 }
124 func (m *memstore) removeScopes(ids []string) error {
125 m.scopeLock.Lock()
126 defer m.scopeLock.Unlock()
128 for _, id := range ids {
129 if _, ok := m.scopes[id]; !ok {
130 return ErrScopeNotFound
131 }
132 }
133 for _, id := range ids {
134 delete(m.scopes, id)
135 }
136 return nil
137 }
139 func (m *memstore) listScopes() ([]Scope, error) {
140 m.scopeLock.RLock()
141 defer m.scopeLock.RUnlock()
143 scopes := []Scope{}
144 for _, scope := range m.scopes {
145 scopes = append(scopes, scope)
146 }
147 sorted := Scopes(scopes)
148 sort.Sort(sorted)
149 scopes = sorted
150 return scopes, nil
151 }