From 131a818d244eea55a4e038c654c72ceee0a17526 Mon Sep 17 00:00:00 2001 From: Christophe Nowicki Date: Wed, 18 Aug 2004 15:03:56 +0000 Subject: Service Provider written in PHP (login is working and logout will work soon ;0) --- php/Attic/examples/sample-sp/admin_user.php | 162 +++++++++++++++++++ php/Attic/examples/sample-sp/assertionConsumer.php | 174 +++++++++++++++++++++ php/Attic/examples/sample-sp/config.php.inc | 27 ++++ php/Attic/examples/sample-sp/index.php | 119 ++++++++++++++ php/Attic/examples/sample-sp/install.php | 130 +++++++++++++++ php/Attic/examples/sample-sp/login.php | 58 +++++++ php/Attic/examples/sample-sp/logout.php | 45 ++++++ php/Attic/examples/sample-sp/register.php | 83 ++++++++++ 8 files changed, 798 insertions(+) create mode 100644 php/Attic/examples/sample-sp/admin_user.php create mode 100644 php/Attic/examples/sample-sp/assertionConsumer.php create mode 100644 php/Attic/examples/sample-sp/config.php.inc create mode 100644 php/Attic/examples/sample-sp/index.php create mode 100755 php/Attic/examples/sample-sp/install.php create mode 100644 php/Attic/examples/sample-sp/login.php create mode 100644 php/Attic/examples/sample-sp/logout.php create mode 100644 php/Attic/examples/sample-sp/register.php (limited to 'php') diff --git a/php/Attic/examples/sample-sp/admin_user.php b/php/Attic/examples/sample-sp/admin_user.php new file mode 100644 index 00000000..83e9b7b1 --- /dev/null +++ b/php/Attic/examples/sample-sp/admin_user.php @@ -0,0 +1,162 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + include "config.php.inc"; + + require_once 'DB.php'; + + + $db = &DB::connect($dsn); + + if (DB::isError($db)) + die($db->getMessage()); + + if (!empty($_GET['dump'])) { + $query = "SELECT identity_dump FROM users WHERE user_id='" . $_GET['dump'] . "'"; + $res =& $db->query($query); + if (DB::isError($res)) + print $res->getMessage(). "\n"; + $row = $res->fetchRow(); +?> + + + + + + + + + + + +
Identity Dump
+ +
Close
+ + +query($query); + if (DB::isError($res)) + print $res->getMessage(). "\n"; + + $query = "DELETE FROM users WHERE user_id='" . $_GET['del'] . "'" ; + $res =& $db->query($query); + if (DB::isError($res)) + print $res->getMessage(). "\n"; + + } + + + $query = "SELECT * FROM users"; + $res =& $db->query($query); + if (DB::isError($res)) + print $res->getMessage(). "\n"; +?> + + + +Lasso Service Provider Example : Users Management + + + + + + +numCols(); + $tableinfo = $db->tableInfo($res); +?> + +" . $tableinfo[$i]['name'] .""; + } +?> + + + +fetchRow()) { +?> + + + + + + + + + + + + + + +
Users
 
+ view"; + break; + + default: + echo (empty($row[$i])) ? " " : $row[$i]; + } + ?> + + delete +
 Total: numRows();?> Users
+ + + + +disconnect(); +?> diff --git a/php/Attic/examples/sample-sp/assertionConsumer.php b/php/Attic/examples/sample-sp/assertionConsumer.php new file mode 100644 index 00000000..a84cb847 --- /dev/null +++ b/php/Attic/examples/sample-sp/assertionConsumer.php @@ -0,0 +1,174 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + include "config.php.inc"; + + require_once 'DB.php'; + + + if (!$_GET['SAMLart']) { + exit(1); + } + + session_start(); + + lasso_init(); + + $server_dump = file_get_contents($server_dump_filename); + + $server = lasso_server_new_from_dump($server_dump); + + $login = lasso_login_new($server); + + lasso_login_init_request($login, $_SERVER['QUERY_STRING'], lassoHttpMethodRedirect); + lasso_login_build_request_msg($login); + + $profile = lasso_cast_to_profile($login); + + + $msg_url = lasso_profile_get_msg_url($profile); + $msg_body = lasso_profile_get_msg_body($profile); + + /*print "msg_url : " . $msg_url . "\n
"; + print "msg_body : " . $msg_body . "\n
"; */ + + $url = parse_url($msg_url); + + $soap = sprintf( + "POST %s HTTP/1.1\r\nHost: %s:%d\r\nAccept-Encoding: identity\r\nContent-Length: %d\r\nContent-Type: text/xml\r\nAccept: text/xml,application/xml,application/xhtml+xml,text/html\r\nConnection: close\r\n\r\n%s\r\n", + $url['path'], $url['host'], $url['port'], strlen($msg_body), $msg_body); + + + # PHP 4.3.0 with OpenSSL support required + $fp = fsockopen("ssl://" . $url['host'], $url['port'], $errno, $errstr, 30) or die($errstr ($errno)); + fwrite($fp, $soap); + $ret = fgets($fp); + + if (!preg_match("/^HTTP\/1\\.. 200/i", $ret)) { + die("Wrong artifact"); + } + + while (!feof($fp)) { + $reponse .= @fread($fp, 8192); + } + + fclose($fp); + + list($header, $body) = preg_split("/\r\n\r\n/", $reponse, 2); + + lasso_login_process_response_msg($login, $body); + $nameidentifier = lasso_profile_get_nameidentifier($profile); + + // print "nameidentifier: " . $nameidentifier . "
\n"; + + + # Look for the name_identifier in user db. + $options = array( + 'debug' => 2, + ); + $db = &DB::connect($dsn, $options); + + if (DB::isError($db)) + die($db->getMessage()); + + $query = "SELECT user_id FROM nameidentifiers WHERE name_identifier='$nameidentifier'"; + $res =& $db->query($query); + + if (DB::isError($res)) + die($res->getMessage()); + + if ($res->numRows() > 0) + { + $row =& $res->fetchRow(); + $user_id = $row[0]; + // print "user id : " . $user_id . "
\n"; + + # Get Identity Dump from the data base + $query = "SELECT identity_dump FROM users WHERE user_id='$user_id'"; + $res =& $db->query($query); + + if (DB::isError($db)) + die($db->getMessage()); + + $row =& $res->fetchRow(); + $identity_dump = $row[0]; + + lasso_profile_set_identity_from_dump($profile, $identity_dump); + + $res->free(); + + lasso_login_accept_sso($login); + } + else + { + // New User + lasso_login_accept_sso($login); + + $identity = lasso_profile_get_identity($profile); + $identity_dump = lasso_identity_dump($identity); + // print "identity_dump: " . $identity_dump . "
\n"; + + $session = lasso_profile_get_session($profile); + $session_dump = lasso_session_dump($session); + // print "session_dump: " . $session_dump . "
\n"; + + // Insert into users + $identity_dump_quoted = $db->quoteSmart($identity_dump); + $query = "INSERT INTO users (user_id,identity_dump,created) VALUES(nextval('user_id_seq'), $identity_dump_quoted, NOW())"; + $res =& $db->query($query); + if (DB::isError($res)) + print $res->getMessage(). "\n"; + + + // Get UserID + $query = "SELECT last_value FROM user_id_seq"; + $res =& $db->query($query); + if (DB::isError($res)) + print $res->getMessage(). "\n"; + $row = $res->fetchRow(); + $user_id = $row[0]; + + // Insert into nameidentifiers + $query = "INSERT INTO nameidentifiers VALUES('$nameidentifier', '$user_id')"; + $res =& $db->query($query); + if (DB::isError($res)) + print $res->getMessage(). "\n"; + + + $_SESSION["nameidentifier"] = $nameidentifier; + $_SESSION["session_dump"] = $session_dump; + $_SESSION["user_id"] = $user_id; + + $url = "register.php"; + } + + $db->disconnect(); + + lasso_shutdown(); + + header("Request-URI: $url"); + header("Content-Location: $url"); + header("Location: $url"); + exit(); +?> diff --git a/php/Attic/examples/sample-sp/config.php.inc b/php/Attic/examples/sample-sp/config.php.inc new file mode 100644 index 00000000..baf26b4a --- /dev/null +++ b/php/Attic/examples/sample-sp/config.php.inc @@ -0,0 +1,27 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + $server_dump_filename = "lasso_server_dump.xml"; + + $dsn = "pgsql://sp:sp@localhost/sp"; +?> diff --git a/php/Attic/examples/sample-sp/index.php b/php/Attic/examples/sample-sp/index.php new file mode 100644 index 00000000..73179fb6 --- /dev/null +++ b/php/Attic/examples/sample-sp/index.php @@ -0,0 +1,119 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + if(!extension_loaded('lasso')) { + $ret = @dl('lasso.' . PHP_SHLIB_SUFFIX); + if ($ret == FALSE) + { + + print "

The Lasso Extension is not available
"; + print "Please check your PHP extensions
"; + print "You can get more informations about Lasso at
"; + print "http://lasso.entrouvert.org/

"; + exit(); + } + } + + session_start(); + + lasso_init(); + +?> + + + +Lasso Service Provider Example + + + +

+ + + + + + + + + + + + + + + + + + + + + + + +
Serice Provider Administration
Users Management
Federation Administration
Serice Provider Fonctionnality
Login!
Logout!
+ + + + + User is not logged in!"; + } + else + { + ?> + + + + + + + + + + + + + + + +
Status
User is logged in!
Name Identifier:
UserID:
Last Name:
First Name:
+ +

Lasso Version :

+ +
+

Copyright © 2004 Entr'ouvert

+ + + + + diff --git a/php/Attic/examples/sample-sp/install.php b/php/Attic/examples/sample-sp/install.php new file mode 100755 index 00000000..7304a27b --- /dev/null +++ b/php/Attic/examples/sample-sp/install.php @@ -0,0 +1,130 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + include "config.php.inc"; + + require_once 'DB.php'; + + print "Install script for L.A.S.S.O (Liberty Alliance Single Sign On)\n"; + + lasso_init(); + print "$server_dump_filename: "; + + + # Create XML Server Dump + if (file_exists($server_dump_filename)) + { + print "file already exists.\n"; + } + else + { + $server = lasso_server_new( + "/home/cnowicki/mcvs/lasso/tests/data/sp1-la/metadata.xml", + "/home/cnowicki/mcvs/lasso/tests/data/sp1-la/public-key.pem", + "/home/cnowicki/mcvs/lasso/tests/data/sp1-la/private-key-raw.pem", + "/home/cnowicki/mcvs/lasso/tests/data/sp1-la/certificate.pem", + lassoSignatureMethodRsaSha1); + + lasso_server_add_provider($server, + "/home/cnowicki/mcvs/lasso/tests/data/idp1-la/metadata.xml", + "/home/cnowicki/mcvs/lasso/tests/data/idp1-la/public-key.pem", + "/home/cnowicki/mcvs/lasso/tests/data/ca1-la/certificate.pem" + ); + + $dump = lasso_server_dump($server); + $fd = fopen($server_dump_filename, "w"); + fwrite($fd, $dump); + print "wrote.\n"; + fclose($fd); + } + + print "Create User Database.\n"; + print "DSN : $dsn\n"; + + $options = array( + 'debug' => 2, + ); + + $db = &DB::connect($dsn, $options); + if (DB::isError($db)) { + die($db->getMessage()); + } + + + # Drop user_id_seq + print "DROP user_id_seq.\n"; + $query = "DROP SEQUENCE user_id_seq"; + $res =& $db->query($query); + if (DB::isError($res)) + die($res->getMessage()); + + + # Create user_id_seq + print "Create user_id_seq Sequence.\n"; + $query = "CREATE SEQUENCE user_id_seq"; + $res =& $db->query($query); + if (DB::isError($res)) + die($res->getMessage()); + + /* print "DROP users.\n"; + $query = "DROP TABLE users CASCADE"; + $res =& $db->query($query); + if (DB::isError($res)) + die($res->getMessage()); */ + + # Create local data base + print "Create users Table.\n"; + $query = "CREATE TABLE users ( + user_id varchar(100) primary key, + identity_dump text, + first_name varchar(50), + last_name varchar(50), + created timestamp)"; + $res =& $db->query($query); + if (DB::isError($res)) + die($res->getMessage()); + + /* print "DROP nameidentifiers.\n"; + $query = "DROP TABLE nameidentifiers"; + + $res =& $db->query($query); */ + + if (DB::isError($res)) + die($res->getMessage()); + + print "Create nameidentifiers Table.\n"; + $query = "CREATE TABLE nameidentifiers ( + name_identifier varchar(100) primary key, + user_id varchar(100), + FOREIGN KEY (user_id) REFERENCES users (user_id))"; + $res =& $db->query($query); + if (DB::isError($res)) + die($res->getMessage()); + + + $db->disconnect(); + + lasso_shutdown(); +?> diff --git a/php/Attic/examples/sample-sp/login.php b/php/Attic/examples/sample-sp/login.php new file mode 100644 index 00000000..4b70c095 --- /dev/null +++ b/php/Attic/examples/sample-sp/login.php @@ -0,0 +1,58 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + include "config.php.inc"; + + lasso_init(); + + $server_dump = file_get_contents($server_dump_filename); + + $server = lasso_server_new_from_dump($server_dump); + + $login = lasso_login_new($server); + + lasso_login_init_authn_request($login); + + $profile = lasso_cast_to_profile($login); + + $node = lasso_profile_get_request($profile); + + $lib_authn_request = lasso_cast_to_lib_authn_request($node); + + // lasso_lib_authn_request_set_forceauthn($lib_authn_request, TRUE); + lasso_lib_authn_request_set_ispassive($lib_authn_request, FALSE); + lasso_lib_authn_request_set_nameidpolicy($lib_authn_request, lassoLibNameIDPolicyTypeFederated); + lasso_lib_authn_request_set_consent($lib_authn_request, lassoLibConsentObtained); + + lasso_login_build_authn_request_msg($login, "https://idp1/metadata"); + + + $url = lasso_profile_get_msg_url($profile); + + header("Request-URI: $url"); + header("Content-Location: $url"); + header("Location: $url"); + exit(); +?> diff --git a/php/Attic/examples/sample-sp/logout.php b/php/Attic/examples/sample-sp/logout.php new file mode 100644 index 00000000..1d4ab750 --- /dev/null +++ b/php/Attic/examples/sample-sp/logout.php @@ -0,0 +1,45 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + include "config.php.inc"; + + session_start(); + + if (!isset($_SESSION["nameidentifier"])) { + print "User is not logged in"; + exit(0); + } + + lasso_init(); + + $server_dump = file_get_contents($server_dump_filename); + + $server = lasso_server_new_from_dump($server_dump); + + $logout = lasso_logout_new($server, lassoProviderTypeSp); + + // $profile = lasso_cast_to_profile($logout); + + lasso_shutdown(); +?> diff --git a/php/Attic/examples/sample-sp/register.php b/php/Attic/examples/sample-sp/register.php new file mode 100644 index 00000000..76b43380 --- /dev/null +++ b/php/Attic/examples/sample-sp/register.php @@ -0,0 +1,83 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + include "config.php.inc"; + + require_once 'DB.php'; + + session_start(); + + if (!isset($_SESSION["nameidentifier"])) { + print "User is not logged in"; + exit(0); + } + + switch($_POST['action']) { + case "submit": + $db = &DB::connect($dsn); + + if (DB::isError($db)) + die($db->getMessage()); + + $query = "UPDATE users SET first_name='" . $_POST['first_name'] . "',last_name='". $_POST['last_name'] ."' WHERE user_id='".$_SESSION["user_id"]."'"; + $res =& $db->query($query); + if (DB::isError($res)) + print $res->getMessage(). "\n"; + + $url = "index.php"; + header("Request-URI: $url"); + header("Content-Location: $url"); + header("Location: $url"); + break; + default: +?> + + + +Lasso Service Provider Example : Registration Form + + + +
+ + + + + + + + + + + +
Registration Form
First Name:
Last Name:
 
+ +
+ + + + -- cgit