{"title":"Authentication using REMOTE_USER","version":"3.0","locale":"el","docname":"howto/auth-remote-user","url":"/el/3.0/howto/auth-remote-user/","canonical":"https://djangodocs.dev/el/3.0/howto/auth-remote-user/","summary":"Αυτό το άρθρο περιγράφει πως μπορείτε να χρησιμοποιήσετε εξωτερικές πηγές πιστοποίησης (όπου ο Web server έχει ορίσει ως environment variable το REMOTE_USER ) μέσα…","html":"<h1>Authentication using <code class=\"docutils literal notranslate\"><span class=\"pre\">REMOTE_USER</span></code><a class=\"heading-anchor\" href=\"#authentication-using-remote-user\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Αυτό το άρθρο περιγράφει πως μπορείτε να χρησιμοποιήσετε εξωτερικές πηγές πιστοποίησης (όπου ο Web server έχει ορίσει ως environment variable το <code class=\"docutils literal notranslate\"><span class=\"pre\">REMOTE_USER</span></code>) μέσα στις Django εφαρμογές σας. Αυτού του είδους η λύση σχετικά με την πιστοποίηση εμφανίζεται, συνήθως, μέσα σε intranet σελίδες, όπου η είσοδος του χρήστη στο σύστημα γίνεται με single sign-on μέσω IIS και του Integrated Windows Authentication ή μέσω Apache και <a class=\"reference external\" href=\"https://httpd.apache.org/docs/2.2/mod/mod_authnz_ldap.html\">mod_authnz_ldap</a>, <a class=\"reference external\" href=\"https://www.apereo.org/projects/cas\">CAS</a>, <a class=\"reference external\" href=\"http://weblogin.org\">Cosign</a>, <a class=\"reference external\" href=\"https://www.stanford.edu/services/webauth/\">WebAuth</a>, <a class=\"reference external\" href=\"https://sourceforge.net/projects/mod-auth-sspi\">mod_auth_sspi</a>, κλπ.</p>\n<p>Όταν ο Web server αναλαμβάνει την πιστοποίηση, στην ουσία θέτει τη μεταβλητή <code class=\"docutils literal notranslate\"><span class=\"pre\">REMOTE_USER</span></code> ως environment variable για να χρησιμοποιηθεί από την εκάστοτε εφαρμογή. Στο Django, η μεταβλητή <code class=\"docutils literal notranslate\"><span class=\"pre\">REMOTE_USER</span></code> είναι διαθέσιμη μέσα από το attribute <a class=\"reference internal\" href=\"/el/3.0/ref/request-response/#django.http.HttpRequest.META\" title=\"django.http.HttpRequest.META\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">request.META</span></code></a> του request object. Το Django μπορεί να παραμετροποιηθεί για να κάνει χρήση της μεταβλητής <code class=\"docutils literal notranslate\"><span class=\"pre\">REMOTE_USER</span></code> χρησιμοποιώντας τη <code class=\"docutils literal notranslate\"><span class=\"pre\">RemoteUserMiddleware</span></code> ή τη <code class=\"docutils literal notranslate\"><span class=\"pre\">PersistentRemoteUserMiddleware</span></code> και την κλάση <a class=\"reference internal\" href=\"/el/3.0/ref/contrib/auth/#django.contrib.auth.backends.RemoteUserBackend\" title=\"django.contrib.auth.backends.RemoteUserBackend\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RemoteUserBackend</span></code></a> που βρίσκεται μέσα στο module <a class=\"reference internal\" href=\"/el/3.0/topics/auth/#module-django.contrib.auth\" title=\"django.contrib.auth: Django's authentication framework.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.auth</span></code></a>.</p>\n<section id=\"configuration\">\n<h2>Παραμετροποίηση<a class=\"heading-anchor\" href=\"#configuration\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Πρώτα πρέπει να προσθέσετε τη middleware κλάση <a class=\"reference internal\" href=\"/el/3.0/ref/middleware/#django.contrib.auth.middleware.RemoteUserMiddleware\" title=\"django.contrib.auth.middleware.RemoteUserMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.contrib.auth.middleware.RemoteUserMiddleware</span></code></a> στη ρύθμιση <a class=\"reference internal\" href=\"/el/3.0/ref/settings/#std-setting-MIDDLEWARE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIDDLEWARE</span></code></a> <strong>μετά</strong> τη middleware κλάση <a class=\"reference internal\" href=\"/el/3.0/ref/middleware/#django.contrib.auth.middleware.AuthenticationMiddleware\" title=\"django.contrib.auth.middleware.AuthenticationMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.contrib.auth.middleware.AuthenticationMiddleware</span></code></a>:</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=\"n\">MIDDLEWARE</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"s1\">&#39;...&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;django.contrib.auth.middleware.AuthenticationMiddleware&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;django.contrib.auth.middleware.RemoteUserMiddleware&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;...&#39;</span><span class=\"p\">,</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>Κατόπιν, πρέπει να αντικαταστήσετε την κλάση <a class=\"reference internal\" href=\"/el/3.0/ref/contrib/auth/#django.contrib.auth.backends.ModelBackend\" title=\"django.contrib.auth.backends.ModelBackend\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ModelBackend</span></code></a> με την <a class=\"reference internal\" href=\"/el/3.0/ref/contrib/auth/#django.contrib.auth.backends.RemoteUserBackend\" title=\"django.contrib.auth.backends.RemoteUserBackend\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RemoteUserBackend</span></code></a> μέσα στη ρύθμιση <a class=\"reference internal\" href=\"/el/3.0/ref/settings/#std-setting-AUTHENTICATION_BACKENDS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">AUTHENTICATION_BACKENDS</span></code></a>:</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=\"n\">AUTHENTICATION_BACKENDS</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"s1\">&#39;django.contrib.auth.backends.RemoteUserBackend&#39;</span><span class=\"p\">,</span>\n<span class=\"p\">]</span>\n</code></pre></div>\n<p>Με αυτές τις ρυθμίσεις, η <code class=\"docutils literal notranslate\"><span class=\"pre\">RemoteUserMiddleware</span></code> θα ανιχνεύσει το username μέσα στο <code class=\"docutils literal notranslate\"><span class=\"pre\">request.META['REMOTE_USER']</span></code> και όχι μόνο θα πιστοποιήσει αλλά και θα εισάγει αυτόν τον χρήστη αυτόματα στο σύστημα, χρησιμοποιώντας την κλάση <a class=\"reference internal\" href=\"/el/3.0/ref/contrib/auth/#django.contrib.auth.backends.RemoteUserBackend\" title=\"django.contrib.auth.backends.RemoteUserBackend\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RemoteUserBackend</span></code></a>.</p>\n<p>Ιδιαίτερη προσοχή πρέπει να δοθεί στο γεγονός ότι η ανωτέρω συγκεκριμένη ρύθμιση απενεργοποιεί την πιστοποίηση του, προεπιλεγμένου, <code class=\"docutils literal notranslate\"><span class=\"pre\">ModelBackend</span></code>. Αυτό σημαίνει ότι αν η τιμή της μεταβλητής <code class=\"docutils literal notranslate\"><span class=\"pre\">REMOTE_USER</span></code> δεν έχει οριστεί, τότε ο χρήστης δεν θα μπορεί να κάνει log in στο σύστημα, ακόμη και αν χρησιμοποιεί το interface του Django admin. Για την επίλυση αυτού, απλώς προσθέστε το <code class=\"docutils literal notranslate\"><span class=\"pre\">'django.contrib.auth.backends.ModelBackend'</span></code> στη λίστα <code class=\"docutils literal notranslate\"><span class=\"pre\">AUTHENTICATION_BACKENDS</span></code>. Με αυτό τον τρόπο το Django θα χρησιμοποιήσει το <code class=\"docutils literal notranslate\"><span class=\"pre\">ModelBackend</span></code> ως fallback αν το <code class=\"docutils literal notranslate\"><span class=\"pre\">REMOTE_USER</span></code> δεν έχει οριστεί.</p>\n<p>Η διαχείριση των χρηστών του Django, όπως τα views μέσα στο <code class=\"docutils literal notranslate\"><span class=\"pre\">contrib.admin</span></code> και η εντολή <a class=\"reference internal\" href=\"/el/3.0/ref/django-admin/#django-admin-createsuperuser\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">createsuperuser</span></code></a>, δεν δουλεύουν με τους απομακρυσμένους χρήστες (remote users). Αυτά τα interfaces δουλεύουν με τους χρήστες οι οποίοι είναι αποθηκευμένοι στην βάση δεδομένων ανεξαρτήτως της ρύθμισης <code class=\"docutils literal notranslate\"><span class=\"pre\">AUTHENTICATION_BACKENDS</span></code>.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Σημείωση</p>\n<p>Επειδή η κλάση <code class=\"docutils literal notranslate\"><span class=\"pre\">RemoteUserBackend</span></code> κληρονομεί από την <code class=\"docutils literal notranslate\"><span class=\"pre\">ModelBackend</span></code>, θα έχετε στην διάθεση σας όλο το API, που είναι υλοποιημένο στο <code class=\"docutils literal notranslate\"><span class=\"pre\">ModelBackend</span></code> και αφορά τον έλεγχο δικαιωμάτων των χρηστών.</p>\n<p>Οι χρήστες με το attribute <a class=\"reference internal\" href=\"/el/3.0/ref/contrib/auth/#django.contrib.auth.models.User.is_active\" title=\"django.contrib.auth.models.User.is_active\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">is_active=False</span></code></a> δεν θα τους επιτραπεί να πιστοποιηθούν. Για να το παρακάμψετε αυτό χρησιμοποιήστε την κλάση <a class=\"reference internal\" href=\"/el/3.0/ref/contrib/auth/#django.contrib.auth.backends.AllowAllUsersRemoteUserBackend\" title=\"django.contrib.auth.backends.AllowAllUsersRemoteUserBackend\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">AllowAllUsersRemoteUserBackend</span></code></a>.</p>\n</aside>\n<p>Οι χρήστες με το attribute <a class=\"reference internal\" href=\"/el/3.0/ref/contrib/auth/#django.contrib.auth.models.User.is_active\" title=\"django.contrib.auth.models.User.is_active\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">is_active=False</span></code></a> δεν θα τους επιτραπεί να πιστοποιηθούν. Για να το παρακάμψετε αυτό χρησιμοποιήστε την κλάση <a class=\"reference internal\" href=\"/el/3.0/ref/contrib/auth/#django.contrib.auth.backends.AllowAllUsersRemoteUserBackend\" title=\"django.contrib.auth.backends.AllowAllUsersRemoteUserBackend\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">AllowAllUsersRemoteUserBackend</span></code></a>.</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\">from</span><span class=\"w\"> </span><span class=\"nn\">django.contrib.auth.middleware</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">RemoteUserMiddleware</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">CustomHeaderMiddleware</span><span class=\"p\">(</span><span class=\"n\">RemoteUserMiddleware</span><span class=\"p\">):</span>\n    <span class=\"n\">header</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;HTTP_AUTHUSER&#39;</span>\n</code></pre></div>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Προειδοποίηση</p>\n<p>Να είστε εξαιρετικά προσεκτικοί όταν χρησιμοποιείτε κάποια subclass της <code class=\"docutils literal notranslate\"><span class=\"pre\">RemoteUserMiddleware</span></code> η οποία ορίζει δικό της HTTP header. Πρέπει να είστε σίγουροι ότι ο front-end web server ορίζει ή αφαιρεί πάντα αυτόν τον header βασιζόμενος στους κατάλληλους ελέγχους πιστοποίησης. Ποτέ να μην επιτρέπετε στον τελικό χρήστη να μπορεί να καταθέτει ψεύτικες (ή «πλαστογραφημένες») τιμές του header. Εφόσον οι HTTP headers, π.χ <code class=\"docutils literal notranslate\"><span class=\"pre\">X-Auth-User</span></code> και <code class=\"docutils literal notranslate\"><span class=\"pre\">X-Auth_User</span></code> ομαλοποιούνται και οι δύο στο κλειδί <code class=\"docutils literal notranslate\"><span class=\"pre\">HTTP_X_AUTH_USER</span></code> του <code class=\"docutils literal notranslate\"><span class=\"pre\">request.META</span></code>, θα πρέπει, επίσης, να ελέγξετε ότι ο web server σας δεν επιτρέπει πλαστογραφημένους headers οι οποίοι χρησιμοποιούν κάτω παύλες (“_”) αντί σκέτων παυλών (“-”).</p>\n<p>Η παραπάνω προειδοποίηση δεν αφορά μόνο την κλάση <code class=\"docutils literal notranslate\"><span class=\"pre\">RemoteUserMiddleware</span></code> με τις προεπιλεγμένες της ρυθμίσεις (<code class=\"docutils literal notranslate\"><span class=\"pre\">header</span> <span class=\"pre\">=</span> <span class=\"pre\">'REMOTE_USER'</span></code>), αφού ένα κλειδί που το όνομα του δεν ξεκινά με το <code class=\"docutils literal notranslate\"><span class=\"pre\">HTTP_</span></code> μέσα στο <code class=\"docutils literal notranslate\"><span class=\"pre\">request.META</span></code>, μπορεί να οριστεί μόνο από τον WSGI server σας και όχι απ’ ευθείας από ένα HTTP request header.</p>\n</aside>\n<p>Αν χρειάζεστε περισσότερο έλεγχο, μπορείτε να δημιουργήσετε το δικό σας backend πιστοποίησης ο οποίος θα κληρονομεί από την κλάση <a class=\"reference internal\" href=\"/el/3.0/ref/contrib/auth/#django.contrib.auth.backends.RemoteUserBackend\" title=\"django.contrib.auth.backends.RemoteUserBackend\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RemoteUserBackend</span></code></a> και θα παρακάμπτει (override) ένα ή περισσότερα από τα attributes και τις μεθόδους της.</p>\n</section>\n<section id=\"using-remote-user-on-login-pages-only\">\n<span id=\"persistent-remote-user-middleware-howto\"></span><h2>Χρήση του <code class=\"docutils literal notranslate\"><span class=\"pre\">REMOTE_USER</span></code> μόνο στις σελίδες login<a class=\"heading-anchor\" href=\"#using-remote-user-on-login-pages-only\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">RemoteUserMiddleware</span></code> authentication middleware assumes that the HTTP\nrequest header <code class=\"docutils literal notranslate\"><span class=\"pre\">REMOTE_USER</span></code> is present with all authenticated requests. That\nmight be expected and practical when Basic HTTP Auth with <code class=\"docutils literal notranslate\"><span class=\"pre\">htpasswd</span></code> or\nsimilar mechanisms are used, but with Negotiate (GSSAPI/Kerberos) or other\nresource intensive authentication methods, the authentication in the front-end\nHTTP server is usually only set up for one or a few login URLs, and after\nsuccessful authentication, the application is supposed to maintain the\nauthenticated session itself.</p>\n<p>Η middleware κλάση <a class=\"reference internal\" href=\"/el/3.0/ref/middleware/#django.contrib.auth.middleware.PersistentRemoteUserMiddleware\" title=\"django.contrib.auth.middleware.PersistentRemoteUserMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">PersistentRemoteUserMiddleware</span></code></a> παρέχει υποστήριξη για αυτήν ακριβώς την περίπτωση. Θα διατηρήσει το πιστοποιημένο session μέχρις ότου ο χρήστης κάνει logout ο ίδιος. Η κλάση μπορεί να χρησιμοποιηθεί ως αντικατάσταση της <a class=\"reference internal\" href=\"/el/3.0/ref/middleware/#django.contrib.auth.middleware.RemoteUserMiddleware\" title=\"django.contrib.auth.middleware.RemoteUserMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RemoteUserMiddleware</span></code></a> όπως αναφέρθηκε παραπάνω.</p>\n</section>","rootId":"authentication-using-remote-user","toc":[{"title":"Παραμετροποίηση","anchor":"configuration","children":[]},{"title":"Χρήση του REMOTE_USER μόνο στις σελίδες login","anchor":"using-remote-user-on-login-pages-only","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"Οδηγοί «Πως-να»","url":"/el/3.0/howto/"}],"prev":{"docname":"howto/index","title":"Οδηγοί «Πως-να»","url":"/el/3.0/howto/"},"next":{"docname":"howto/custom-management-commands","title":"Γράφοντας δικές σας django-admin εντολές","url":"/el/3.0/howto/custom-management-commands/"},"formats":{"html":"/el/3.0/howto/auth-remote-user/","markdown":"/el/3.0/howto/auth-remote-user.md","json":"/el/3.0/howto/auth-remote-user.json"},"source":"https://github.com/django/django/blob/stable/3.0.x/docs/howto/auth-remote-user.txt","official":"https://docs.djangoproject.com/el/3.0/howto/auth-remote-user/","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"]}