Skip to content

Commit 357b37b

Browse files
committed
Address review comments
Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
1 parent 8deff24 commit 357b37b

5 files changed

Lines changed: 28 additions & 70 deletions

File tree

CHANGELOG.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ Version v30.0.0
5959

6060
- Add bulk search support for CPEs.
6161

62-
- Add authentication for API.
62+
- Add authentication for REST API endpoint.
63+
The autentication is disabled by default and can be enabled using the
64+
SCANCODEIO_REQUIRE_AUTHENTICATION settings.
65+
When enabled, users have to authenticate through a login form in the Web UI, or using
66+
their API Key in the REST API.
67+
The API Key can be viewed in the Web UI "Profile settings" view once logged-in.
68+
Users can be created using the Django "createsuperuser" management command.
6369

6470
Other:
6571

vulnerabilities/templates/base.html

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@
3838
</div>
3939
{% endif %}
4040
</div>
41+
<div class="navbar-end">
42+
{% if not user.is_authenticated %}
43+
<a class="navbar-item is-right" href="{% url 'login' %}">
44+
Sign in
45+
</a>
46+
{% endif %}
47+
</div>
4148
</div>
4249
</nav>
4350
<main class="Site-content">

vulnerabilities/tests/test_auth.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@
88
# This is copied from https://github.com/nexB/scancode.io/commit/eab8eeb13989c26a1600cc64e8b054f171341063
99
#
1010

11+
1112
from django.conf import settings
1213
from django.contrib.auth import get_user_model
1314
from django.contrib.auth.models import AnonymousUser
1415
from django.test import TestCase
15-
from django.test import override_settings
1616
from django.urls import reverse
1717

18-
from vulnerablecode.auth import is_authenticated_when_required
19-
2018
TEST_PASSWORD = "secret"
2119

2220
User = get_user_model()
@@ -33,17 +31,6 @@ def setUp(self):
3331
self.anonymous_user = AnonymousUser()
3432
self.basic_user = User.objects.create_user(username="basic_user", password=TEST_PASSWORD)
3533

36-
def test_vulnerablecode_auth_is_authenticated_when_required(self):
37-
with override_settings(VULNERABLECODEIO_REQUIRE_AUTHENTICATION=True):
38-
self.assertFalse(self.anonymous_user.is_authenticated)
39-
self.assertFalse(is_authenticated_when_required(user=self.anonymous_user))
40-
41-
self.assertTrue(self.basic_user.is_authenticated)
42-
self.assertTrue(is_authenticated_when_required(user=self.basic_user))
43-
44-
with override_settings(VULNERABLECODEIO_REQUIRE_AUTHENTICATION=False):
45-
self.assertTrue(is_authenticated_when_required(user=None))
46-
4734
def test_vulnerablecode_auth_login_view(self):
4835
data = {"username": self.basic_user.username, "password": ""}
4936
response = self.client.post(login_url, data)
@@ -81,12 +68,10 @@ def test_vulnerablecode_account_profile_view(self):
8168
response = self.client.get(profile_url)
8269
expected = '<label class="label">API Key</label>'
8370
self.assertContains(response, expected, html=True)
84-
expected = '<label class="label">API Key</label>'
8571
self.assertContains(response, self.basic_user.auth_token.key)
8672

8773
def test_vulnerablecode_auth_api_required_authentication(self):
88-
with override_settings(VULNERABLECODEIO_REQUIRE_AUTHENTICATION=True):
89-
response = self.client.get(api_package_url)
90-
expected = {"detail": "Authentication credentials were not provided."}
91-
self.assertEqual(expected, response.json())
92-
self.assertEqual(401, response.status_code)
74+
response = self.client.get(api_package_url)
75+
expected = {"detail": "Authentication credentials were not provided."}
76+
self.assertEqual(expected, response.json())
77+
self.assertEqual(401, response.status_code)

vulnerablecode/auth.py

Lines changed: 0 additions & 49 deletions
This file was deleted.

vulnerablecode/settings.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
# See https://aboutcode.org for more information about nexB OSS projects.
88
#
99

10+
import sys
1011
from pathlib import Path
1112

1213
import environ
@@ -129,13 +130,21 @@
129130

130131
USE_I18N = True
131132

133+
IS_TESTS = False
134+
135+
if len(sys.argv) > 0:
136+
IS_TESTS = "pytest" in sys.argv[0]
137+
132138
VULNERABLECODEIO_REQUIRE_AUTHENTICATION = env.bool(
133139
"VULNERABLECODEIO_REQUIRE_AUTHENTICATION", default=False
134140
)
135141

136142
LOGIN_REDIRECT_URL = "/"
137143
LOGOUT_REDIRECT_URL = "/"
138144

145+
if IS_TESTS:
146+
VULNERABLECODEIO_REQUIRE_AUTHENTICATION = True
147+
139148
USE_L10N = True
140149

141150
USE_TZ = True

0 commit comments

Comments
 (0)