Completed
Push — master ( 74e5e8...8844e1 )
by
unknown
02:51
created

HttpAuth::current()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
/**
4
 * Authentication mechanism using a BasicAuth request.
5
 *
6
 * @author Andre Lohmann <[email protected]>
7
 */
8
class HttpAuth extends Object implements IAuth {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
10
    public static function authenticate($email, $password) {
11
        $authenticator = Injector::inst()->get('ApiMemberAuthenticator');
12
        if($user = $authenticator->authenticate(['Password' => $password, 'Email' => $email])) {
13
	        return self::createSession($user);
14
        }
15
    }
16
17
	/**
18
	 * @param Member $user
19
	 * @return ApiSession
20
	 */
21 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...
22
		$user->logIn();
23
		/** @var Member $user */
24
		$user = DataObject::get(Config::inst()->get('BaseRestController', 'Owner'))->byID($user->ID);
25
26
		// create session
27
		$session = ApiSession::create();
28
		$session->User = $user;
29
		$session->Token = AuthFactory::generate_token($user);
30
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) ? DataObject::get(Config::inst()->get('BaseRestController', 'Owner'))->byID($member->ID) : null;
51
        }
52
        
53
        /**
54
         * @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...
55
         */
56
        protected static function getBasicAuthMember(){
57
            $realm = Config::inst()->get('HttpAuth', 'Realm');
58
            $permissionCode = Config::inst()->get('HttpAuth', 'PermissionCode');
59
            $isRunningTests = (class_exists('SapphireTest', false) && SapphireTest::is_running_test());
60
            $tryUsingSessionLogin = $isRunningTests || Config::inst()->get('HttpAuth', 'TryUsingSessionLogin');
61
62
            try{
63
                $member = BasicAuth::requireLogin($realm, $permissionCode, $tryUsingSessionLogin);
64
                return $member;
65
            } catch (Exception $ex) {
66
                return null;
67
            }
68
        }
69
70
}
71