Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/muffin_man.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
require "muffin_man/customer_feedback/v20240601"
require "muffin_man/uploads/v20201101"
require "muffin_man/aplus_content/v20201101"
require "muffin_man/application_management/v20231130"

module MuffinMan
class Error < StandardError; end
Expand Down
14 changes: 14 additions & 0 deletions lib/muffin_man/application_management/v20231130.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

module MuffinMan
module ApplicationManagement
class V20231130
def self.rotate_application_client_secret(access_token)
Typhoeus.post(
"https://sellingpartnerapi-na.amazon.com/applications/2023-11-30/clientSecret",
headers: { "x-amz-access-token" => access_token }
)
end
end
end
end
23 changes: 23 additions & 0 deletions lib/muffin_man/lwa/auth_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,28 @@ def self.get_refresh_token(client_id, client_secret, auth_code)
end
JSON.parse(response.body)["refresh_token"]
end

def self.get_access_token(scope, client_id, client_secret)
body = {
grant_type: "client_credentials",
scope: scope,
client_id: client_id,
client_secret: client_secret
}

response = Typhoeus.post(
ACCESS_TOKEN_URL,
body: URI.encode_www_form(body),
headers: {
"Content-Type" => "application/x-www-form-urlencoded;charset=UTF-8"
}
)
if response.code != 200
error_body = JSON.parse(response.body)
error = "#{error_body["error"]}: #{error_body["error_description"]}"
raise MuffinMan::Error, error
end
JSON.parse(response.body)["access_token"]
end
end
end
31 changes: 31 additions & 0 deletions spec/muffin_man/application_management/v20231130_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

RSpec.describe MuffinMan::ApplicationManagement::V20231130 do
let(:access_token) { "test_access_token" }
let(:new_app_credential_url) { "https://sellingpartnerapi-na.amazon.com/applications/2023-11-30/clientSecret" }

describe ".rotate_application_client_secret" do
let(:response) { instance_double(Typhoeus::Response) }

before do
allow(Typhoeus).to receive(:post).and_return(response)
end

context "when the request is successful (204)" do
before do
allow(response).to receive(:code).and_return(204)
allow(response).to receive(:body).and_return("")
end

it "returns the response and makes the correct API call" do
expect(Typhoeus).to receive(:post).with(
new_app_credential_url,
headers: { "x-amz-access-token" => access_token }
)

result = described_class.rotate_application_client_secret(access_token)
expect(result).to eq(response)
end
end
end
end
Loading