forked from stripe/stripe-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support OAuth flow for Connect accounts (stripe#555)
Fixes stripe#422
- Loading branch information
1 parent
686607f
commit 0ff2d99
Showing
5 changed files
with
204 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
'use strict'; | ||
|
||
var StripeResource = require('../StripeResource'); | ||
var stripeMethod = StripeResource.method; | ||
var utils = require('../utils'); | ||
|
||
var oAuthHost = 'connect.stripe.com'; | ||
|
||
module.exports = StripeResource.extend({ | ||
basePath: '/', | ||
|
||
authorizeUrl: function(params, options) { | ||
params = params || {}; | ||
options = options || {}; | ||
|
||
var path = 'oauth/authorize'; | ||
|
||
// For Express accounts, the path changes | ||
if (options.express) { | ||
path = 'express/' + path; | ||
} | ||
|
||
if (!params.response_type) { | ||
params.response_type = 'code'; | ||
} | ||
|
||
if (!params.client_id) { | ||
params.client_id = this._stripe.getClientId(); | ||
} | ||
|
||
if (!params.scope) { | ||
params.scope = 'read_write'; | ||
} | ||
|
||
return 'https://' + oAuthHost + '/' + path + '?' + utils.stringifyRequestData(params); | ||
}, | ||
|
||
token: stripeMethod({ | ||
method: 'POST', | ||
path: 'oauth/token', | ||
host: oAuthHost, | ||
}), | ||
|
||
deauthorize: function(spec) { | ||
if (!spec.client_id) { | ||
spec.client_id = this._stripe.getClientId(); | ||
} | ||
|
||
return stripeMethod({ | ||
method: 'POST', | ||
path: 'oauth/deauthorize', | ||
host: oAuthHost, | ||
}).apply(this, arguments); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
'use strict'; | ||
|
||
var stripe = require('../../testUtils').getSpyableStripe(); | ||
|
||
var expect = require('chai').expect; | ||
var URL = require('url'); | ||
var qs = require('qs'); | ||
|
||
describe('OAuth', function() { | ||
describe('authorize', function() { | ||
describe('when a default client_id is set', function() { | ||
beforeEach(function() { | ||
stripe.setClientId('default_client_id'); | ||
}); | ||
|
||
it('Uses the correct host', function() { | ||
var url = stripe.oauth.authorizeUrl(); | ||
|
||
var host = URL.parse(url).hostname; | ||
|
||
expect(host).to.equal('connect.stripe.com'); | ||
}); | ||
|
||
it('Uses the correct path', function() { | ||
var url = stripe.oauth.authorizeUrl({state: 'some_state'}); | ||
|
||
var pathname = URL.parse(url).pathname; | ||
|
||
expect(pathname).to.equal('/oauth/authorize'); | ||
}); | ||
|
||
it('Uses the correct query', function() { | ||
var url = stripe.oauth.authorizeUrl({state: 'some_state'}); | ||
|
||
var query = qs.parse(URL.parse(url).query) | ||
|
||
expect(query.client_id).to.equal('default_client_id'); | ||
expect(query.response_type).to.equal('code'); | ||
expect(query.scope).to.equal('read_write'); | ||
expect(query.state).to.equal('some_state'); | ||
}); | ||
|
||
it('Uses a provided client_id instead of the default', function() { | ||
var url = stripe.oauth.authorizeUrl({client_id: '123abc'}); | ||
|
||
var query = qs.parse(URL.parse(url).query) | ||
|
||
expect(query.client_id).to.equal('123abc'); | ||
}); | ||
|
||
describe('for Express account', function() { | ||
it('Uses the correct path', function() { | ||
var url = stripe.oauth.authorizeUrl({}, {express: true}); | ||
|
||
var pathname = URL.parse(url).pathname; | ||
|
||
expect(pathname).to.equal('/express/oauth/authorize'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('token', function() { | ||
it('Sends the correct request', function() { | ||
stripe.oauth.token({ | ||
code: '123abc', | ||
grant_type: 'authorization_code' | ||
}); | ||
|
||
expect(stripe.LAST_REQUEST).to.deep.equal({ | ||
method: 'POST', | ||
host: 'connect.stripe.com', | ||
url: '/oauth/token', | ||
headers: {}, | ||
data: { | ||
code: '123abc', | ||
grant_type: 'authorization_code' | ||
}, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('deauthorize', function() { | ||
beforeEach(function() { | ||
stripe.setClientId('default_client_id'); | ||
}); | ||
|
||
it('Sends the correct request without explicit client_id', function() { | ||
stripe.oauth.deauthorize({ | ||
stripe_user_id: 'some_user_id', | ||
}); | ||
|
||
expect(stripe.LAST_REQUEST).to.deep.equal({ | ||
method: 'POST', | ||
host: 'connect.stripe.com', | ||
url: '/oauth/deauthorize', | ||
headers: {}, | ||
data: { | ||
client_id: stripe.getClientId(), | ||
stripe_user_id: 'some_user_id' | ||
}, | ||
}); | ||
}); | ||
|
||
it('Sends the correct request with explicit client_id', function() { | ||
stripe.oauth.deauthorize({ | ||
stripe_user_id: 'some_user_id', | ||
client_id: '123abc', | ||
}); | ||
|
||
expect(stripe.LAST_REQUEST).to.deep.equal({ | ||
method: 'POST', | ||
host: 'connect.stripe.com', | ||
url: '/oauth/deauthorize', | ||
headers: {}, | ||
data: { | ||
client_id: '123abc', | ||
stripe_user_id: 'some_user_id' | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |