CASLogin.php 12.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
namespace Drupal\casost\Controller;

use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Controller\ControllerBase;
require_once '/home/haris/devel/eepal/drupal/modules/casost/src/CAS/phpCAS.php';
// require_once '/home/harisnd/public_html/drupal/modules/casost/src/CAS/phpCAS.php';
// use Drupal\casost\CAS\phpCAS;
// use CASException;
use Drupal\user\Entity\User;
use Drupal\Core\Database\Connection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;

class CASLogin extends ControllerBase
{

    protected $serverVersion;
    protected $serverHostname;
    protected $serverPort;
    protected $serverUri;
    protected $changeSessionId;
    protected $CASServerCACert;
    protected $CASServerCNValidate;
    protected $noCASServerValidation;
    protected $proxy;
    protected $handleLogoutRequests;
    protected $CASLang;
    protected $allowed1;
    protected $allowed1Value;
    protected $allowed2;
    protected $allowed2Value;

    protected $entity_query;
    protected $entityTypeManager;
    protected $logger;
    protected $connection;

    public function __construct(
    EntityTypeManagerInterface $entityTypeManager,
    QueryFactory $entity_query,
    Connection $connection,
    LoggerChannelFactoryInterface $loggerChannel)
    {
        $this->entityTypeManager = $entityTypeManager;
        $this->entity_query = $entity_query;
        $this->connection = $connection;
        $this->logger = $loggerChannel->get('casost');

    }

    public static function create(ContainerInterface $container)
    {
        return new static(
          $container->get('entity.manager'),
          $container->get('entity.query'),
          $container->get('database'),
          $container->get('logger.factory')
      );
    }

    public function loginGo(Request $request)
    {
        $CASOSTConfigs = $this->entityTypeManager->getStorage('casost_config')->loadByProperties(array('name' => 'casost_sch_sso_config'));
        $CASOSTConfig = reset($CASOSTConfigs);
        if ($CASOSTConfig) {
            $this->serverVersion = $CASOSTConfig->serverversion->value;
            $this->serverHostname = $CASOSTConfig->serverhostname->value;
            $this->serverPort = $CASOSTConfig->serverport->value;
            $this->serverUri = $CASOSTConfig->serveruri->value === null ? '' : $CASOSTConfig->serveruri->value;
            $this->changeSessionId = $CASOSTConfig->changesessionid->value;
            $this->CASServerCACert = $CASOSTConfig->casservercacert->value;
            $this->CASServerCNValidate = $CASOSTConfig->casservercnvalidate->value;
            $this->noCASServerValidation = $CASOSTConfig->nocasservervalidation->value;
            $this->proxy = $CASOSTConfig->proxy->value;
            $this->handleLogoutRequests = $CASOSTConfig->handlelogoutrequests->value;
            $this->CASLang = $CASOSTConfig->caslang->value;
            $this->allowed1 = $CASOSTConfig->allowed1->value;
            $this->allowed1Value = $CASOSTConfig->allowed1value->value;
            $this->allowed2 = $CASOSTConfig->allowed2->value;
            $this->allowed2Value = $CASOSTConfig->allowed2value->value;
        } else {
            $response = new Response();
            $response->setContent('forbidden. No config');
            $response->setStatusCode(Response::HTTP_FORBIDDEN);
            $response->headers->set('Content-Type', 'application/json');
            return $response;
        }

        try {

            // Enable debugging
            \phpCAS::setDebug("/home/haris/devel/eepal/drupal/modules/casost/phpcas.log");
            // Enable verbose error messages. Disable in production!
            \phpCAS::setVerbose(true);

            // Initialize phpCAS
            \phpCAS::client($this->serverVersion,
                $this->serverHostname,
                intval($this->serverPort),
                $this->serverUri,
                boolval($this->changeSessionId));

//            \phpCAS::setServerLoginURL('http://sso-test.sch.gr/login');
//            \phpCAS::setServerServiceValidateURL('http://sso-test.sch.gr/cas/samlValidate');

            // For production use set the CA certificate that is the issuer of the cert
            // on the CAS server and uncomment the line below
            // phpCAS::setCasServerCACert($cas_server_ca_cert_path);

            // For quick testing you can disable SSL validation of the CAS server.
            // THIS SETTING IS NOT RECOMMENDED FOR PRODUCTION.
            // VALIDATING THE CAS SERVER IS CRUCIAL TO THE SECURITY OF THE CAS PROTOCOL!
    //        \phpCAS::setNoCasServerValidation();

            // force CAS authentication
    //        \phpCAS::forceAuthentication();

            // at this step, the user has been authenticated by the CAS server
            // and the user's login name can be read with phpCAS::getUser().

            // logout if desired
    /*        if (isset($_REQUEST['logout'])) {
            	\phpCAS::logout();
            }

            $CASUser = \phpCAS::getUser();

            $this->logger->warning($CASUser); */







$this->logger->warning("hello1");
            if ($this->CASServerCACert) {
                if ($this->CASServerCNValidate) {
                    \phpCAS::setCasServerCACert($this->CASServerCACert, true);
                } else {
                    \phpCAS::setCasServerCACert($this->CASServerCACert, false);
                }
            }
$this->logger->warning("hello2");
            if ($this->noCASServerValidation) {
                $this->logger->warning("hello222");
                print_r("************no CAS server validation********");
                \phpCAS::setNoCasServerValidation();
            }
            $this->logger->warning("hello3");
            \phpCAS::handleLogoutRequests();
                        $this->logger->warning("hello33");
            if (!\phpCAS::forceAuthentication()) {
                $this->logger->warning("hello33333");
                $response = new Response();
                $response->setContent('forbidden. cannot force authentication');
                $response->setStatusCode(Response::HTTP_FORBIDDEN);
                $response->headers->set('Content-Type', 'application/json');
                return $response;
            }
            $this->logger->warning("hello4");
            $attributes = \phpCAS::getAttributes();
            $this->logger->warning("hello2");
            print_r($attributes);

            $isAllowed = true;
            $att1 = $attributes[$this->allowed1];
            $att2 = $attributes[$this->allowed2];
            if (!isset($att1) || !isset($att2)) {
                $isAllowed = false;
            }
            if (!is_array($attributes[$this->allowed1])) {
                $attributes[$this->allowed1] = [$attributes[$this->allowed1]];
            }
            if (!is_array($attributes[$this->allowed2])) {
                $attributes[$this->allowed2] = [$attributes[$this->allowed2]];
            }
            $found1 = false;
            foreach ($attributes[$this->allowed1] as $value) {
                if (1 === preg_match($this->allowed1Value, $value)) {
                    $found1 = true;
                }
            }
            $found2 = false;
            foreach ($attributes[$this->allowed2] as $value) {
                if (1 === preg_match($this->allowed2Value, $value)) {
                    $found2 = true;
                }
            }
            if (!$found1 || !$found2) {
                $isAllowed = false;
            }

    /*        if (!$isAllowed) {
                $response = new Response();
                $response->setContent(t('Access is allowed only to official school accounts'));
                $response->setStatusCode(Response::HTTP_FORBIDDEN);
                $response->headers->set('Content-Type', 'application/json;charset=UTF-8');
                return $response;
            } */

            $CASUser = \phpCAS::getUser();

            $this->logger->warning($CASUser);

            $filterAttribute = function ($attribute) use ($attributes) {
                if (!isset($attributes[$attribute])) {
                    return;
                }

                if (is_array($attributes[$attribute])) {
                    return $attributes[$attribute];
                }

                return $attributes[$attribute];
            };


            $epalToken = $this->authenticatePhase2($request, $CASUser);

            if ($epalToken) {
                return new RedirectResponse('/dist/#/school?auth_token=' . $epalToken.'&auth_role=director', 302, []);
            } else {
                $response = new Response();
                $response->setContent('forbidden');
                $response->setStatusCode(Response::HTTP_FORBIDDEN);
                $response->headers->set('Content-Type', 'application/json');
                return $response;
            }

/*            $identityClass = call_user_func($this->resolveIdentityClass);
            $identity      = new $identityClass(
                null,
                $identity,
                $filterAttribute('mail'),
                $filterAttribute('cn'),
                $filterAttribute('ou'),
                'CAS'
            ); */

/*
            $oauth = new OAuth($this->consumer_key, $this->consumer_secret, OAUTH_SIG_METHOD_PLAINTEXT, OAUTH_AUTH_TYPE_URI);
            $oauth->enableDebug();

            $uniqid = uniqid('sid');
            $requestToken = $oauth->getRequestToken($this->request_token_url, $this->callback_url . '?sid_ost=' . $uniqid);
                // store auth token

            $oauthostSession = $this->entityTypeManager()->getStorage('oauthost_session')->create(array(
        //    'langcode' => $language_interface->getId(),
              'langcode' => 'el',
              'user_id' => \Drupal::currentUser()->id(),
              'name' => $uniqid,
              'request_token' => $requestToken['oauth_token'],
              'request_token_secret' => $requestToken['oauth_token_secret'],
              'status' => 1
          ));
            $oauthostSession->save();
            header('Location: '.$this->user_authorization_url.'?oauth_token='.$requestToken['oauth_token']);
            $this->logger->warning('redirected to:'.$this->user_authorization_url.'?oauth_token='.$requestToken['oauth_token']); */
//            exit;
        } catch (\Exception $e) {
            $this->logger->warning($e->getMessage());
            $response = new Response();
            $response->setContent('forbidden');
            $response->setStatusCode(Response::HTTP_FORBIDDEN);
            $response->headers->set('Content-Type', 'application/json');
            return $response;
        }
    }




    public function authenticatePhase2($request, $CASUser)
    {
    $trx = $this->connection->startTransaction();
    try {

        $currentTime = time();

        $epalToken = md5(uniqid(mt_rand(), true));

            $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('mail' => $CASUser));
            $user = reset($users);
            if ($user) {
                $user->setPassword($epalToken);
                $user->setUsername($epalToken);
                $user->save();
            }


        if ($user === null || !$user) {

            //Create a User
            $user = User::create();
            //Mandatory settings
            $unique_id = uniqid('####');
            $user->setPassword($epalToken);
            $user->enforceIsNew();
            $user->setEmail($CASUser);
            $user->setUsername($epalToken); //This username must be unique and accept only a-Z,0-9, - _ @ .
            $user->activate();
            $user->set('init', $unique_id);

            //Set Language
            $language_interface = \Drupal::languageManager()->getCurrentLanguage();
            $user->set('langcode', $language_interface->getId());
            $user->set('preferred_langcode', $language_interface->getId());
            $user->set('preferred_admin_langcode', $language_interface->getId());

            //Adding default user role
            $user->addRole('director');
            $user->save();
        }

/*            $users = $this->entityTypeManager->getStorage('user')->loadByProperties(array('mail' => $CASUser->email));
            $user = reset($users);
            if ($user) {
                $this->logger->warning('userid 190='.$user->id().'*** name='.$user->name->value);

            } else {
                return false;
            }

        }
        $this->oauthostSession->delete(); */

        return $epalToken;
    } catch (OAuthException $e) {
        $this->logger->warning($e->getMessage());
        $trx->rollback();
        return false;
    } catch (\Exception $ee) {
        $this->logger->warning($ee->getMessage());
        $trx->rollback();
        return false;
    }

        return false;
    }

}