Skip to content

Commit

Permalink
Handle OAuth Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
cjavilla-stripe committed Jul 19, 2019
1 parent 6f40d03 commit e9b2c0b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 21 deletions.
4 changes: 4 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
16 changes: 3 additions & 13 deletions oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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"`
Expand Down
5 changes: 1 addition & 4 deletions oauth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
Expand All @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions oauth/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 8 additions & 1 deletion stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit e9b2c0b

Please sign in to comment.