auth
auth/oauth2.go
Break out scopes and events. This repo has gotten unwieldy, and there are portions of it that need to be imported by a large number of other packages. For example, scopes will be used in almost every API we write. Rather than importing the entirety of this codebase into every API we write, I've opted to move the scope logic out into a scopes package, with a subpackage for the defined types, which is all most projects actually want to import. We also define some event type constants, and importing those shouldn't require a project to import all our dependencies, either. So I made an events subpackage that just holds those constants. This package has become a little bit of a red-headed stepchild and is do for a refactor, but I'm trying to put that off as long as I can. The refactoring of our scopes stuff has left a bug wherein a token can be granted for scopes that don't exist. I'm going to need to revisit that, and also how to limit scopes to only be granted to the users that should be able to request them. But that's a battle for another day.
1.1 --- a/oauth2.go Sat Jul 18 03:38:27 2015 -0400 1.2 +++ b/oauth2.go Mon Dec 14 04:17:21 2015 -0800 1.3 @@ -15,6 +15,7 @@ 1.4 "sync" 1.5 "time" 1.6 1.7 + "code.secondbit.org/scopes.hg/types" 1.8 "code.secondbit.org/uuid.hg" 1.9 "github.com/gorilla/mux" 1.10 ) 1.11 @@ -68,7 +69,7 @@ 1.12 // 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.13 // was successfully returned and the Invalidate function will be called asynchronously. 1.14 type GrantType struct { 1.15 - Validate func(w http.ResponseWriter, r *http.Request, context Context) (scopes Scopes, profileID uuid.ID, valid bool) 1.16 + Validate func(w http.ResponseWriter, r *http.Request, context Context) (scopes scopeTypes.Scopes, profileID uuid.ID, valid bool) 1.17 Invalidate func(r *http.Request, context Context) error 1.18 ReturnToken func(w http.ResponseWriter, r *http.Request, token Token, context Context) bool 1.19 AuditString func(r *http.Request) string 1.20 @@ -278,21 +279,20 @@ 1.21 http.Redirect(w, r, redirectURL.String(), http.StatusFound) 1.22 return 1.23 } 1.24 - scopeParams := strings.Split(r.URL.Query().Get("scope"), " ") 1.25 - scopes, err := context.GetScopes(scopeParams) 1.26 - if err != nil { 1.27 - if err == ErrScopeNotFound { 1.28 - q.Add("error", "invalid_scope") 1.29 - redirectURL.RawQuery = q.Encode() 1.30 - http.Redirect(w, r, redirectURL.String(), http.StatusFound) 1.31 - return 1.32 - } 1.33 - log.Println("Error retrieving scopes:", err) 1.34 - q.Add("error", "server_error") 1.35 + scopes := scopeTypes.StringsToScopes(strings.Split(r.URL.Query().Get("scope"), " ")) 1.36 + // BUG(paddy): need to check if Scopes actually exist 1.37 + /*if err == ErrScopeNotFound { 1.38 + q.Add("error", "invalid_scope") 1.39 redirectURL.RawQuery = q.Encode() 1.40 http.Redirect(w, r, redirectURL.String(), http.StatusFound) 1.41 return 1.42 } 1.43 + log.Println("Error retrieving scopes:", err) 1.44 + q.Add("error", "server_error") 1.45 + redirectURL.RawQuery = q.Encode() 1.46 + http.Redirect(w, r, redirectURL.String(), http.StatusFound) 1.47 + return 1.48 + */ 1.49 if r.Method == "POST" { 1.50 if checkCSRF(r, session) != nil { 1.51 log.Println("CSRF attempt detected.")