Use postgres arrays for scope associations.
Use the new pqarrays library I wrote to store Scope associations for Tokens and
AuthorizationCodes, instead of using our hacky and abstraction-breaking
many-to-many code.
We also created the authStore.deleteAuthorizationCodesByProfileID method, to
clear out the AuthorizationCodes that belong to a Profile (used when the Profile
is deleted). So we added the implementation for memstore and for our postgres
store.
Call Context.DeleteAuthorizationCodesByProfileID when deleting a Profile to
clean up after it.
Rename sortedScopes to Scopes, which we use pqarrays.StringArray's methods on to
fulfill the sql.Scanner and driver.Valuer interfaces. This lets us store Scopes
in postgres arrays.
Create a stringsToScopes helper function that creates Scope objects, with their
IDs filled by the strings specified.
Update our GrantType.Validate function signature to return Scopes instead of
[]string.
Create a Scopes.Strings() helper method that returns a []string of the IDs of
the Scopes.
Update our SQL init file to use the new postgres array definition, instead of
the many-to-many definition.
9 ErrNoScopeStore = errors.New("scopeStore not set in Context")
10 ErrScopeNotFound = errors.New("scope not found")
11 ErrScopeAlreadyExists = errors.New("scope already exists")
14 // Scope represents a limit on the access that a grant provides.
21 func (s *Scope) ApplyChange(change ScopeChange) {
22 if change.Name != nil {
25 if change.Description != nil {
26 s.Description = *change.Description
32 func (s Scopes) Len() int {
36 func (s Scopes) Swap(i, j int) {
37 s[i], s[j] = s[j], s[i]
40 func (s Scopes) Less(i, j int) bool {
41 return s[i].ID < s[j].ID
44 func (s Scopes) Strings() []string {
45 res := make([]string, len(s))
46 for pos, scope := range s {
52 func stringsToScopes(s []string) Scopes {
53 res := make(Scopes, len(s))
54 for pos, scope := range s {
55 res[pos] = Scope{ID: scope}
60 // ScopeChange represents a change to a Scope.
61 type ScopeChange struct {
66 func (s ScopeChange) Empty() bool {
67 return s.Name == nil && s.Description == nil
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)
78 func (m *memstore) createScopes(scopes []Scope) error {
80 defer m.scopeLock.Unlock()
82 for _, scope := range scopes {
83 if _, ok := m.scopes[scope.ID]; ok {
84 return ErrScopeAlreadyExists
87 for _, scope := range scopes {
88 m.scopes[scope.ID] = scope
93 func (m *memstore) getScopes(ids []string) ([]Scope, error) {
95 defer m.scopeLock.RUnlock()
98 for _, id := range ids {
99 scope, ok := m.scopes[id]
103 scopes = append(scopes, scope)
105 sorted := Scopes(scopes)
111 func (m *memstore) updateScope(id string, change ScopeChange) error {
113 defer m.scopeLock.Unlock()
115 scope, ok := m.scopes[id]
117 return ErrScopeNotFound
119 scope.ApplyChange(change)
124 func (m *memstore) removeScopes(ids []string) error {
126 defer m.scopeLock.Unlock()
128 for _, id := range ids {
129 if _, ok := m.scopes[id]; !ok {
130 return ErrScopeNotFound
133 for _, id := range ids {
139 func (m *memstore) listScopes() ([]Scope, error) {
141 defer m.scopeLock.RUnlock()
144 for _, scope := range m.scopes {
145 scopes = append(scopes, scope)
147 sorted := Scopes(scopes)