Completed
Push — master ( ae7e79...4a5483 )
by Patrick
03:04
created

GoogleAuthenticator::getUser()   B

Complexity

Conditions 3
Paths 18

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 3
eloc 16
c 1
b 1
f 1
nc 18
nop 1
dl 0
loc 24
rs 8.9713
1
<?php
2
namespace Auth;
3
require dirname(__FILE__).'/../libs/google/src/Google/autoload.php';
4
5
class GoogleAuthenticator extends Authenticator
6
{
7
    protected $client;
8
    protected $token = null;
9
10
    public function __construct($params)
11
    {
12
        parent::__construct($params);
13
        if(!isset($params['client_secrets_path']))
14
        {
15
            throw new \Exception('Missing required parameter client_secrets_path!');
16
        }
17
        if(!isset($params['redirect_url']))
18
        {
19
            $params['redirect_url'] = 'https://'.$_SERVER['HTTP_HOST'].'/oauth2callback.php?src=google';
20
        }
21
        $this->token = \FlipSession::getVar('GoogleToken', null);
0 ignored issues
show
Documentation introduced by
null is of type null, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
22
        $this->client = new \Google_Client();
23
        $this->client->setAuthConfigFile($params['client_secrets_path']);
24
        $this->client->addScope(array(\Google_Service_Oauth2::USERINFO_PROFILE, \Google_Service_Oauth2::USERINFO_EMAIL));
25
        $this->client->setRedirectUri($params['redirect_url']);
26
    }
27
28
    /**
29
     * Get the link to login using this method
30
     *
31
     * @return string The link to login using this method
32
     */
33
    public function getSupplementLink()
34
    {
35
        $authUrl = $this->client->createAuthUrl();
36
        return '<a href="'.filter_var($authUrl, FILTER_SANITIZE_URL).'"><img src="/img/common/google_sign_in.png" style="width: 2em;"/></a>';
0 ignored issues
show
Bug Best Practice introduced by
The return type of return '<a href="' . fil...e="width: 2em;"/></a>'; (string) is incompatible with the return type of the parent method Auth\Authenticator::getSupplementLink of type boolean.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
37
    }
38
39
    public function authenticate($code, &$currentUser = false)
40
    {
41
        $googleUser = false;
42
        try
43
        {
44
            $this->client->authenticate($code);
45
            $this->token = $this->client->getAccessToken();
46
            \FlipSession::setVar('GoogleToken', $this->token);
47
            $oauth2Service = new \Google_Service_Oauth2($this->client);
48
            $googleUser = $oauth2Service->userinfo->get();
49
        }
50
        catch(\Exception $ex)
51
        {
52
            return self::LOGIN_FAILED;
53
        }
54
55
        $auth = \AuthProvider::getInstance();
56
        $localUsers = $auth->getUsersByFilter(new \Data\Filter('mail eq '.$googleUser->email));
57
        if($localUsers !== false && isset($localUsers[0]))
58
        {
59
            if($localUsers[0]->canLoginWith('google.com'))
60
            {
61
                $auth->impersonateUser($localUsers[0]);
62
                return self::SUCCESS;
63
            }
64
            $currentUser = $localUsers[0];
65
            return self::ALREADY_PRESENT;
66
        }
67
        else
68
        {
69
            $user = new PendingUser();
70
            $user->setEmail($googleUser->email);
71
            $user->setGivenName($googleUser->givenName);
72
            $user->setLastName($googleUser->familyName);
73
            $user->addLoginProvider('google.com');
74
            $ret = $auth->activatePendingUser($user);
75
            if($ret === false)
76
            {
77
                throw new \Exception('Unable to create user! '.$res);
78
            }
79
            return self::SUCCESS;
80
        }
81
    }
82
83
    public function getUser($data = false)
84
    {
85
        if($data === false)
86
        {
87
            $data = $this->token;
88
        }
89
        try
90
        {
91
            $this->client->setAccessToken($data);
92
            $oauth2Service = new \Google_Service_Oauth2($this->client);
93
            $googleUser = $oauth2Service->userinfo->get();
94
            $profileUser = array();
95
            $profileUser['mail'] = $googleUser->email;
96
            $profileUser['sn'] = $googleUser->familyName;
97
            $profileUser['givenName'] = $googleUser->givenName;
98
            $profileUser['displayName'] = $googleUser->name;
99
            $profileUser['jpegPhoto'] = base64_encode(file_get_contents($googleUser->picture));
100
            return $profileUser;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $profileUser; (array) is incompatible with the return type of the parent method Auth\Authenticator::getUser of type Auth\Auth\User|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
101
        }
102
        catch(\Exception $e)
103
        {
104
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type of the parent method Auth\Authenticator::getUser of type Auth\Auth\User|null.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
105
        }
106
    }
107
}
108
/* vim: set tabstop=4 shiftwidth=4 expandtab: */
109
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
110