1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ntb\RestAPI; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Authentication mechanism using a BasicAuth request. |
7
|
|
|
* |
8
|
|
|
* @author Andre Lohmann <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class HttpAuth extends \SS_Object implements IAuth { |
11
|
|
|
|
12
|
|
|
public static function authenticate($email, $password) { |
13
|
|
|
$authenticator = \Injector::inst()->get('ApiMemberAuthenticator'); |
14
|
|
View Code Duplication |
if($user = $authenticator->authenticate(['Password' => $password, 'Email' => $email])) { |
|
|
|
|
15
|
|
|
return self::createSession($user); |
16
|
|
|
} |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param \Member $user |
21
|
|
|
* @return ApiSession |
22
|
|
|
*/ |
23
|
|
View Code Duplication |
public static function createSession($user) { |
|
|
|
|
24
|
|
|
$user->logIn(); |
25
|
|
|
/** @var \Member $user */ |
26
|
|
|
$user = \DataObject::get(\Config::inst()->get('BaseRestController', 'Owner'))->byID($user->ID); |
27
|
|
|
// create session |
28
|
|
|
$session = ApiSession::create(); |
29
|
|
|
$session->User = $user; |
30
|
|
|
$session->Token = AuthFactory::generate_token($user); |
31
|
|
|
return $session; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
View Code Duplication |
public static function delete($request) { |
|
|
|
|
35
|
|
|
$owner = self::current($request); |
36
|
|
|
if(!$owner) { |
37
|
|
|
throw new RestUserException("No session found", 404, 404); |
38
|
|
|
} |
39
|
|
|
$owner->logOut(); |
40
|
|
|
return true; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param \SS_HTTPRequest $request |
46
|
|
|
* @return \Member |
|
|
|
|
47
|
|
|
*/ |
48
|
|
|
public static function current($request) { |
49
|
|
|
$member = self::getBasicAuthMember(); |
50
|
|
|
return ($member instanceof \Member) ? |
51
|
|
|
\DataObject::get(\Config::inst()->get('BaseRestController', 'Owner'))->byID($member->ID) : null; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return \Member |
|
|
|
|
56
|
|
|
*/ |
57
|
|
|
protected static function getBasicAuthMember() { |
58
|
|
|
$realm = \Config::inst()->get('HttpAuth', 'Realm'); |
59
|
|
|
$permissionCode = \Config::inst()->get('HttpAuth', 'PermissionCode'); |
60
|
|
|
$isRunningTests = (class_exists('SapphireTest', false) && \SapphireTest::is_running_test()); |
61
|
|
|
$tryUsingSessionLogin = $isRunningTests || \Config::inst()->get('HttpAuth', 'TryUsingSessionLogin'); |
62
|
|
|
try { |
63
|
|
|
$member = \BasicAuth::requireLogin($realm, $permissionCode, $tryUsingSessionLogin); |
64
|
|
|
return $member; |
65
|
|
|
} catch (\Exception $ex) { |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.