auth

Paddy 2015-04-19 Parent:3e8964a914ef

163:73e12d5a1124 Go to Latest

auth/scope.go

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.

History
     1.1 --- a/scope.go	Sat Apr 11 19:07:26 2015 -0400
     1.2 +++ b/scope.go	Sun Apr 19 23:18:26 2015 -0400
     1.3 @@ -27,20 +27,36 @@
     1.4  	}
     1.5  }
     1.6  
     1.7 -type sortedScopes []Scope
     1.8 +type Scopes []Scope
     1.9  
    1.10 -func (s sortedScopes) Len() int {
    1.11 +func (s Scopes) Len() int {
    1.12  	return len(s)
    1.13  }
    1.14  
    1.15 -func (s sortedScopes) Swap(i, j int) {
    1.16 +func (s Scopes) Swap(i, j int) {
    1.17  	s[i], s[j] = s[j], s[i]
    1.18  }
    1.19  
    1.20 -func (s sortedScopes) Less(i, j int) bool {
    1.21 +func (s Scopes) Less(i, j int) bool {
    1.22  	return s[i].ID < s[j].ID
    1.23  }
    1.24  
    1.25 +func (s Scopes) Strings() []string {
    1.26 +	res := make([]string, len(s))
    1.27 +	for pos, scope := range s {
    1.28 +		res[pos] = scope.ID
    1.29 +	}
    1.30 +	return res
    1.31 +}
    1.32 +
    1.33 +func stringsToScopes(s []string) Scopes {
    1.34 +	res := make(Scopes, len(s))
    1.35 +	for pos, scope := range s {
    1.36 +		res[pos] = Scope{ID: scope}
    1.37 +	}
    1.38 +	return res
    1.39 +}
    1.40 +
    1.41  // ScopeChange represents a change to a Scope.
    1.42  type ScopeChange struct {
    1.43  	Name        *string
    1.44 @@ -86,7 +102,7 @@
    1.45  		}
    1.46  		scopes = append(scopes, scope)
    1.47  	}
    1.48 -	sorted := sortedScopes(scopes)
    1.49 +	sorted := Scopes(scopes)
    1.50  	sort.Sort(sorted)
    1.51  	scopes = sorted
    1.52  	return scopes, nil
    1.53 @@ -128,7 +144,7 @@
    1.54  	for _, scope := range m.scopes {
    1.55  		scopes = append(scopes, scope)
    1.56  	}
    1.57 -	sorted := sortedScopes(scopes)
    1.58 +	sorted := Scopes(scopes)
    1.59  	sort.Sort(sorted)
    1.60  	scopes = sorted
    1.61  	return scopes, nil