Skip to content

Commit

Permalink
Merge pull request #50 from raszi/add-partner-id
Browse files Browse the repository at this point in the history
feat(autuentication): add optional partner_id param
  • Loading branch information
raszi committed Dec 1, 2019
2 parents 66d6322 + a38e099 commit 5e5a291
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Get the following from your Marketo admin:
* hostname, i.e. `'123-abc-123.mktorest.com'`
* client id, e.g. `'4567e1cdf-0fae-4685-a914-5be45043f2d8'`
* client secret, e.g. `'7Gn0tuiHZiDHnzeu9P14uDQcSx9xIPPt'`
* partner id, e.g. `'335b1c91511b8d8b49c7bbf66f53288f16f37b60_a0147938d3135f8ddb5a75850ea3c39313fd23c4'` (optional)


## Usage
Expand All @@ -42,7 +43,9 @@ Get the following from your Marketo admin:
client = Mrkt::Client.new(
host: '123-abc-123.mktorest.com',
client_id: '4567e1cdf-0fae-4685-a914-5be45043f2d8',
client_secret: '7Gn0tuiHZiDHnzeu9P14uDQcSx9xIPPt')
client_secret: '7Gn0tuiHZiDHnzeu9P14uDQcSx9xIPPt',
partner_id: '335b1c91511b8d8b49c7bbf66f53288f16f37b60_a0147938d3135f8ddb5a75850ea3c39313fd23c4' # optional
)
```

If you need verbosity during troubleshooting, enable debug mode:
Expand Down
1 change: 1 addition & 0 deletions lib/mrkt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def initialize(options = {})

@client_id = options.fetch(:client_id)
@client_secret = options.fetch(:client_secret)
@partner_id = options[:partner_id]

@retry_authentication = options.fetch(:retry_authentication, false)
@retry_authentication_count = options.fetch(:retry_authentication_count, 3).to_i
Expand Down
20 changes: 16 additions & 4 deletions lib/mrkt/concerns/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,27 @@ def authenticate
end

def authentication_params
merge_params(required_authentication_params, optional_authentication_params)
end

def add_authorization(req)
req.headers[:authorization] = "Bearer #{@token}"
end

private

def optional_authentication_params
{
partner_id: @partner_id
}
end

def required_authentication_params
{
grant_type: 'client_credentials',
client_id: @client_id,
client_secret: @client_secret
}
end

def add_authorization(req)
req.headers[:authorization] = "Bearer #{@token}"
end
end
end
38 changes: 38 additions & 0 deletions spec/concerns/authentication_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,44 @@
expect(client.authenticated?).to be true
end

context 'with optional partner_id client option' do
before { remove_request_stub(@authentication_request_stub) }

let(:partner_id) { SecureRandom.uuid }

let(:client_options) do
{
host: host,
client_id: client_id,
client_secret: client_secret,
partner_id: partner_id
}
end

let(:query) do
{
client_id: client_id,
client_secret: client_secret,
partner_id: partner_id,
grant_type: 'client_credentials'
}
end

subject(:client) { Mrkt::Client.new(client_options) }

before do
stub_request(:get, "https://#{host}/identity/oauth/token")
.with(query: query)
.to_return(json_stub(authentication_stub))
end

it 'should authenticate and then be authenticated?' do
expect(client.authenticated?).to_not be true
client.authenticate!
expect(client.authenticated?).to be true
end
end

context 'when the token has expired and @retry_authentication = true' do
before { remove_request_stub(@authentication_request_stub) }

Expand Down

0 comments on commit 5e5a291

Please sign in to comment.