Wordpress Moodle SSO

Wordpress Moodle SSO

by Zil Vis -
Number of replies: 1

**Where I am:**
I have Wordpress and Moodle up and running. Also I have LDAP server. Users are able to login using same name and password to both systems successfully.


----------


**What I am trying to achieve:**
When users log in to Wordpress, they automatically log in to Moodle (SSO) too.


----------


**Current code:**
On Wordpress user authentication I get his username and password.

add_filter('authenticate', 'zg_sso_auth_signon', 30, 3);

function zg_sso_auth_signon($user, $username, $password) {
if ($username != "") {
$data = array(
'username' => $username,
'password' => $password,
'Submit' => 'Login'
);
$response = post_to_url("http://domain.com/moodle/login/index.php", $data);
update_option('moodle_response', $response);
}
return $user;
}

And post it using cURL to Moodle login page.

function post_to_url($url, $data) {
$EMAIL = $data['username'];
$PASSWORD = $data['password'];
$cookie_file_path = plugin_dir_path(__FILE__) . 'cookie.txt';
$agent = "Nokia-Communicator-WWW-Browser/2.0 (Geos 3.0 Nokia-9000i)";

$ch = curl_init();

$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);


// set postfields using what we extracted from the form
$POSTFIELDS = http_build_query($data);

// change URL to login URL
curl_setopt($ch, CURLOPT_URL, $url);

// set post options
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);

// perform login
$result = curl_exec($ch);

preg_match('/^Set-Cookie:\s*([^;]*)/mi', $result, $m);
parse_str($m[1], $cookies);
setcookie("MoodleSession",$cookies['MoodleSession'],"Session","/moodle/","domain.com");
//return $cookies['MoodleSession'];
}
**And I stuck** with last line (setcookie...). How I should set moodle session coockie, that when I open moodle in my browser I would be logged in?

Average of ratings: -
In reply to Zil Vis

Re: Wordpress Moodle SSO

by dewey decibel -
I have a solution but it relies on CORS. 
please read this page about how to setup CORS on your server:

then add this code to your functions.php in wordpress theme folder.  Keep in mind you have to change it slightly to have your url instead.

seriously this is my first and LAST post on moodle forums, this is the worst forum software i have even encountered.

For some reason i can't post code here to look good, and i got flagged as a spammer when i tried to post the pastie link so here it is, you have to fix the url slightly as you would expect it to be formatted normally.
http pastie org/9344952

function moodle_sso ()
{ ?>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
function crossDomainPost(user, pass) {
$.ajax({
   type: 'POST',
   cache: false,
 xhrFields: {
   withCredentials: true
 },
 crossDomain: true,
   data: {"username":user, "password":pass},
   success: function(responseData, textStatus, jqXHR) {
      $('#loginform').submit();
      // console.log(textStatus+' with POST')
   },
   error: function (responseData, textStatus, errorThrown) {
      // eeeek! failed to login to moodle, this is bad.
   }
});
}
$(document).ready(function(){
$('#wp-submit').click(function(ev){
ev.preventDefault();
return crossDomainPost($('#user_login').val(), $('#user_pass').val());
})
})
</script>
<?php
}
add_action('login_footer', 'moodle_sso');