Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Single sign-on between WordPress and Django

I have two web applications on Apache Ubuntu 16.04 Server:

  1. WordPress 4.8.1 website
  2. Django 1.11 application (with django-rest-framework)

I want to install a Single Sign-On service (SSO). For instance, User logs on WordPress, then when he goes to Django website, he is already connected. Actually I don't find anything about SSO between WordPress and Django. Do you have an idea how to do it ?

like image 463
B. Mike Avatar asked Mar 08 '23 05:03

B. Mike


2 Answers

I Used this https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/ for WP login

I created two insert function for both auth_user(Django) and wp_users(Wordpress) install library

pip install passlib

serializer.py (im using django rest_framework jwt)

from passlib.hash import md5_crypt

from django.contrib.auth.hashers import make_password

registration serializer function (auth_user)

pwd_hash = make_password(password) 
usrobj = User(username = username, email = email, is_active=True, password = pwd_hash) 
usrobj.save()

duplicate registration for Wordpress dbtable

wph = md5_crypt.hash(password)
wpusrobj = WpUsers(user_login = str(username), user_pass = str(wph), user_nicename='', user_email = str(email), user_url='', user_activation_key='', display_name='', user_status=0)
wpusrobj.save()

make sure you run fake migrate your wp tables

this is how you get the table to model

python manage.py inspectdb > models.py

it should look like this: (i just add some null and blank)

class WpUsers(models.Model):
    id = models.BigAutoField(db_column='ID', primary_key=True)  # Field name made lowercase.
    user_login = models.CharField(max_length=60)
    user_pass = models.CharField(max_length=255)
    user_nicename = models.CharField(max_length=50, blank=True, null=True)
    user_email = models.CharField(max_length=100)
    user_url = models.CharField(max_length=100, blank=True, null=True)
    user_registered = models.DateTimeField(("Created Date"), default=timezone.now, blank=True, null=True)
    user_activation_key = models.CharField(max_length=255, blank=True, null=True)
    user_status = models.IntegerField(null=True, blank=True)
    display_name = models.CharField(max_length=250, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'wp_users'
like image 56
ohmcodes Avatar answered Mar 18 '23 04:03

ohmcodes


Use below plugin implement JWT token for authentication in wordpress website https://wordpress.org/plugins/jwt-authentication-for-wp-rest-api/

After successful login in wordpress website redirect to Django website, When logout in Django . destroy the session in wordpress viceversa

like image 39
Gnanasekaran Loganathan Avatar answered Mar 18 '23 03:03

Gnanasekaran Loganathan