{"title":"Authenticating against Django’s user database from Apache","version":"3.1","locale":"pl","docname":"howto/deployment/wsgi/apache-auth","url":"/pl/3.1/howto/deployment/wsgi/apache-auth/","canonical":"https://djangodocs.dev/pl/3.1/howto/deployment/wsgi/apache-auth/","summary":"Since keeping multiple authentication databases in sync is a common problem when dealing with Apache, you can configure Apache to authenticate against Django’s…","html":"<h1>Authenticating against Django’s user database from Apache<a class=\"heading-anchor\" href=\"#authenticating-against-django-s-user-database-from-apache\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Since keeping multiple authentication databases in sync is a common problem when\ndealing with Apache, you can configure Apache to authenticate against Django’s\n<a class=\"reference internal\" href=\"/pl/3.1/topics/auth/\"><span class=\"doc\">authentication system</span></a> directly. This requires Apache\nversion &gt;= 2.2 and mod_wsgi &gt;= 2.0. For example, you could:</p>\n<ul class=\"simple\">\n<li><p>Serve static/media files directly from Apache only to authenticated users.</p></li>\n<li><p>Authenticate access to a <a class=\"reference external\" href=\"https://subversion.apache.org/\">Subversion</a> repository against Django users with\na certain permission.</p></li>\n<li><p>Allow certain users to connect to a WebDAV share created with <a class=\"reference external\" href=\"https://httpd.apache.org/docs/2.2/mod/mod_dav.html\">mod_dav</a>.</p></li>\n</ul>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Informacja</p>\n<p>If you have installed a <a class=\"reference internal\" href=\"/pl/3.1/topics/auth/customizing/#auth-custom-user\"><span class=\"std std-ref\">custom user model</span></a> and\nwant to use this default auth handler, it must support an <code class=\"docutils literal notranslate\">is_active</code>\nattribute. If you want to use group based authorization, your custom user\nmust have a relation named «groups», referring to a related object that has\na «name» field. You can also specify your own custom mod_wsgi\nauth handler if your custom cannot conform to these requirements.</p>\n</aside>\n<section id=\"authentication-with-mod-wsgi\">\n<h2>Autentykacja z <code class=\"docutils literal notranslate\">mod_wsgi</code><a class=\"heading-anchor\" href=\"#authentication-with-mod-wsgi\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Informacja</p>\n<p>The use of <code class=\"docutils literal notranslate\">WSGIApplicationGroup %{GLOBAL}</code> in the configurations below\npresumes that your Apache instance is running only one Django application.\nIf you are running more than one Django application, please refer to the\n<a class=\"reference external\" href=\"https://modwsgi.readthedocs.io/en/develop/user-guides/configuration-guidelines.html#defining-application-groups\">Defining Application Groups</a> section of the mod_wsgi docs for more\ninformation about this setting.</p>\n</aside>\n<p>Make sure that mod_wsgi is installed and activated and that you have\nfollowed the steps to setup <a class=\"reference internal\" href=\"/pl/3.1/howto/deployment/wsgi/modwsgi/\"><span class=\"doc\">Apache with mod_wsgi</span></a>.</p>\n<p>Next, edit your Apache configuration to add a location that you want\nonly authenticated users to be able to view:</p>\n<div class=\"code-block\" data-language=\"apache\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Apache</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Apache code\"><code><span class=\"nb\">WSGIScriptAlias</span> / <span class=\"sx\">/path/to/mysite.com/mysite/wsgi.py</span>\n<span class=\"nb\">WSGIPythonPath</span> <span class=\"sx\">/path/to/mysite.com</span>\n\n<span class=\"nb\">WSGIProcessGroup</span> %{GLOBAL}\n<span class=\"nb\">WSGIApplicationGroup</span> %{GLOBAL}\n\n<span class=\"nt\">&lt;Location</span> <span class=\"s\">&quot;/secret&quot;</span><span class=\"nt\">&gt;</span>\n    <span class=\"nb\">AuthType</span> Basic\n    <span class=\"nb\">AuthName</span> <span class=\"s2\">&quot;Top Secret&quot;</span>\n    <span class=\"nb\">Require</span> valid-user\n    <span class=\"nb\">AuthBasicProvider</span> wsgi\n    <span class=\"nb\">WSGIAuthUserScript</span> <span class=\"sx\">/path/to/mysite.com/mysite/wsgi.py</span>\n<span class=\"nt\">&lt;/Location&gt;</span>\n</code></pre></div>\n<p>The <code class=\"docutils literal notranslate\">WSGIAuthUserScript</code> directive tells mod_wsgi to execute the\n<code class=\"docutils literal notranslate\">check_password</code> function in specified wsgi script, passing the user name and\npassword that it receives from the prompt. In this example, the\n<code class=\"docutils literal notranslate\">WSGIAuthUserScript</code> is the same as the <code class=\"docutils literal notranslate\">WSGIScriptAlias</code> that defines your\napplication <a class=\"reference internal\" href=\"/pl/3.1/howto/deployment/wsgi/\"><span class=\"doc\">that is created by django-admin startproject</span></a>.</p>\n<aside class=\"admonition-using-apache-2-2-with-authentication admonition\" role=\"note\">\n<p class=\"admonition-title\">Wykorzystywanie Apache 2.2 z autentykacją</p>\n<p>Make sure that <code class=\"docutils literal notranslate\">mod_auth_basic</code> and <code class=\"docutils literal notranslate\">mod_authz_user</code> are loaded.</p>\n<p>These might be compiled statically into Apache, or you might need to use\nLoadModule to load them dynamically in your <code class=\"docutils literal notranslate\">httpd.conf</code>:</p>\n<div class=\"code-block\" data-language=\"apache\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Apache</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Apache code\"><code><span class=\"nb\">LoadModule</span> auth_basic_module modules/mod_auth_basic.so\n<span class=\"nb\">LoadModule</span> authz_user_module modules/mod_authz_user.so\n</code></pre></div>\n</aside>\n<p>Finally, edit your WSGI script <code class=\"docutils literal notranslate\">mysite.wsgi</code> to tie Apache’s authentication\nto your site’s authentication mechanisms by importing the <code class=\"docutils literal notranslate\">check_password</code>\nfunction:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">import</span> <span class=\"nn\">os</span>\n\nos<span class=\"o\">.</span>environ<span class=\"p\">[</span><span class=\"s1\">&#39;DJANGO_SETTINGS_MODULE&#39;</span><span class=\"p\">]</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;mysite.settings&#39;</span>\n\n<span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.handlers.modwsgi</span> <span class=\"kn\">import</span> check_password\n\n<span class=\"kn\">from</span> <span class=\"nn\">django.core.handlers.wsgi</span> <span class=\"kn\">import</span> WSGIHandler\napplication <span class=\"o\">=</span> WSGIHandler<span class=\"p\">()</span>\n</code></pre></div>\n<p>Requests beginning with <code class=\"docutils literal notranslate\">/secret/</code> will now require a user to authenticate.</p>\n<p>The mod_wsgi <a class=\"reference external\" href=\"https://modwsgi.readthedocs.io/en/develop/user-guides/access-control-mechanisms.html\">access control mechanisms documentation</a> provides additional\ndetails and information about alternative methods of authentication.</p>\n<section id=\"authorization-with-mod-wsgi-and-django-groups\">\n<h3>Authorization with <code class=\"docutils literal notranslate\">mod_wsgi</code> and Django groups<a class=\"heading-anchor\" href=\"#authorization-with-mod-wsgi-and-django-groups\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>mod_wsgi also provides functionality to restrict a particular location to\nmembers of a group.</p>\n<p>W tym przypadku konfiguracja Apache powinna wyglądać jak:</p>\n<div class=\"code-block\" data-language=\"apache\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Apache</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Apache code\"><code><span class=\"nb\">WSGIScriptAlias</span> / <span class=\"sx\">/path/to/mysite.com/mysite/wsgi.py</span>\n\n<span class=\"nb\">WSGIProcessGroup</span> %{GLOBAL}\n<span class=\"nb\">WSGIApplicationGroup</span> %{GLOBAL}\n\n<span class=\"nt\">&lt;Location</span> <span class=\"s\">&quot;/secret&quot;</span><span class=\"nt\">&gt;</span>\n    <span class=\"nb\">AuthType</span> Basic\n    <span class=\"nb\">AuthName</span> <span class=\"s2\">&quot;Top Secret&quot;</span>\n    <span class=\"nb\">AuthBasicProvider</span> wsgi\n    <span class=\"nb\">WSGIAuthUserScript</span> <span class=\"sx\">/path/to/mysite.com/mysite/wsgi.py</span>\n    <span class=\"nb\">WSGIAuthGroupScript</span> <span class=\"sx\">/path/to/mysite.com/mysite/wsgi.py</span>\n    <span class=\"nb\">Require</span> <span class=\"k\">group</span> secret-agents\n    <span class=\"nb\">Require</span> valid-user\n<span class=\"nt\">&lt;/Location&gt;</span>\n</code></pre></div>\n<p>To support the <code class=\"docutils literal notranslate\">WSGIAuthGroupScript</code> directive, the same WSGI script\n<code class=\"docutils literal notranslate\">mysite.wsgi</code> must also import the <code class=\"docutils literal notranslate\">groups_for_user</code> function which\nreturns a list groups the given user belongs to.</p>\n<div class=\"code-block\" data-language=\"python\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Python code\"><code><span class=\"kn\">from</span> <span class=\"nn\">django.contrib.auth.handlers.modwsgi</span> <span class=\"kn\">import</span> check_password<span class=\"p\">,</span> groups_for_user\n</code></pre></div>\n<p>Requests for <code class=\"docutils literal notranslate\">/secret/</code> will now also require user to be a member of the\n„secret-agents” group.</p>\n</section>\n</section>","rootId":"authenticating-against-django-s-user-database-from-apache","toc":[{"title":"Autentykacja z mod_wsgi","anchor":"authentication-with-mod-wsgi","children":[{"title":"Authorization with mod_wsgi and Django groups","anchor":"authorization-with-mod-wsgi-and-django-groups","children":[]}]}],"breadcrumbs":[{"docname":"howto/index","title":"Przewodniki „Jak to zrobić”","url":"/pl/3.1/howto/"},{"docname":"howto/deployment/index","title":"Wdrażanie Django","url":"/pl/3.1/howto/deployment/"},{"docname":"howto/deployment/wsgi/index","title":"Jak wdrażać z WSGI","url":"/pl/3.1/howto/deployment/wsgi/"}],"prev":{"docname":"howto/deployment/wsgi/modwsgi","title":"Jak używać Django z Apache i mod_wsgi","url":"/pl/3.1/howto/deployment/wsgi/modwsgi/"},"next":{"docname":"howto/deployment/asgi/index","title":"How to deploy with ASGI","url":"/pl/3.1/howto/deployment/asgi/"},"formats":{"html":"/pl/3.1/howto/deployment/wsgi/apache-auth/","markdown":"/pl/3.1/howto/deployment/wsgi/apache-auth.md","json":"/pl/3.1/howto/deployment/wsgi/apache-auth.json"},"source":"https://github.com/django/django/blob/stable/3.1.x/docs/howto/deployment/wsgi/apache-auth.txt","official":"https://docs.djangoproject.com/pl/3.1/howto/deployment/wsgi/apache-auth/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10"],"inLocales":["en","zh-hans","fr","ja","id","pt-br","ko","es","el","pl"]}