Skip to content

Commit

Permalink
Clean up OAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
cjavilla-stripe committed Jul 19, 2019
1 parent d9ab7ec commit 6f40d03
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 36 deletions.
5 changes: 4 additions & 1 deletion form/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,10 @@ func (f *Values) Encode() string {
if buf.Len() > 0 {
buf.WriteByte('&')
}
buf.WriteString(v.Key)
key := url.QueryEscape(v.Key)
key = strings.Replace(key, "%5B", "[", -1)
key = strings.Replace(key, "%5D", "]", -1)
buf.WriteString(key)
buf.WriteString("=")
buf.WriteString(url.QueryEscape(v.Value))
}
Expand Down
31 changes: 8 additions & 23 deletions oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,6 @@ const (
OAuthStripeUserGenderMale OAuthStripeUserGender = "male"
)

// OAuthError is the type of errors raised when failing authorization.
type OAuthError string

// List of supported OAuthError values.
const (
OAuthErrorInvalidGrant OAuthError = "invalid_grant"
OAuthErrorInvalidRequest OAuthError = "invalid_request"
OAuthErrorInvalidScope OAuthError = "invalid_scope"
OAuthErrorUnsupportedGrantType OAuthError = "unsupported_grant_type"
OAuthErrorUnsupportedResponseType OAuthError = "unsupported_response_type"
)

// DeauthorizeError the type of errors raised when failing authorization.
type DeauthorizeError string
Expand All @@ -72,9 +61,9 @@ type OAuthStripeUserParams struct {
City *string `form:"city"`
Country *string `form:"country"`
Currency *string `form:"currency"`
DOBDay *uint64 `form:"dob_day"`
DOBMonth *uint64 `form:"dob_month"`
DOBYear *uint64 `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 Expand Up @@ -132,12 +121,10 @@ type OAuthTokenParams struct {
// OAuthToken is the value of the OAuthToken from OAuth flow.
// https://stripe.com/docs/connect/oauth-reference#post-token
type OAuthToken struct {
Error OAuthError `json:"error"`
ErrorDescription string `json:"error_description"`
Livemode bool `json:"livemode"`
Scope OAuthScopeType `json:"scope"`
StripeUserID string `json:"stripe_user_id"`
TokenType OAuthTokenType `json:"token_type"`
Livemode bool `json:"livemode"`
Scope OAuthScopeType `json:"scope"`
StripeUserID string `json:"stripe_user_id"`
TokenType OAuthTokenType `json:"token_type"`

// Deprecated, please use StripeUserID
AccessToken string `json:"access_token"`
Expand All @@ -148,7 +135,5 @@ type OAuthToken struct {
// Deauthorize is the value of the return from deauthorizing.
// https://stripe.com/docs/connect/oauth-reference#post-deauthorize
type Deauthorize struct {
Error DeauthorizeError `json:"error"`
ErrorDescription string `json:"error_description"`
StripeUserID string `json:"stripe_user_id"`
StripeUserID string `json:"stripe_user_id"`
}
3 changes: 2 additions & 1 deletion oauth/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func (c Client) AuthorizeURL(params *stripe.AuthorizeURLParams) string {
qs := &form.Values{}
form.AppendTo(qs, params)
return fmt.Sprintf(
"https://connect.stripe.com%s/oauth/authorize?%s",
"%s%s/oauth/authorize?%s",
stripe.ConnectURL,
express,
qs.Encode(),
)
Expand Down
42 changes: 36 additions & 6 deletions oauth/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ func TestAuthorizeURLWithOptionalArgs(t *testing.T) {

func TestAuthorizeURLWithStripeUser(t *testing.T) {
stripe.ClientID = "ca_123"
var dobDay uint64 = 15
var dobMonth uint64 = 10
var dobYear uint64 = 2019
url := AuthorizeURL(&stripe.AuthorizeURLParams{
ResponseType: stripe.String("test-code"),
StripeUser: &stripe.OAuthStripeUserParams{
Expand All @@ -61,9 +58,9 @@ func TestAuthorizeURLWithStripeUser(t *testing.T) {
City: stripe.String("Elko"),
Country: stripe.String("US"),
Currency: stripe.String("USD"),
DOBDay: &dobDay,
DOBMonth: &dobMonth,
DOBYear: &dobYear,
DOBDay: stripe.Int64(15),
DOBMonth: stripe.Int64(10),
DOBYear: stripe.Int64(2019),
Email: stripe.String("[email protected]"),
FirstName: stripe.String("first-name"),
FirstNameKana: stripe.String("first-name-kana"),
Expand Down Expand Up @@ -211,6 +208,39 @@ func TestNewOAuthTokenWithCustomKey(t *testing.T) {
assert.NotNil(t, token)
}

func TestNewOAuthTokenWithError(t *testing.T) {
stripe.Key = "sk_123"
// stripe-mock doesn't support connect URL's so this stubs out the server.

responseBody := `{"error":"invalid_grant","error_description": "Authorization code does not exist"}`
httpClient := NewTestClient(func(req *http.Request) *http.Response {
buf := new(bytes.Buffer)
buf.ReadFrom(req.Body)
reqBody := buf.String()
assert.Contains(t, reqBody, "client_secret=sk_999")

return &http.Response{
StatusCode: 400,
Body: ioutil.NopCloser(bytes.NewBufferString(responseBody)),
Header: make(http.Header),
}
})
StubConnectBackend(httpClient)

token, err := New(&stripe.OAuthTokenParams{
ClientSecret: stripe.String("sk_999"),
})

assert.NotNil(t, token)
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)
}

func TestDeauthorize(t *testing.T) {
stripe.Key = "sk_123"

Expand Down
7 changes: 2 additions & 5 deletions stripe.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ var Key string

// ClientID is the Stripe Client ID used by default for OAuth requests.
// Relevant OAuth parameter types can also be initialized with a specific
// ClientID that will take precidence over this global ClientID.
// ClientID that will take precedence over this global ClientID.
var ClientID string

//
Expand Down Expand Up @@ -777,7 +777,7 @@ func GetBackendWithConfig(backendType SupportedBackend, config *BackendConfig) B

case ConnectBackend:
if config.URL == "" {
config.URL = connectURL
config.URL = ConnectURL
}

config.URL = normalizeURL(config.URL)
Expand Down Expand Up @@ -908,9 +908,6 @@ func StringSlice(v []string) []*string {

const apiURL = "https://api.stripe.com"

// URL Base used for OAuth requests.
const connectURL = "https://connect.stripe.com"

// clientversion is the binding version
const clientversion = "61.17.0"

Expand Down

0 comments on commit 6f40d03

Please sign in to comment.