From 544bba73de4ec3bfb6990cd572c91a2dc1092638 Mon Sep 17 00:00:00 2001
From: llaffont <llaffont@afi-sa.fr>
Date: Tue, 1 Dec 2015 10:01:01 +0100
Subject: [PATCH] dev #19304 : start CAS SSO / LeKiosk

add LeKiosk CAS link generation
---
 library/Class/AdminVar.php                    |  3 +-
 library/Class/LeKiosk/CasLink.php             | 35 +++++++++++++
 .../{LeKioskLink.php => LeKiosk/Link.php}     | 13 +++--
 library/Class/Systeme/ModulesMenu/LeKiosk.php |  2 +-
 tests/library/Class/LeKioskLinkTest.php       | 51 +++++++++++++++----
 5 files changed, 88 insertions(+), 16 deletions(-)
 create mode 100644 library/Class/LeKiosk/CasLink.php
 rename library/Class/{LeKioskLink.php => LeKiosk/Link.php} (88%)

diff --git a/library/Class/AdminVar.php b/library/Class/AdminVar.php
index 4d38ab9b6c4..cd565ef4526 100644
--- a/library/Class/AdminVar.php
+++ b/library/Class/AdminVar.php
@@ -272,7 +272,8 @@ class Class_AdminVarLoader extends Storm_Model_Loader {
 
                    'LEKIOSK_SSO_MODE' => Class_AdminVar_Meta::newCombo($this->_('Type de SSO lekiosk.com'),
                                                                        ['options' => ['selectOptions' => ['label' => $this->_('Mode d\'authentification'),
-                                                                                                          'multioptions' => ['0' => 'Lien', '1' => 'CAS']]]])->bePrivate(),
+                                                                                                          'multioptions' => ['' => 'Lien',
+                                                                                                                             'CAS' => 'CAS']]]])->bePrivate(),
 
                    'KIDILANGUES_VERSION' => Class_AdminVar_Meta::newCombo($this->_('Version de Kidilangues.'),
                                                                           ['options' => ['selectOptions' => ['label' => $this->_('Version'),
diff --git a/library/Class/LeKiosk/CasLink.php b/library/Class/LeKiosk/CasLink.php
new file mode 100644
index 00000000000..28320461f1d
--- /dev/null
+++ b/library/Class/LeKiosk/CasLink.php
@@ -0,0 +1,35 @@
+<?php
+/**
+ * Copyright (c) 2012-2014, Agence Française Informatique (AFI). All rights reserved.
+ *
+ * BOKEH is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
+ * the Free Software Foundation.
+ *
+ * There are special exceptions to the terms and conditions of the AGPL as it
+ * is applied to this software (see README file).
+ *
+ * BOKEH 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 AFFERO GENERAL PUBLIC LICENSE for more details.
+ *
+ * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
+ * along with BOKEH; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
+ */
+
+
+class Class_LeKiosk_CasLink {
+  public function url() {
+    return 'https://pro.lekiosk.com/AutentificationCas.aspx?'
+      . http_build_query(
+                         [
+                          'cas_fournisseur' => Class_Url::absolute(['controller' => 'cas-server-v10'], null, true),
+                          'id' => Class_AdminVar::get('LEKIOSK_ID'),
+                          'ReturnUrl' => $_SERVER['HTTP_REFERER']
+                         ] );
+  }
+}
+
+?>
\ No newline at end of file
diff --git a/library/Class/LeKioskLink.php b/library/Class/LeKiosk/Link.php
similarity index 88%
rename from library/Class/LeKioskLink.php
rename to library/Class/LeKiosk/Link.php
index bb9f9a03a5a..39ef6a72171 100644
--- a/library/Class/LeKioskLink.php
+++ b/library/Class/LeKiosk/Link.php
@@ -16,11 +16,11 @@
  *
  * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
  * along with BOKEH; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
  */
 
 
-class Class_LeKioskLink {
+class Class_LeKiosk_Link {
   const ROOT_URL = 'http://pro.lekiosk.com';
   const BASE_URL = '/lekiosque.authenticate.aspx?';
   const AES_KEY = '56FGH4sTOV9ZXr4Q';
@@ -31,7 +31,10 @@ class Class_LeKioskLink {
 
 
   public static function newFor($mail) {
-    return new self($mail);
+    return
+      ('CAS' == Class_AdminVar::get('LEKIOSK_SSO_MODE'))
+      ? new Class_LeKiosk_CasLink()
+      : new self($mail);
   }
 
 
@@ -41,7 +44,7 @@ class Class_LeKioskLink {
 
 
   public function url() {
-    return static::ROOT_URL 
+    return static::ROOT_URL
       . $this->baseUrl()
       . '&AccessHash=' . $this->accessHash();
   }
@@ -54,7 +57,7 @@ class Class_LeKioskLink {
 
   protected function baseUrl() {
     if (null === $this->_base_url)
-      $this->_base_url = static::BASE_URL 
+      $this->_base_url = static::BASE_URL
         . 'email=' . $this->cryptedMail()
         . '&id=' . Class_AdminVar::get('LEKIOSK_ID')
         ;
diff --git a/library/Class/Systeme/ModulesMenu/LeKiosk.php b/library/Class/Systeme/ModulesMenu/LeKiosk.php
index 3d32198aded..515d4b7d99a 100644
--- a/library/Class/Systeme/ModulesMenu/LeKiosk.php
+++ b/library/Class/Systeme/ModulesMenu/LeKiosk.php
@@ -41,7 +41,7 @@ class Class_Systeme_ModulesMenu_LeKiosk extends Class_Systeme_ModulesMenu_SSOAbs
       return '';
     }
 
-    return Class_LeKioskLink::newFor($user->getMail())->url();
+    return Class_LeKiosk_Link::newFor($user->getMail())->url();
   }
 
   public function isVisibleForProfil($profil) {
diff --git a/tests/library/Class/LeKioskLinkTest.php b/tests/library/Class/LeKioskLinkTest.php
index bcaea0f8c8e..67e56edca84 100644
--- a/tests/library/Class/LeKioskLinkTest.php
+++ b/tests/library/Class/LeKioskLinkTest.php
@@ -16,11 +16,13 @@
  *
  * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
  * along with BOKEH; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA 
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
  */
 
+abstract class LeKioskLinkModeLinkTestCase extends ModelTestCase {
+  protected
+    $_storm_default_to_volatile = true;
 
-class LeKioskLinkTest extends Storm_Test_ModelTestCase {
   public function setUp() {
     parent::setUp();
     $this->fixture('Class_Profil', [
@@ -28,24 +30,28 @@ class LeKioskLinkTest extends Storm_Test_ModelTestCase {
       'libelle' => 'portail',
       'mail_site' => '']);
 
-    Class_AdminVar::newInstanceWithId('LEKIOSK_ID', ['valeur' => '29']);
+    Class_AdminVar::set('LEKIOSK_ID', '29');
   }
+}
+
+
 
 
+class LeKioskLinkModeLinkTest extends LeKioskLinkModeLinkTestCase {
   /** @test */
-  public function urlForMyemailAtLekioskDotComShouldBeAsExpected() {
+  public function urlForMyemailAtLekioskDotComShouldContainsEncryptedEmail() {
     $expected = 'http://pro.lekiosk.com/lekiosque.authenticate.aspx?email=1671f18f515f49271985280397c1c2bc48e1bfa31a3b638cb22e510e4953d65e&id=29&AccessHash=e6715cdcc53d6d2b130f4025ea44b67872eb5455';
     $this->assertEquals(
-      $expected, 
-      Class_LeKioskLink::newFor('myemail@lekiosk.com')->url()
+      $expected,
+      Class_LeKiosk_Link::newFor('myemail@lekiosk.com')->url()
     );
   }
 
 
   /** @test */
-  public function withoutMailShouldBeAsExpected() {
+  public function withoutMailShouldBeNotContainsEmailKey() {
     $expected = 'http://pro.lekiosk.com/lekiosque.authenticate.aspx?email=18ca3d8ad40255ce09d5d20debc1e069&id=29&AccessHash=b78f9cd4198878b85de383ccddd27ab286fc47f6';
-    $this->assertEquals($expected, Class_LeKioskLink::newFor('')->url());
+    $this->assertEquals($expected, Class_LeKiosk_Link::newFor('')->url());
   }
 
 
@@ -53,7 +59,34 @@ class LeKioskLinkTest extends Storm_Test_ModelTestCase {
   public function withEmptyUserMailShouldGetSiteOrProfileMail() {
     Class_Profil::find(1)->setMailSite('toto@example.com');
     $expected = 'http://pro.lekiosk.com/lekiosque.authenticate.aspx?email=76903fe54055ab757db99c2370d89970e25c5b33b5a69cafc108c0031685af88&id=29&AccessHash=c5d56beb8fbd723bc562739cbc3f70ed43a79b9e';
-    $this->assertEquals($expected, Class_LeKioskLink::newFor('')->url());
+    $this->assertEquals($expected, Class_LeKiosk_Link::newFor('')->url());
+  }
+}
+
+
+
+
+class LeKioskLinkModeSSOTest extends LeKioskLinkModeLinkTestCase {
+  public function setUp() {
+    parent::setUp();
+
+    $_SERVER['HTTP_REFERER'] = 'http://localhost/view/notice';
+    Zend_Controller_Front::getInstance()
+      ->getRouter()
+      ->route(new Zend_Controller_Request_Http('http://localhost/'));
+    Class_AdminVar::set('LEKIOSK_SSO_MODE', 'CAS');
+  }
+
+
+  /** @test */
+  public function linkShouldBeCasUrlWithLeKioskId() {
+    $url = 'https://pro.lekiosk.com/AutentificationCas.aspx?'
+      .'cas_fournisseur=' . urlencode('http://localhost' . BASE_URL . '/cas-server-v10')
+      .'&id=29'
+      .'&ReturnUrl=' . urlencode('http://localhost/view/notice');
+
+    $this->assertEquals($url,
+                        Class_LeKiosk_Link::newFor('')->url());
   }
 }
 ?>
\ No newline at end of file
-- 
GitLab