auth

Paddy 2015-03-06 Parent:f59559b33c76 Child:026adb0c7fc4

138:874c21d1dd8d Browse Files

Add ListEndpoints handler. Add a handler responsible for returning the endpoints that exist on a client. It still needs unit tests, but appears to work as required.

client.go

     1.1 --- a/client.go	Thu Mar 05 19:11:53 2015 -0500
     1.2 +++ b/client.go	Fri Mar 06 19:27:19 2015 -0500
     1.3 @@ -57,12 +57,14 @@
     1.4  )
     1.5  
     1.6  const (
     1.7 -	clientTypePublic          = "public"
     1.8 -	clientTypeConfidential    = "confidential"
     1.9 -	minClientNameLen          = 2
    1.10 -	maxClientNameLen          = 24
    1.11 -	defaultClientResponseSize = 20
    1.12 -	maxClientResponseSize     = 50
    1.13 +	clientTypePublic            = "public"
    1.14 +	clientTypeConfidential      = "confidential"
    1.15 +	minClientNameLen            = 2
    1.16 +	maxClientNameLen            = 24
    1.17 +	defaultClientResponseSize   = 20
    1.18 +	maxClientResponseSize       = 50
    1.19 +	defaultEndpointResponseSize = 20
    1.20 +	maxEndpointResponseSize     = 50
    1.21  
    1.22  	normalizeFlags = purell.FlagsUsuallySafeNonGreedy | purell.FlagSortQuery
    1.23  )
    1.24 @@ -397,7 +399,7 @@
    1.25  	// BUG(paddy): We need to implement a handler to delete a client. Also, what should that do with the grants and tokens belonging to that client?
    1.26  	r.Handle("/clients/{id}/endpoints", wrap(context, AddEndpointsHandler)).Methods("POST")
    1.27  	// BUG(paddy): We need to implement a handler to remove an endpoint from a client.
    1.28 -	// BUG(paddy): We need to implement a handler to list endpoints.
    1.29 +	r.Handle("/clients/{id}/endpoints", wrap(context, ListEndpointsHandler)).Methods("GET")
    1.30  }
    1.31  
    1.32  func CreateClientHandler(w http.ResponseWriter, r *http.Request, c Context) {
    1.33 @@ -736,6 +738,51 @@
    1.34  	encode(w, r, http.StatusCreated, resp)
    1.35  }
    1.36  
    1.37 +func ListEndpointsHandler(w http.ResponseWriter, r *http.Request, c Context) {
    1.38 +	errors := []requestError{}
    1.39 +	vars := mux.Vars(r)
    1.40 +	clientID, err := uuid.Parse(vars["id"])
    1.41 +	if err != nil {
    1.42 +		errors = append(errors, requestError{Slug: requestErrInvalidFormat, Param: "client_id"})
    1.43 +		encode(w, r, http.StatusBadRequest, response{Errors: errors})
    1.44 +		return
    1.45 +	}
    1.46 +	num := defaultEndpointResponseSize
    1.47 +	offset := 0
    1.48 +	numStr := r.URL.Query().Get("num")
    1.49 +	offsetStr := r.URL.Query().Get("offset")
    1.50 +	if numStr != "" {
    1.51 +		num, err = strconv.Atoi(numStr)
    1.52 +		if err != nil {
    1.53 +			errors = append(errors, requestError{Slug: requestErrInvalidFormat, Param: "num"})
    1.54 +		}
    1.55 +		if num > maxEndpointResponseSize {
    1.56 +			errors = append(errors, requestError{Slug: requestErrOverflow, Param: "num"})
    1.57 +		}
    1.58 +	}
    1.59 +	if offsetStr != "" {
    1.60 +		offset, err = strconv.Atoi(offsetStr)
    1.61 +		if err != nil {
    1.62 +			errors = append(errors, requestError{Slug: requestErrInvalidFormat, Param: "offset"})
    1.63 +		}
    1.64 +	}
    1.65 +	if len(errors) > 0 {
    1.66 +		encode(w, r, http.StatusBadRequest, response{Errors: errors})
    1.67 +		return
    1.68 +	}
    1.69 +	endpoints, err := c.ListEndpoints(clientID, num, offset)
    1.70 +	if err != nil {
    1.71 +		errors = append(errors, requestError{Slug: requestErrActOfGod})
    1.72 +		encode(w, r, http.StatusInternalServerError, response{Errors: errors})
    1.73 +		return
    1.74 +	}
    1.75 +	resp := response{
    1.76 +		Endpoints: endpoints,
    1.77 +		Errors:    errors,
    1.78 +	}
    1.79 +	encode(w, r, http.StatusOK, resp)
    1.80 +}
    1.81 +
    1.82  func clientCredentialsValidate(w http.ResponseWriter, r *http.Request, context Context) (scopes []string, profileID uuid.ID, valid bool) {
    1.83  	scopes = strings.Split(r.PostFormValue("scope"), " ")
    1.84  	valid = true