Skip to content

Commit 3dc83bb

Browse files
committed
add state param
1 parent 138c6d7 commit 3dc83bb

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-3
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ creds = {
6161
client_id: ENV['CLIENT_ID'],
6262
client_secret: ENV['CLIENT_SECRET'],
6363
redirect_uri: ENV['REDIRECT_URI'],
64-
scopes: ENV['SCOPES']
64+
scopes: ENV['SCOPES'],
65+
state: "this-can-be-a-custom-state-parameter" # optional
6566
}
6667
xero_client ||= XeroRuby::ApiClient.new(credentials: creds)
6768
```
@@ -86,6 +87,9 @@ In your callback route catch, calling `get_token_set_from_callback` will exchang
8687
token_set = xero_client.get_token_set_from_callback(params)
8788

8889
# save token_set JSON in a datastore in relation to the user authentication
90+
91+
puts params['state']
92+
=> "this-can-be-a-custom-state-parameter"
8993
```
9094

9195
## Making API calls once you have a token_set

lib/xero-ruby/api_client.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def initialize(config: Configuration.default, credentials: {})
3434
@client_secret = credentials[:client_secret]
3535
@redirect_uri = credentials[:redirect_uri]
3636
@scopes = credentials[:scopes]
37+
@state = credentials[:state]
3738
@config = config
3839
@user_agent = "xero-ruby-#{VERSION}"
3940
@default_headers = {
@@ -43,7 +44,7 @@ def initialize(config: Configuration.default, credentials: {})
4344
end
4445

4546
def authorization_url
46-
url = "#{@config.login_url}?response_type=code&client_id=#{@client_id}&redirect_uri=#{@redirect_uri}&scope=#{@scopes}"
47+
url = "#{@config.login_url}?response_type=code&client_id=#{@client_id}&redirect_uri=#{@redirect_uri}&scope=#{@scopes}&state=#{@state}"
4748
return url
4849
end
4950

spec/api_client_spec.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
describe XeroRuby::ApiClient do
44
context 'initialization' do
5-
context 'URL stuff' do
5+
context 'URL config' do
66
context 'host' do
77
it 'removes http from host' do
88
XeroRuby.configure { |c| c.host = 'http://example.com' }
@@ -36,6 +36,20 @@
3636
expect(XeroRuby::Configuration.default.base_path).to eq('')
3737
end
3838
end
39+
40+
context "creates a valid authorization_url" do
41+
it "passes through attributes" do
42+
creds = {
43+
client_id: 'abc',
44+
client_secret: '123',
45+
redirect_uri: 'https://mydomain.com/callback',
46+
scopes: 'openid profile email accounting.transactions accounting.settings',
47+
state: 'i-am-customer-state'
48+
}
49+
api_client = XeroRuby::ApiClient.new(credentials: creds)
50+
expect(api_client.authorization_url).to eq('https://login.xero.com/identity/connect/authorize?response_type=code&client_id=abc&redirect_uri=https://mydomain.com/callback&scope=openid profile email accounting.transactions accounting.settings&state=i-am-customer-state')
51+
end
52+
end
3953
end
4054
end
4155

0 commit comments

Comments
 (0)