auth

Paddy 2015-01-18 Parent:9a5999963868 Child:da77e083cf02

116:e000b1c24fc0 Go to Latest

auth/oauth2.go

Make all tests that deal with the store interfaces go through the Context. This is mainly important so that pre- and post- save/retrieval/deletion/whatever transforms can be done without doing them in every single implementation of the store. Change the Endpoint URI property to be a string, not a *url.URL. This makes testing easier, JSON responses cleaner, and is all around just a better strategy. Just because we turn it into a URL every now and then doesn't mean that's how we need to store it. Add JSON tags to the Client type and Endpoint type. Create normalizeURI and normalizeURIString methods to... well, normalize the Endpoint URIs. This makes it so that we can compare them, and forgive some arbitrary user behaviour (like slashes, etc.) Add a NormalizedURI property to the Endpoint type. This is where we store the NormalizedURI, which is what we'll be using when we want to check if an endpoint is valid or not. For the sake of tests and predictability, however, we always want to redirect to the URI, not the NormalizedURI. Add checks to the Client creation API endpoint to give better errors. Now leaving out the Type won't be considered an invalid type, it will be considered a missing parameter. An empty name will be reported as a missing parameter, a name with too few characters will be reported as an insufficient name, and a name with too many characters will be reported as an overflow name. We gather as many of these errors as apply before returning. Check if an Endpoint URI is absolute before adding it as an endpoint, or return an invalid value error if it is not. Always return the errors array when creating a client. We could succeed in creating one or more things and still have errors. We should return anything that's created _as well as_ any errors encountered. Add unit testing for our CreateClientHandler. Fix our oauth2 tests so that if there's an error in the body, it's in the test logs. This should help debugging significantly. Fix our oauth2 tests so that the Profile only requires 1 iteration for its password hashing. This means each time we want to validate a session, it doesn't add a full second to our test runs. This is a big speed improvement for our tests. Add test helper methods for comparing API errors, API responses, and filling in server-generated information in a response that it's impossible to have an expectation around (e.g., IDs) so that we can use our comparison helpers to check if a response is as we expect it. Fix a typo in our Context helpers that was reporting no sessionStore being set _only_ when a sessionStore was set. So yes, the opposite of what we wanted. Oops. This was discovered by passing all our tests through the context. methods instead of operating on the stores themselves.

History
     1.1 --- a/oauth2.go	Wed Jan 14 00:23:30 2015 -0500
     1.2 +++ b/oauth2.go	Sun Jan 18 01:02:14 2015 -0500
     1.3 @@ -173,14 +173,6 @@
     1.4  		return
     1.5  	}
     1.6  	redirectURI := r.URL.Query().Get("redirect_uri")
     1.7 -	redirectURL, err := url.Parse(redirectURI)
     1.8 -	if err != nil {
     1.9 -		w.WriteHeader(http.StatusBadRequest)
    1.10 -		context.Render(w, getAuthorizationCodeTemplateName, map[string]interface{}{
    1.11 -			"error": template.HTML("The redirect_uri specified is not valid."),
    1.12 -		})
    1.13 -		return
    1.14 -	}
    1.15  	client, err := context.GetClient(clientID)
    1.16  	if err != nil {
    1.17  		if err == ErrClientNotFound {
    1.18 @@ -211,9 +203,15 @@
    1.19  	}
    1.20  	var validURI bool
    1.21  	if redirectURI != "" {
    1.22 -		// BUG(paddy): We really should normalize URIs before trying to compare them.
    1.23  		validURI, err = context.CheckEndpoint(clientID, redirectURI)
    1.24  		if err != nil {
    1.25 +			if err == ErrEndpointURINotURL {
    1.26 +				w.WriteHeader(http.StatusBadRequest)
    1.27 +				context.Render(w, getAuthorizationCodeTemplateName, map[string]interface{}{
    1.28 +					"error": template.HTML("The redirect_uri specified is not valid."),
    1.29 +				})
    1.30 +				return
    1.31 +			}
    1.32  			log.Println(err.Error())
    1.33  			w.WriteHeader(http.StatusInternalServerError)
    1.34  			context.Render(w, getAuthorizationCodeTemplateName, map[string]interface{}{
    1.35 @@ -237,9 +235,7 @@
    1.36  		if len(endpoints) != 1 {
    1.37  			validURI = false
    1.38  		} else {
    1.39 -			u := endpoints[0].URI // Copy it here to avoid grabbing a pointer to the memstore
    1.40 -			redirectURI = u.String()
    1.41 -			redirectURL = &u
    1.42 +			redirectURI = endpoints[0].URI
    1.43  		}
    1.44  	} else {
    1.45  		validURI = false
    1.46 @@ -251,6 +247,14 @@
    1.47  		})
    1.48  		return
    1.49  	}
    1.50 +	redirectURL, err := url.Parse(redirectURI)
    1.51 +	if err != nil {
    1.52 +		w.WriteHeader(http.StatusBadRequest)
    1.53 +		context.Render(w, getAuthorizationCodeTemplateName, map[string]interface{}{
    1.54 +			"error": template.HTML("The redirect_uri specified is not valid."),
    1.55 +		})
    1.56 +		return
    1.57 +	}
    1.58  	scope := r.URL.Query().Get("scope")
    1.59  	state := r.URL.Query().Get("state")
    1.60  	if r.URL.Query().Get("response_type") != "code" {