From e9b2c0bba94821a27786c9ebd9bf1fc7491a5742 Mon Sep 17 00:00:00 2001 From: CJ Avilla Date: Thu, 18 Jul 2019 19:03:26 -0700 Subject: [PATCH] Handle OAuth Errors --- error.go | 4 ++++ oauth.go | 16 +++------------- oauth/client.go | 5 +---- oauth/client_test.go | 5 ++--- stripe.go | 9 ++++++++- 5 files changed, 18 insertions(+), 21 deletions(-) diff --git a/error.go b/error.go index 8fe28d7f47..3852e0372d 100644 --- a/error.go +++ b/error.go @@ -58,6 +58,10 @@ type Error struct { SetupIntent *SetupIntent `json:"setup_intent,omitempty"` Source *PaymentSource `json:"source,omitempty"` Type ErrorType `json:"type"` + + // OAuth specific Error properties. Named OAuthError because of name conflict. + OAuthError string `json:"error,omitempty"` + OAuthErrorDescription string `json:"error_description,omitempty"` } // Error serializes the error object to JSON and returns it as a string. diff --git a/oauth.go b/oauth.go index 3769d7b424..a89e9c2246 100644 --- a/oauth.go +++ b/oauth.go @@ -40,16 +40,6 @@ const ( OAuthStripeUserGenderMale OAuthStripeUserGender = "male" ) - -// DeauthorizeError the type of errors raised when failing authorization. -type DeauthorizeError string - -// List of supported DeauthorizeError values. -const ( - DeauthorizeErrorInvalidClient DeauthorizeError = "invalid_client" - DeauthorizeErrorInvalidRequest DeauthorizeError = "invalid_request" -) - // OAuthStripeUserParams for the stripe_user OAuth Authorize params. type OAuthStripeUserParams struct { BlockKana *string `form:"block_kana"` @@ -61,9 +51,9 @@ type OAuthStripeUserParams struct { City *string `form:"city"` Country *string `form:"country"` Currency *string `form:"currency"` - DOBDay *int64 `form:"dob_day"` - DOBMonth *int64 `form:"dob_month"` - DOBYear *int64 `form:"dob_year"` + DOBDay *int64 `form:"dob_day"` + DOBMonth *int64 `form:"dob_month"` + DOBYear *int64 `form:"dob_year"` Email *string `form:"email"` FirstName *string `form:"first_name"` FirstNameKana *string `form:"first_name_kana"` diff --git a/oauth/client.go b/oauth/client.go index 106dabed6c..8c0adabd48 100644 --- a/oauth/client.go +++ b/oauth/client.go @@ -36,7 +36,7 @@ func (c Client) AuthorizeURL(params *stripe.AuthorizeURLParams) string { form.AppendTo(qs, params) return fmt.Sprintf( "%s%s/oauth/authorize?%s", - stripe.ConnectURL, + stripe.ConnectURL, express, qs.Encode(), ) @@ -53,9 +53,6 @@ func (c Client) New(params *stripe.OAuthTokenParams) (*stripe.OAuthToken, error) if stripe.StringValue(params.ClientSecret) == "" { params.ClientSecret = stripe.String(stripe.Key) } - if stripe.StringValue(params.GrantType) == "" { - params.GrantType = stripe.String("authorization_code") - } oauthToken := &stripe.OAuthToken{} err := c.B.Call(http.MethodPost, "/oauth/token", c.Key, params, oauthToken) diff --git a/oauth/client_test.go b/oauth/client_test.go index 8bfc65b400..b0956c1316 100644 --- a/oauth/client_test.go +++ b/oauth/client_test.go @@ -235,10 +235,9 @@ func TestNewOAuthTokenWithError(t *testing.T) { assert.NotNil(t, err) stripeErr := err.(*stripe.Error) - // TODO: I think this is what I want to test once we get the structure right. assert.Equal(t, 400, stripeErr.HTTPStatusCode) - assert.Equal(t, "Authorization code does not exist", stripeErr.Msg) - assert.Equal(t, "invalid_grant", stripeErr.Type) + assert.Equal(t, "Authorization code does not exist", stripeErr.OAuthErrorDescription) + assert.Equal(t, "invalid_grant", stripeErr.OAuthError) } func TestDeauthorize(t *testing.T) { diff --git a/stripe.go b/stripe.go index 6aa8cebf5b..4389e2cb1f 100644 --- a/stripe.go +++ b/stripe.go @@ -463,7 +463,14 @@ func (s *BackendImplementation) Do(req *http.Request, body *bytes.Buffer, v inte func (s *BackendImplementation) ResponseToError(res *http.Response, resBody []byte) error { var raw rawError if err := s.UnmarshalJSONVerbose(res.StatusCode, resBody, &raw); err != nil { - return err + // If deserializing the error json fails at the top level, we retry + // serializing at the error level because the shape of error response body + // for the API is different from the OAuth error response. + var topLevelError rawErrorInternal + if err := s.UnmarshalJSONVerbose(res.StatusCode, resBody, &topLevelError); err != nil { + return err + } + raw.E = &topLevelError } // no error in resBody