auth

Paddy 2014-08-13 Parent:fb2fd59f9930 Child:244ac84003b3

9:10b84165df41 Browse Files

Handle remaining access errors. Fill out remaining TODOs about returning errors when trying to obtain an access token.

access.go errors.go

     1.1 --- a/access.go	Wed Aug 13 06:07:37 2014 -0400
     1.2 +++ b/access.go	Wed Aug 13 06:46:13 2014 -0400
     1.3 @@ -90,16 +90,20 @@
     1.4  	if err != nil {
     1.5  		if err == ClientNotFoundError || err == InvalidClientError {
     1.6  			ctx.RenderJSONError(w, ErrorInvalidClient, "Invalid client auth.", ctx.Config.DocumentationDomain)
     1.7 -		} else {
     1.8 -			ctx.RenderJSONError(w, ErrorServerError, "Internal server error.", ctx.Config.DocumentationDomain)
     1.9 +			return
    1.10  		}
    1.11 +		ctx.RenderJSONError(w, ErrorServerError, "Internal server error.", ctx.Config.DocumentationDomain)
    1.12  		return
    1.13  	}
    1.14  
    1.15  	// must be a valid authorization code
    1.16  	authData, err := ctx.Tokens.GetAuthorization(code)
    1.17  	if err != nil {
    1.18 -		// TODO: return error
    1.19 +		if err == AuthorizationNotFoundError {
    1.20 +			ctx.RenderJSONError(w, ErrorInvalidGrant, "Invalid authorization.", ctx.Config.DocumentationDomain)
    1.21 +			return
    1.22 +		}
    1.23 +		ctx.RenderJSONError(w, ErrorServerError, "Internal server error.", ctx.Config.DocumentationDomain)
    1.24  		return
    1.25  	}
    1.26  	if authData.RedirectURI == "" {
    1.27 @@ -170,16 +174,20 @@
    1.28  	if err != nil {
    1.29  		if err == ClientNotFoundError || err == InvalidClientError {
    1.30  			ctx.RenderJSONError(w, ErrorInvalidClient, "Invalid client auth.", ctx.Config.DocumentationDomain)
    1.31 -		} else {
    1.32 -			ctx.RenderJSONError(w, ErrorServerError, "Internal server error.", ctx.Config.DocumentationDomain)
    1.33 +			return
    1.34  		}
    1.35 +		ctx.RenderJSONError(w, ErrorServerError, "Internal server error.", ctx.Config.DocumentationDomain)
    1.36  		return
    1.37  	}
    1.38  
    1.39  	// must be a valid refresh code
    1.40  	refreshData, err := ctx.Tokens.GetRefresh(code)
    1.41  	if err != nil {
    1.42 -		// TODO: return error
    1.43 +		if err == TokenNotFoundError {
    1.44 +			ctx.RenderJSONError(w, ErrorInvalidGrant, "Refresh token not valid.", ctx.Config.DocumentationDomain)
    1.45 +			return
    1.46 +		}
    1.47 +		ctx.RenderJSONError(w, ErrorServerError, "Internal server error.", ctx.Config.DocumentationDomain)
    1.48  		return
    1.49  	}
    1.50  
    1.51 @@ -232,15 +240,19 @@
    1.52  	if err != nil {
    1.53  		if err == ClientNotFoundError || err == InvalidClientError {
    1.54  			ctx.RenderJSONError(w, ErrorInvalidClient, "Invalid client auth.", ctx.Config.DocumentationDomain)
    1.55 -		} else {
    1.56 -			ctx.RenderJSONError(w, ErrorServerError, "Internal server error.", ctx.Config.DocumentationDomain)
    1.57 +			return
    1.58  		}
    1.59 +		ctx.RenderJSONError(w, ErrorServerError, "Internal server error.", ctx.Config.DocumentationDomain)
    1.60  		return
    1.61  	}
    1.62  
    1.63  	_, err = ctx.Profiles.GetProfile(username, password)
    1.64  	if err != nil {
    1.65 -		// TODO: return error
    1.66 +		if err == ProfileNotFoundError {
    1.67 +			ctx.RenderJSONError(w, ErrorInvalidGrant, "Invalid credentials.", ctx.Config.DocumentationDomain)
    1.68 +			return
    1.69 +		}
    1.70 +		ctx.RenderJSONError(w, ErrorServerError, "Internal server error.", ctx.Config.DocumentationDomain)
    1.71  		return
    1.72  	}
    1.73  
    1.74 @@ -274,9 +286,9 @@
    1.75  	if err != nil {
    1.76  		if err == ClientNotFoundError || err == InvalidClientError {
    1.77  			ctx.RenderJSONError(w, ErrorInvalidClient, "Invalid client auth.", ctx.Config.DocumentationDomain)
    1.78 -		} else {
    1.79 -			ctx.RenderJSONError(w, ErrorServerError, "Internal server error.", ctx.Config.DocumentationDomain)
    1.80 +			return
    1.81  		}
    1.82 +		ctx.RenderJSONError(w, ErrorServerError, "Internal server error.", ctx.Config.DocumentationDomain)
    1.83  		return
    1.84  	}
    1.85  
     2.1 --- a/errors.go	Wed Aug 13 06:07:37 2014 -0400
     2.2 +++ b/errors.go	Wed Aug 13 06:46:13 2014 -0400
     2.3 @@ -14,12 +14,15 @@
     2.4  )
     2.5  
     2.6  var (
     2.7 -	ClientNotFoundError   = errors.New("Client not found.")
     2.8 -	URIMissingError       = errors.New("Redirect URI missing.")
     2.9 -	InvalidMethodError    = errors.New("Invalid request method.")
    2.10 -	InternalServerError   = errors.New("Internal server error.")
    2.11 -	ErrorNotAuthenticated = errors.New("Not authenticated.")
    2.12 -	InvalidClientError    = errors.New("Invalid client.")
    2.13 +	ClientNotFoundError        = errors.New("Client not found.")
    2.14 +	URIMissingError            = errors.New("Redirect URI missing.")
    2.15 +	InvalidMethodError         = errors.New("Invalid request method.")
    2.16 +	InternalServerError        = errors.New("Internal server error.")
    2.17 +	ErrorNotAuthenticated      = errors.New("Not authenticated.")
    2.18 +	InvalidClientError         = errors.New("Invalid client.")
    2.19 +	AuthorizationNotFoundError = errors.New("Authorization not found.")
    2.20 +	ProfileNotFoundError       = errors.New("Profile not found.")
    2.21 +	TokenNotFoundError         = errors.New("Token not found.")
    2.22  )
    2.23  
    2.24  type URIFormatError string