My solution for the basic auth problem would be something like this

Tim Graham <timogra...@gmail.com> ezt írta (időpont: 2021. márc. 4., Cs,
19:36):

> I'd like to see what your code looks like so far. Personally, this is
> sounding a lot more complicated than I imagined when I accepted the ticket.
> I doubt this is a highly requested feature that couldn't be solved another
> way (e.g. downloading the template file without Django), and It's not clear
> to me that adding logic to Django is worth it.
>
> On Thursday, March 4, 2021 at 11:01:41 AM UTC-5 benc...@gmail.com wrote:
>
>> Thanks for the fast reply
>>
>> I found these test cases but I am not sure how to extend them with basic
>> auth, because I don't know if the *LiveServerTestCase *is capable of
>> doing basic auth. As I see currently there is no testcase checking basic
>> auth here and it has to be checked by hand.
>>
>> I am sorry but could you elaborate on this please because I couldn't find
>> these functions.
>>
>>
>> *"first add view (in admin_scripts/urls.py is fine) to first check for
>> basic auth credentials and then pass off to `serve` (as the existing route
>> already does). That should give us the reproduce (or we can closed as
>> fixed)."*
>>
>> Best Regards,
>>    Bence
>>
>> Carlton Gibson <carlton...@gmail.com> ezt írta (időpont: 2021. márc. 4.,
>> Cs, 11:15):
>>
>>> Hi Bence, welcome!
>>>
>>> There are already a couple of tests in place to check the remove
>>> fetching:
>>>
>>>
>>> https://github.com/django/django/blob/05bbff82638731a6abfed2fe0ae06a4d429cb32f/tests/admin_scripts/tests.py#L2047-L2072
>>>
>>> Without changing the command code I'd first add view (in
>>> admin_scripts/urls.py is fine) to first check for basic auth credentials
>>> and then pass off to `serve` (as the existing route already does). That
>>> should give us the reproduce (or we can closed as fixed).
>>>
>>> Then I think it's easier if you open a PR with your suggested change
>>> after that (but more-or-less sounds plausible without looking in depth.)
>>>
>>> Hopefully that gets you going?
>>>
>>> Kind Regards,
>>>
>>> Carlton
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Django developers (Contributions to Django itself)" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to django-develop...@googlegroups.com.
>>> To view this discussion on the web visit
>>> https://groups.google.com/d/msgid/django-developers/66771c44-5b9f-4c29-b92a-91cd3092e016n%40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-developers/66771c44-5b9f-4c29-b92a-91cd3092e016n%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Django developers (Contributions to Django itself)" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-developers+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/814187f8-dc2b-47e1-b91d-76d0ab78241an%40googlegroups.com
> <https://groups.google.com/d/msgid/django-developers/814187f8-dc2b-47e1-b91d-76d0ab78241an%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/CALgZer6XVkJPP4iPQN4DQFpfg%2BCvc6ZH7So1amxC37D6nKKn%2BA%40mail.gmail.com.
commit b282c7f603f01ec074a0e7c6c133ab624e1fe78c
Author: Bence Gaspar <bence.gas...@balabit.com>
Date:   Thu Mar 4 17:43:28 2021 +0100

    Refs #28426 -- Added basic authentication handling to startproject --template option
    
    Added prompt_user_passwd(url) function that prompts for the username and
    password for the given url

diff --git a/django/core/management/templates.py b/django/core/management/templates.py
index 8dc6068286..e6a13c258f 100644
--- a/django/core/management/templates.py
+++ b/django/core/management/templates.py
@@ -6,7 +6,11 @@ import shutil
 import stat
 import tempfile
 from importlib import import_module
-from urllib.request import urlretrieve
+from urllib.error import HTTPError
+from urllib.request import (
+    HTTPBasicAuthHandler, HTTPPasswordMgrWithDefaultRealm, build_opener,
+    install_opener, urlretrieve,
+)
 
 import django
 from django.conf import settings
@@ -263,6 +267,18 @@ class TemplateCommand(BaseCommand):
             self.stdout.write('Downloading %s' % display_url)
         try:
             the_path, info = urlretrieve(url, os.path.join(tempdir, filename))
+        except HTTPError as exc:
+            if exc.code == 401:
+                username, password = self.prompt_user_passwd(url)
+                p = HTTPPasswordMgrWithDefaultRealm()
+                p.add_password(None, url, username, password)
+                auth_handler = HTTPBasicAuthHandler(p)
+                opener = build_opener(auth_handler)
+                install_opener(opener)
+                try:
+                    the_path, info = urlretrieve(url, os.path.join(tempdir, filename))
+                except HTTPError as exc:
+                    raise CommandError("couldn't download URL %s to %s: %s" % (url, filename, exc))
         except OSError as e:
             raise CommandError("couldn't download URL %s to %s: %s" %
                                (url, filename, e))
@@ -295,6 +311,17 @@ class TemplateCommand(BaseCommand):
         # Giving up
         return the_path
 
+    def prompt_user_passwd(self, url):
+        """Prompt the user for username and password for the given url"""
+        import getpass
+        try:
+            user = input("Enter username for %s: " % url)
+            passwd = getpass.getpass("Enter password for %s in %s: " % (user, url))
+            return user, passwd
+        except KeyboardInterrupt:
+            print()
+            return None, None
+
     def splitext(self, the_path):
         """
         Like os.path.splitext, but takes off .tar, too
  • Hel... Bence Gáspár
    • ... Carlton Gibson
      • ... Bence Gáspár
        • ... Tim Graham
          • ... Bence Gáspár
          • ... 'Adam Johnson' via Django developers (Contributions to Django itself)
            • ... Tom Forbes

Reply via email to