Skip to content

Commit

Permalink
add state param
Browse files Browse the repository at this point in the history
  • Loading branch information
SerKnight committed Dec 21, 2020
1 parent 138c6d7 commit 3dc83bb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ creds = {
client_id: ENV['CLIENT_ID'],
client_secret: ENV['CLIENT_SECRET'],
redirect_uri: ENV['REDIRECT_URI'],
scopes: ENV['SCOPES']
scopes: ENV['SCOPES'],
state: "this-can-be-a-custom-state-parameter" # optional
}
xero_client ||= XeroRuby::ApiClient.new(credentials: creds)
```
Expand All @@ -86,6 +87,9 @@ In your callback route catch, calling `get_token_set_from_callback` will exchang
token_set = xero_client.get_token_set_from_callback(params)

# save token_set JSON in a datastore in relation to the user authentication

puts params['state']
=> "this-can-be-a-custom-state-parameter"
```

## Making API calls once you have a token_set
Expand Down
3 changes: 2 additions & 1 deletion lib/xero-ruby/api_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def initialize(config: Configuration.default, credentials: {})
@client_secret = credentials[:client_secret]
@redirect_uri = credentials[:redirect_uri]
@scopes = credentials[:scopes]
@state = credentials[:state]
@config = config
@user_agent = "xero-ruby-#{VERSION}"
@default_headers = {
Expand All @@ -43,7 +44,7 @@ def initialize(config: Configuration.default, credentials: {})
end

def authorization_url
url = "#{@config.login_url}?response_type=code&client_id=#{@client_id}&redirect_uri=#{@redirect_uri}&scope=#{@scopes}"
url = "#{@config.login_url}?response_type=code&client_id=#{@client_id}&redirect_uri=#{@redirect_uri}&scope=#{@scopes}&state=#{@state}"
return url
end

Expand Down
16 changes: 15 additions & 1 deletion spec/api_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe XeroRuby::ApiClient do
context 'initialization' do
context 'URL stuff' do
context 'URL config' do
context 'host' do
it 'removes http from host' do
XeroRuby.configure { |c| c.host = 'http://example.com' }
Expand Down Expand Up @@ -36,6 +36,20 @@
expect(XeroRuby::Configuration.default.base_path).to eq('')
end
end

context "creates a valid authorization_url" do
it "passes through attributes" do
creds = {
client_id: 'abc',
client_secret: '123',
redirect_uri: 'https://mydomain.com/callback',
scopes: 'openid profile email accounting.transactions accounting.settings',
state: 'i-am-customer-state'
}
api_client = XeroRuby::ApiClient.new(credentials: creds)
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')
end
end
end
end

Expand Down

0 comments on commit 3dc83bb

Please sign in to comment.