auth
auth/http.go
Parse the redirect URI early, add new failure modes to tests. When obtaining a grant code, parse the redirect_uri early as a URL, so we can fail without even hitting the database, if possible. Add a test to cover the scenario where the client doesn't specify an endpoint, and the client has no endpoints registered. Add a test to cover the scenario where the client has two endpoints registered, but doesn't specify an endpoint. Fix the test that tested for invalid URLs by actually using an invalid URL. Apparently, "not a URL" is a valid URL. Go figure.
1.1 --- a/http.go Sun Nov 02 21:15:46 2014 -0500 1.2 +++ b/http.go Sun Nov 02 22:28:49 2014 -0500 1.3 @@ -32,6 +32,15 @@ 1.4 }) 1.5 return 1.6 } 1.7 + redirectURI := r.URL.Query().Get("redirect_uri") 1.8 + redirectURL, err := url.Parse(redirectURI) 1.9 + if err != nil { 1.10 + w.WriteHeader(http.StatusBadRequest) 1.11 + context.Render(w, getGrantTemplateName, map[string]interface{}{ 1.12 + "error": template.HTML("The redirect_uri specified is not valid."), 1.13 + }) 1.14 + return 1.15 + } 1.16 client, err := context.GetClient(clientID) 1.17 if err != nil { 1.18 if err == ErrClientNotFound { 1.19 @@ -57,7 +66,6 @@ 1.20 }) 1.21 return 1.22 } 1.23 - redirectURI := r.URL.Query().Get("redirect_uri") 1.24 var validURI bool 1.25 if redirectURI != "" { 1.26 // BUG(paddy): We really should normalize URIs before trying to compare them. 1.27 @@ -98,14 +106,6 @@ 1.28 } 1.29 scope := r.URL.Query().Get("scope") 1.30 state := r.URL.Query().Get("state") 1.31 - redirectURL, err := url.Parse(redirectURI) 1.32 - if err != nil { 1.33 - w.WriteHeader(http.StatusBadRequest) 1.34 - context.Render(w, getGrantTemplateName, map[string]interface{}{ 1.35 - "error": template.HTML("The redirect_uri specified is not valid."), 1.36 - }) 1.37 - return 1.38 - } 1.39 if r.URL.Query().Get("response_type") != "code" { 1.40 redirectURL.Query().Add("error", "invalid_request") 1.41 redirectURL.Query().Add("state", state)