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

Authorize according to API key (if given) over cookies #3877

Merged
merged 7 commits into from
Jun 12, 2019
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
4 changes: 4 additions & 0 deletions redash/authentication/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ def sign(key, path, expires):

@login_manager.user_loader
def load_user(user_id_with_identity):
user = api_key_load_user_from_request(request)
if user:
return user
Copy link
Member

Choose a reason for hiding this comment

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

This feels almost as too simple 😆

Copy link
Contributor Author

@rauchy rauchy Jun 4, 2019

Choose a reason for hiding this comment

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

I've battled flask_login for like 8 hours, trying to get it to cooperate with user_loader and request_loader until this hit me 🤦‍♂️


org = current_org._get_current_object()

try:
Expand Down
13 changes: 13 additions & 0 deletions tests/test_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ def test_user_api_key(self):
self.assertEqual(user.id, hmac_load_user_from_request(request).id)


class TestSessionAuthentication(BaseTestCase):
def test_prefers_api_key_over_session_user_id(self):
user = self.factory.create_user()
query = self.factory.create_query(user=user)

other_org = self.factory.create_org()
other_user = self.factory.create_user(org=other_org)
models.db.session.flush()

rv = self.make_request('get', '/api/queries/{}?api_key={}'.format(query.id, query.api_key), user=other_user)
self.assertEqual(rv.status_code, 200)


class TestCreateAndLoginUser(BaseTestCase):
def test_logins_valid_user(self):
user = self.factory.create_user(email=u'[email protected]')
Expand Down