{"id":75905,"date":"2020-01-20T12:31:15","date_gmt":"2020-01-20T07:01:15","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=75905"},"modified":"2024-04-12T14:17:42","modified_gmt":"2024-04-12T08:47:42","slug":"integrating-with-an-authentication-system","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-an-authentication-system\/","title":{"rendered":"Integrating with an Authentication System"},"content":{"rendered":"<p><strong>It\u2019s possible to integrate Django with an existing authentication system<\/strong> \u2014 another source of usernames and passwords or authentication methods. For example, your company may already have an LDAP setup that stores a username and password for every employee. It would be a hassle for both the network administrator and the users themselves if users had separate accounts in LDAP and the Django-based applications.<\/p>\n<p>To handle situations like this, the Django authentication system lets you plug in other authentication sources. You can override Django\u2019s default database-based scheme, or you can use<br \/>\nthe default system in tandem with other systems.<\/p>\n<p><strong>Specifying Authentication Back-ends<\/strong> &#8211; Behind the scenes, Django maintains a list of \u201cauthentication back-ends\u201d that it checks for authentication. When somebody calls django.contrib.auth.authenticate(), Django tries authenticating across all of its authentication back-ends. If the first authentication method fails, Django tries the second one, and so on, until all back-ends have been attempted.<\/p>\n<p>The list of authentication back-ends to use is specified in the AUTHENTICATION_BACKENDS setting. This should be a tuple of Python path names that point to Python classes that know how to authenticate. These classes can be anywhere on your Python path. By default, AUTHENTICATION_BACKENDS is set to the following:<\/p>\n<p>(&#8216;django.contrib.auth.backends.ModelBackend&#8217;,)<\/p>\n<p>That\u2019s the basic authentication scheme that checks the Django users database. The order of AUTHENTICATION_BACKENDS matters, so if the same username and password are valid in multiple back-ends, Django will stop processing at the first positive match.<\/p>\n<p><strong>Writing an Authentication Back-end<\/strong> &#8211; An authentication back-end is a class that implements two methods: get_user(id) and authenticate(**credentials). The get_user method takes an id \u2014 which could be a username, database ID, or whatever \u2014 and returns a User object. The authenticate method takes credentials as keyword arguments. Most of the time it looks like this:<\/p>\n<p>class MyBackend(object):<br \/>\ndef authenticate(self, username=None, password=None):<br \/>\n# Check the username\/password and return a User.<\/p>\n<p>But it could also authenticate a token, like so:<\/p>\n<p>class MyBackend(object):<br \/>\ndef authenticate(self, token=None):<br \/>\n# Check the token and return a User.<\/p>\n<p>Either way, authenticate should check the credentials it gets, and it should return a User object that matches those credentials, if the credentials are valid. If they\u2019re not valid, it should return None. The Django admin system is tightly coupled to Django\u2019s own database-backed User object. The best way to deal with this is to create a Django User object for each user that exists for your back-end (e.g., in your LDAP directory, your external SQL database, etc.). Either you can write a script to do this in advance or your authenticate method can do it the first time a user logs in.<\/p>\n<p>Here\u2019s an example back-end that authenticates against a username and password variable defined<br \/>\nin your settings.py file and creates a Django User object the first time a user authenticates:<\/p>\n<p>from django.conf import settings<br \/>\nfrom django.contrib.auth.models import User, check_password<br \/>\nclass SettingsBackend(object):<br \/>\n&#8220;&#8221;&#8221;<br \/>\nAuthenticate against the settings ADMIN_LOGIN and ADMIN_PASSWORD.<br \/>\nUse the login name, and a hash of the password. For example:<br \/>\nADMIN_LOGIN = &#8216;admin&#8217;<br \/>\nADMIN_PASSWORD = &#8216;sha1$4e987$afbcf42e21bd417fb71db8c66b321e9fc33051de&#8217;<br \/>\n&#8220;&#8221;&#8221;<br \/>\ndef authenticate(self, username=None, password=None):<br \/>\nlogin_valid = (settings.ADMIN_LOGIN == username)<br \/>\npwd_valid = check_password(password, settings.ADMIN_PASSWORD)<br \/>\nif login_valid and pwd_valid:<br \/>\ntry:<br \/>\nuser = User.objects.get(username=username)<br \/>\nexcept User.DoesNotExist:<br \/>\n# Create a new user. Note that we can set password<br \/>\n# to anything, because it won&#8217;t be checked; the password<br \/>\n# from settings.py will.<br \/>\nuser = User(username=username, password=&#8217;get from settings.py&#8217;)<br \/>\nuser.is_staff = True<br \/>\nuser.is_superuser = True<br \/>\nuser.save()<br \/>\nreturn user<br \/>\nreturn None<\/p>\n<p>def get_user(self, user_id):<br \/>\ntry:<br \/>\nreturn User.objects.get(pk=user_id)<br \/>\nexcept User.DoesNotExist:<br \/>\nreturn None<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It\u2019s possible to integrate Django with an existing authentication system \u2014 another source of usernames and passwords or authentication methods. For example, your company may already have an LDAP setup that stores a username and password for every employee. It would be a hassle for both the network administrator and the users themselves if users&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8655],"tags":[8857],"class_list":["post-75905","page","type-page","status-publish","hentry","category-django-web-development","tag-integrating-with-an-authentication-system"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Integrating with an Authentication System - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-an-authentication-system\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Integrating with an Authentication System - Tutorial\" \/>\n<meta property=\"og:description\" content=\"It\u2019s possible to integrate Django with an existing authentication system \u2014 another source of usernames and passwords or authentication methods. For example, your company may already have an LDAP setup that stores a username and password for every employee. It would be a hassle for both the network administrator and the users themselves if users...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-an-authentication-system\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:47:42+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-an-authentication-system\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-an-authentication-system\/\",\"name\":\"Integrating with an Authentication System - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2020-01-20T07:01:15+00:00\",\"dateModified\":\"2024-04-12T08:47:42+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-an-authentication-system\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-an-authentication-system\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-an-authentication-system\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Integrating with an Authentication System\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Integrating with an Authentication System - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-an-authentication-system\/","og_locale":"en_US","og_type":"article","og_title":"Integrating with an Authentication System - Tutorial","og_description":"It\u2019s possible to integrate Django with an existing authentication system \u2014 another source of usernames and passwords or authentication methods. For example, your company may already have an LDAP setup that stores a username and password for every employee. It would be a hassle for both the network administrator and the users themselves if users...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-an-authentication-system\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:47:42+00:00","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-an-authentication-system\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-an-authentication-system\/","name":"Integrating with an Authentication System - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2020-01-20T07:01:15+00:00","dateModified":"2024-04-12T08:47:42+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-an-authentication-system\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-an-authentication-system\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-an-authentication-system\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Integrating with an Authentication System"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75905","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=75905"}],"version-history":[{"count":3,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75905\/revisions"}],"predecessor-version":[{"id":76899,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75905\/revisions\/76899"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=75905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=75905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=75905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}