auth

Paddy 2015-04-19 Parent:3e8964a914ef Child:581c60f8dd23

163:73e12d5a1124 Go to Latest

auth/oauth2.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/oauth2.go	Sat Apr 11 19:07:26 2015 -0400
     1.2 +++ b/oauth2.go	Sun Apr 19 23:18:26 2015 -0400
     1.3 @@ -68,7 +68,7 @@
     1.4  // The ReturnToken will be called when a token is created and needs to be returned to the client. If it returns true, the token
     1.5  // was successfully returned and the Invalidate function will be called asynchronously.
     1.6  type GrantType struct {
     1.7 -	Validate      func(w http.ResponseWriter, r *http.Request, context Context) (scopes []string, profileID uuid.ID, valid bool)
     1.8 +	Validate      func(w http.ResponseWriter, r *http.Request, context Context) (scopes Scopes, profileID uuid.ID, valid bool)
     1.9  	Invalidate    func(r *http.Request, context Context) error
    1.10  	ReturnToken   func(w http.ResponseWriter, r *http.Request, token Token, context Context) bool
    1.11  	AuditString   func(r *http.Request) string
    1.12 @@ -318,7 +318,7 @@
    1.13  					Created:     time.Now(),
    1.14  					ExpiresIn:   defaultAuthorizationCodeExpiration,
    1.15  					ClientID:    clientID,
    1.16 -					Scopes:      scopeParams,
    1.17 +					Scopes:      scopes,
    1.18  					RedirectURI: r.URL.Query().Get("redirect_uri"),
    1.19  					State:       state,
    1.20  					ProfileID:   session.ProfileID,
    1.21 @@ -337,7 +337,7 @@
    1.22  					CreatedFrom: "implicit",
    1.23  					ExpiresIn:   defaultTokenExpiration,
    1.24  					TokenType:   "bearer",
    1.25 -					Scopes:      scopeParams,
    1.26 +					Scopes:      scopes,
    1.27  					ProfileID:   session.ProfileID,
    1.28  					ClientID:    clientID,
    1.29  				}
    1.30 @@ -351,7 +351,7 @@
    1.31  				q.Add("access_token", token.AccessToken)
    1.32  				q.Add("token_type", token.TokenType)
    1.33  				q.Add("expires_in", strconv.FormatInt(int64(token.ExpiresIn), 10))
    1.34 -				q.Add("scope", strings.Join(token.Scopes, " "))
    1.35 +				q.Add("scope", strings.Join(token.Scopes.Strings(), " "))
    1.36  				q.Add("state", state) // we wiped out the old values, so we need to set the state again
    1.37  				fragment = true
    1.38  			}