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

Don't request HTTP Basic authenticaion when using a token #488

Merged
merged 5 commits into from
Oct 11, 2018
Merged

Don't request HTTP Basic authenticaion when using a token #488

merged 5 commits into from
Oct 11, 2018

Conversation

himdel
Copy link
Contributor

@himdel himdel commented Oct 10, 2018

Only request fallback to HTTP Basic if no other authentication method is used.

Specifically don't fall back to HTTP Basic when using the token, because otherwise there is no way for the UI to prevent the browser log in dialog when attempting to log out with a stale token (or even trying to use one during a normal request, after suspend for example).

Such attempts will now return a proper HTTP 401 JSON response, but will not trigger the popup.

--

Closes ManageIQ/manageiq-ui-classic#4717
Introduced in #359

@miq-bot miq-bot changed the title Don't request HTTP Basic authenticaion when using a token [WIP] Don't request HTTP Basic authenticaion when using a token Oct 10, 2018
@miq-bot miq-bot added the wip label Oct 10, 2018
@himdel
Copy link
Contributor Author

himdel commented Oct 10, 2018

Testing the UI issue:

open the UI on the login screen,
in the browser console, do:

localStorage.miq_token = '123';
API.logout();

before: a browser login dialog will appear
after: the request will 401 (with an error in the console), but no dialog will appear in the browser

@himdel
Copy link
Contributor Author

himdel commented Oct 10, 2018

@miq-bot add_label hammer/yes

(#359 was never backported to gaprindashvili)

@himdel
Copy link
Contributor Author

himdel commented Oct 10, 2018

401 responses:

when not providing token and cancelling the dialog (same before and after):

{"error":{"kind":"unauthorized","message":"Api::AuthenticationError","klass":"Api::AuthenticationError"}}
(or {"error":{"kind":"unauthorized","message":"Authentication failed","klass":"Api::AuthenticationError"}})

when providing a token that is not valid (new now):

{"error":{"kind":"unauthorized","message":"Invalid Authentication Token 123 specified","klass":"Api::AuthenticationError"}}

@himdel
Copy link
Contributor Author

himdel commented Oct 10, 2018

@miq-bot remove_label wip

Specs are already passing, just added one that fails without this :).

@miq-bot miq-bot changed the title [WIP] Don't request HTTP Basic authenticaion when using a token Don't request HTTP Basic authenticaion when using a token Oct 10, 2018
@miq-bot miq-bot removed the wip label Oct 10, 2018
@himdel
Copy link
Contributor Author

himdel commented Oct 10, 2018

@miq-bot add_label bug

@miq-bot miq-bot added the bug label Oct 10, 2018
@@ -7,11 +7,13 @@ module Authentication
# REST APIs Authenticator and Redirector
#
def require_api_user_or_token
using_http_basic = false
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be a method?

def using_http_basic_auth?
  !(request.headers[HttpHeaders::MIQ_TOKEN] || request.headers[HttpHeaders::AUTH_TOKEN])
end

Copy link
Member

Choose a reason for hiding this comment

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

or def auth_mechanism or something

Copy link
Contributor Author

@himdel himdel Oct 10, 2018

Choose a reason for hiding this comment

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

OK, makes sense :)

Will do, looks like there is a similar bit of logic in container deployments, I'll try to merge that into a helper.

app/controllers/api/container_deployments_controller.rb
20:      [HttpHeaders::MIQ_TOKEN, HttpHeaders::AUTH_TOKEN, "HTTP_AUTHORIZATION"].any? do |header|

only request fallback to HTTP Basic if no other authentication method is used

specifically don't fall back to HTTP Basic when using the token, because otherwise there is no way to prevent the browser dialog when attempting to log out with a stale token, or even trying to use one during a normal request

such attempts will now return a proper HTTP 401 JSON response, but will not trigger the popup
auth_mechanism returns :system for system tokens, :token for user tokens,
:basic for HTTP Basic and nil if no such headers exist

nil usually means the same as basic, but not for OPTION requests which support unauthenticated access
@himdel
Copy link
Contributor Author

himdel commented Oct 11, 2018

@bdunne can you take another look please?

I added auth_mechanism returning one of :system, :token, :basic or nil (no headers),
made both conditions switch on auth_mechanism,
removed a similar condition from Logger, replacing it with auth_mechanism,
and changed ContainerDeploymentsController#authentication_requested? to also use it,
which means we're not touching those headers anywhere else (apart from logging) :).

I also extracted 4 lines from all 3 authentication methods and moved them to a shared auth_user method because it bugged me :).

Copy link
Member

@bdunne bdunne left a comment

Choose a reason for hiding this comment

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

👍 Looks great! Thanks @himdel

@bdunne bdunne merged commit c50b0ab into ManageIQ:master Oct 11, 2018
@bdunne bdunne self-assigned this Oct 11, 2018
@bdunne bdunne added this to the Sprint 97 Ending Oct 22, 2018 milestone Oct 11, 2018
@himdel himdel deleted the fix-basic-401 branch October 11, 2018 15:04
simaishi pushed a commit that referenced this pull request Oct 12, 2018
Don't request HTTP Basic authenticaion when using a token

(cherry picked from commit c50b0ab)
@simaishi
Copy link
Contributor

Hammer backport details:

$ git log -1
commit 4303f761089c8793b8474eb0f57d3d32b95e618a
Author: Brandon Dunne <brandondunne@hotmail.com>
Date:   Thu Oct 11 10:00:17 2018 -0400

    Merge pull request #488 from himdel/fix-basic-401
    
    Don't request HTTP Basic authenticaion when using a token
    
    (cherry picked from commit c50b0aba41d5f2e7a6454983a6c59553e96e05a2)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants