Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch OIDC error #13078

Closed
Closed
Show file tree
Hide file tree
Changes from 7 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
9 changes: 9 additions & 0 deletions app/controllers/admin/dfc_product_imports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ def index
end

@count = imported.compact.count
rescue Rack::OAuth2::Client::Error => e
flash[:error] = I18n.t(
'admin.dfc_product_imports.index.oauth_error_html',
message: helpers.sanitize(e.message),
oidc_settings_link: helpers.link_to(
I18n.t('spree.admin.tab.oidc_settings'), Rails.application.routes.url_helpers.admin_oidc_settings_path
)
).html_safe
redirect_to admin_product_import_path
rescue Faraday::Error,
Addressable::URI::InvalidURIError,
ActionController::ParameterMissing => e
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,7 @@ en:
index:
title: "Importing a DFC product catalog"
imported_products: "Imported products:"
oauth_error_html: "Authentication error: %{message}. See %{oidc_settings_link}"
enterprise_fees:
index:
title: "Enterprise Fees"
Expand Down
6 changes: 6 additions & 0 deletions engines/dfc_provider/app/services/dfc_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,11 @@ def refresh_access_token!
token: token.access_token,
refresh_token: token.refresh_token
)
rescue Rack::OAuth2::Client::Error => e
@user.oidc_account.update!(
token: nil,
refresh_token: nil
)
raise
end
end
75 changes: 53 additions & 22 deletions engines/dfc_provider/spec/services/dfc_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,58 @@
# The absence of errors makes this test pass.
end

it "refreshes the access token and retrieves the FDC catalog", vcr: true do
# A refresh is only attempted if the token is stale.
account.uid = "testdfc@protonmail.com"
account.refresh_token = ENV.fetch("OPENID_REFRESH_TOKEN")
account.updated_at = 1.day.ago

response = nil
expect {
response = api.call(
"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts"
)
}.to change {
account.token
}.and change {
account.refresh_token
}

json = JSON.parse(response)

graph = DfcIo.import(json)
products = graph.select { |s| s.semanticType == "dfc-b:SuppliedProduct" }
expect(products).to be_present
describe "refreshing token when stale" do
before do
account.uid = "testdfc@protonmail.com"
account.refresh_token = ENV.fetch("OPENID_REFRESH_TOKEN")
account.updated_at = 1.day.ago
end

it "refreshes the access token and retrieves the FDC catalog", vcr: true do
response = nil
expect {
response = api.call(
"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts"
)
}.to change {
account.token
}.and change {
account.refresh_token
}

json = JSON.parse(response)

graph = DfcIo.import(json)
products = graph.select { |s| s.semanticType == "dfc-b:SuppliedProduct" }
expect(products).to be_present
end

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mkllnk I've had a go at speccing this, but in order to test that it clears the tokens, we need to set tokens first.

I've copied secret vars from Bitwarden and am able to make these requests, but the test session isn't active and results in the error "invalid_grant :: Session not active" (the exact error I'm trying to test!)

Am I doing things the long way round? Let me know if you see a better way.

Otherwise, can you please help me to refresh the session? Then hopefully the below spec will work..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't use VCR for this because the spec then depends on the state of your session. The one in Bitwarden is probably outdated. Use webmock. And you got the right error response to fill your example already.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback. I had another go, but I can't seem to set up the right context for this case to work.

Maybe it's not worth trying to make the spec. What if we merge this without a spec, and see if it solves the errors?

context "with account tokens" do
before do
account.refresh_token = ENV.fetch("OPENID_REFRESH_TOKEN")
api.call(
"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts"
)
expect(account.token).not_to be_nil
end

it "clears the token if authentication fails", vcr: true do
allow_any_instance_of(OpenIDConnect::Client).to receive(:access_token!).and_raise(
Rack::OAuth2::Client::Error.new(
1, { error: "invalid_grant", error_description: "session not active" }
)
)

expect {
api.call(
"https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts"
)
}.to raise_error(Rack::OAuth2::Client::Error).and change {
account.token
}.to(nil).and change {
account.refresh_token
}.to(nil)
end
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions spec/system/admin/dfc_product_import_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,30 @@
expect(product.image).to be_present
end

it "shows oauth error message", vcr: true do
allow_any_instance_of(DfcRequest).to receive(:refresh_access_token!).and_raise(
Rack::OAuth2::Client::Error.new(
1, { error: "invalid_grant", error_description: "session not active" }
)
)
dacook marked this conversation as resolved.
Show resolved Hide resolved

user.update!(oidc_account: build(:testdfc_account))

visit admin_product_import_path

select enterprise.name, from: "Enterprise"
url = "https://env-0105831.jcloud-ver-jpe.ik-server.com/api/dfc/Enterprises/test-hodmedod/SuppliedProducts"
fill_in "catalog_url", with: url

click_button "Import"

within ".flash" do
expect(page).to have_content "invalid_grant"
expect(page).to have_content "session not active"
expect(page).to have_link "OIDC Settings"
end
end

it "fails gracefully" do
user.oidc_account.update!(
uid: "anonymous@example.net",
Expand Down