Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 0d015de

Browse files
committed
ENH: refs #250. create web api authentication component
1 parent cc7183c commit 0d015de

File tree

2 files changed

+56
-24
lines changed

2 files changed

+56
-24
lines changed

modules/api/controllers/IndexController.php

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
class Api_IndexController extends Api_AppController
2424
{
2525
public $_moduleModels = array('Userapi');
26+
public $_moduleComponents = array('Authentication');
2627
public $_models = array('Community', 'ItemRevision', 'Item', 'User', 'Folderpolicyuser', 'Folderpolicygroup', 'Folder');
2728
public $_components = array('Upload', 'Search', 'Uuid', 'Sortdao');
2829

@@ -403,30 +404,7 @@ private function _initApiCommons()
403404
/** Return the user dao */
404405
private function _getUser($args)
405406
{
406-
if(array_key_exists('useSession', $args))
407-
{
408-
return $this->userSession->Dao;
409-
}
410-
else
411-
{
412-
if(!array_key_exists('token', $args))
413-
{
414-
return 0;
415-
}
416-
$token = $args['token'];
417-
$userapiDao = $this->Api_Userapi->getUserapiFromToken($token);
418-
if(!$userapiDao)
419-
{
420-
throw new Exception('Invalid token', MIDAS_INVALID_TOKEN);
421-
}
422-
$userid = $userapiDao->getUserId();
423-
if($userid == 0)
424-
{
425-
return false;
426-
}
427-
$userDao = $this->User->load($userid);
428-
return $userDao;
429-
}
407+
return $this->ModuleComponent->Authentication->getUser($args, $this->userSession->Dao);
430408
}
431409

432410
/** Controller action handling REST request */
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/*=========================================================================
3+
MIDAS Server
4+
Copyright (c) Kitware SAS. 20 rue de la Villette. All rights reserved.
5+
69328 Lyon, FRANCE.
6+
7+
See Copyright.txt for details.
8+
This software is distributed WITHOUT ANY WARRANTY; without even
9+
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
10+
PURPOSE. See the above copyright notices for more information.
11+
=========================================================================*/
12+
13+
/** Web API Authentication Component */
14+
class Api_AuthenticationComponent extends AppComponent
15+
{
16+
17+
/** Constructor */
18+
function __construct()
19+
{
20+
}
21+
22+
/**
23+
* Gets the user dao from either the session (if via ajax)
24+
* or using token-based authentication otherwise.
25+
* Returns false for anonymous users.
26+
*/
27+
public function getUser($args, $sessionDao)
28+
{
29+
if(array_key_exists('useSession', $args))
30+
{
31+
return $sessionDao;
32+
}
33+
else
34+
{
35+
if(!array_key_exists('token', $args))
36+
{
37+
return 0;
38+
}
39+
$token = $args['token'];
40+
$userapiDao = $this->Api_Userapi->getUserapiFromToken($token);
41+
if(!$userapiDao)
42+
{
43+
throw new Exception('Invalid token', MIDAS_INVALID_TOKEN);
44+
}
45+
$userid = $userapiDao->getUserId();
46+
if($userid == 0)
47+
{
48+
return false;
49+
}
50+
$userDao = $this->User->load($userid);
51+
return $userDao;
52+
}
53+
}
54+
}

0 commit comments

Comments
 (0)