HttpAuth::createSession()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 1
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])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
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) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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
0 ignored issues
show
Documentation introduced by
Should the return type not be \Member|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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
0 ignored issues
show
Documentation introduced by
Should the return type not be \Member|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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