Skip to content

Commit be1a54d

Browse files
committed
Adding missing tests and documentation
1 parent 0b2f693 commit be1a54d

File tree

3 files changed

+31
-10
lines changed

3 files changed

+31
-10
lines changed

docs/configuration.rst

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ General Settings
6262
indefinitely in a state of having entered their password successfully but not
6363
having passed two factor authentication. Set to ``0`` to disable.
6464

65+
``TOTP_ISSUER`` (default ``site name``)
66+
Changes the Site Name that is shown in the TOTP app after scanning the QR Code.
67+
If not set, falls back to default site name.
68+
69+
6570
Phone-related settings
6671
----------------------
6772

@@ -123,7 +128,7 @@ Next, add additional urls to your config:
123128
124129
# urls.py
125130
from two_factor.gateways.twilio.urls import urlpatterns as tf_twilio_urls
126-
131+
127132
urlpatterns = [
128133
path('', include(tf_twilio_urls)),
129134
...
@@ -168,7 +173,7 @@ Start by providing a value for the following setting:
168173
``TWO_FACTOR_WEBAUTHN_RP_NAME`` (default: ``None``)
169174
The human-palatable identifier for the `Relying Party`_. You **MUST** name your application. Failing to do so will
170175
raise an ``ImproperlyConfigured`` exception.
171-
176+
172177
The defaults provided for all other settings should be enough to enable the use of fingerprint readers, security keys
173178
and android phones (Chrome-based browsers only).
174179

@@ -184,19 +189,19 @@ will be sent to your application after the authentication takes place:
184189
A list of preferred communication transports that will be set for all registered authenticators. **This can be
185190
used to optimize user interaction at authentication time. Its implementation is highly browser-dependent and may
186191
even be disregarded.**
187-
192+
188193
Chrome uses this to filter out credentials that do not use any of the transports listed.
189194
For example, if set to ``['usb', 'internal']`` Chrome will not attempt to authenticate the user with authenticators
190195
that communicate using CaBLE (e.g., android phones).
191-
196+
192197
Possible values for each element in the list are members of ``webauthn.helpers.structs.AuthenticatorTransport``. The
193198
default is to accept all transports.
194199

195200
``TWO_FACTOR_WEBAUTHN_UV_REQUIREMENT`` (default: ``'discouraged'``)
196201
The type of `User Verification`_ that is required. Verification ranges from a simple test of user presence such as
197202
by touching a button to more thorough checks like using biometrics or requiring user PIN input.
198203
Possible values: ``'discouraged'``, ``'preferred'``, ``'required'``.
199-
204+
200205
``TWO_FACTOR_WEBAUTHN_ATTESTATION_CONVEYANCE`` (default: ``'none'``)
201206
The type of `Attestation Conveyance`_. A `Relying Party`_ may want to verify attestations to ensure that
202207
only authentication devices from certain approved vendors can be used. Depending on the level of conveyance, the
@@ -213,13 +218,13 @@ will be sent to your application after the authentication takes place:
213218
``'fido-u2f'``, ``'packed'`` and ``'tpm'`` do not come pre-configured with root certificates. Download the
214219
additional certificates that you needed for your particular device and use the
215220
``TWO_FACTOR_WEBAUTHN_PEM_ROOT_CERTS_BYTES_BY_FMT`` setting below.
216-
221+
217222
``TWO_FACTOR_WEBAUTHN_PEM_ROOT_CERTS_BYTES_BY_FMT`` (default: ``None``)
218223
A mapping of attestation statement formats to lists of Root Certificates, provided as bytes. These will be used in
219224
addition to those already provided by ``py_webauthn`` to verify attestation objects.
220225

221226
**Example:**
222-
227+
223228
If you want to verify attestations made by a Yubikey, get `Yubico's root CA`_ and use it as follows:
224229

225230
.. code-block:: python
@@ -237,7 +242,7 @@ will be sent to your application after the authentication takes place:
237242
AttestationFormat.FIDO_U2F: root_ca_list,
238243
}
239244
240-
The following settings control how the attributes for WebAuthn entities are built:
245+
The following settings control how the attributes for WebAuthn entities are built:
241246

242247
``TWO_FACTOR_WEBAUTHN_ENTITIES_FORM_MIXIN`` (default: ``'two_factor.webauthn.utils.WebauthnEntitiesFormMixin'``)
243248
A mixin to provide WebAuthn entities (user and `Relying Party`_) needed during setup and authentication. Although

tests/test_views_qrcode.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from unittest import mock
22

33
import qrcode.image.svg
4-
from django.test import RequestFactory, TestCase
4+
from django.test import RequestFactory, TestCase, override_settings
55
from django.urls import reverse
66

77
from two_factor.utils import get_otpauth_url
@@ -91,3 +91,19 @@ def side_effect(resp):
9191
self.assertEqual(response.status_code, 200)
9292
self.assertEqual(response.content.decode('utf-8'), self.test_img)
9393
self.assertEqual(response['Content-Type'], 'image/svg+xml; charset=utf-8')
94+
95+
@override_settings(TOTP_ISSUER='My Custom App')
96+
def test_totp_issuer_setting(self):
97+
"""Test that TOTP_ISSUER setting is used when provided."""
98+
view = QRGeneratorView()
99+
view.request = mock.Mock()
100+
self.assertEqual(view.get_issuer(), 'My Custom App')
101+
102+
def test_totp_issuer_fallback(self):
103+
"""Test fallback to site name when TOTP_ISSUER is not set."""
104+
view = QRGeneratorView()
105+
view.request = mock.Mock()
106+
view.request.META = {'SERVER_NAME': 'testserver', 'SERVER_PORT': '80'}
107+
with mock.patch('two_factor.views.core.get_current_site') as mock_site:
108+
mock_site.return_value.name = 'testserver'
109+
self.assertEqual(view.get_issuer(), 'testserver')

two_factor/views/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ class QRGeneratorView(View):
707707
}
708708

709709
def get_issuer(self):
710-
if settings.TOTP_ISSUER:
710+
if hasattr(settings, 'TOTP_ISSUER'):
711711
return settings.TOTP_ISSUER
712712
return get_current_site(self.request).name
713713

0 commit comments

Comments
 (0)