Issues (6)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

Service/ClientProvider.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Happyr\GoogleSiteAuthenticatorBundle\Service;
4
5
use Happyr\GoogleSiteAuthenticatorBundle\Model\AccessToken;
6
use Happyr\GoogleSiteAuthenticatorBundle\Model\TokenConfig;
7
use Psr\Cache\CacheItemPoolInterface;
8
9
/**
10
 * @author Tobias Nyholm <[email protected]>
11
 */
12
class ClientProvider
13
{
14
    /**
15
     * @var TokenConfig config
16
     */
17
    private $config;
18
19
    /**
20
     * @var CacheItemPoolInterface storage
21
     */
22
    private $pool;
23
24
    public function __construct(TokenConfig $config, CacheItemPoolInterface $pool)
25
    {
26
        $this->config = $config;
27
        $this->pool = $pool;
28
    }
29
30
    public function getClient(string $tokenName = null): \Google_Client
31
    {
32
        $client = new \Google_Client();
33
34
        // Set values from configuration
35
        $client->setApplicationName($this->config->getApplicationName());
36
        $client->setClientId($this->config->getClientId($tokenName));
37
        $client->setClientSecret($this->config->getSecret($tokenName));
38
        $client->setRedirectUri($this->config->getRedirectUrl($tokenName));
39
        $client->setScopes($this->config->getScopes($tokenName));
40
41
        if (null !== $accessToken = $this->getAccessToken($tokenName)) {
42
            // set access token to client if we got one
43
            $client->setAccessToken((string) $accessToken);
44
45
            // make sure to refresh the stored access token
46
            $this->refreshToken($client);
47
        }
48
49
        return $client;
50
    }
51
52
    /**
53
     * Check if a token is valid.
54
     * This is an expensive operation that makes multiple API calls.
55
     */
56
    public function isTokenValid(string $tokenName = null): bool
57
    {
58
        // We must fetch the client here. A client will automatically refresh the stored access token.
59
        $client = $this->getClient($tokenName);
60
        if (null === $accessToken = $client->getAccessToken()) {
61
            return false;
62
        }
63
64
        // Get the token string from access token
65
        $token = \json_decode($accessToken)->access_token;
66
        $url = \sprintf('https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=%s', $token);
67
        if (false === @\file_get_contents($url)) {
68
            return false;
69
        }
70
71
        // Retrieve HTTP status code
72
        list($version, $statusCode, $msg) = \explode(' ', $http_response_header[0], 3);
0 ignored issues
show
The assignment to $version is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
The assignment to $msg is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
73
74
        return 200 === (int) $statusCode;
75
    }
76
77
    /**
78
     * Store the access token in the storage.
79
     */
80
    public function setAccessToken(?string $accessToken, string $tokenName = null)
81
    {
82
        $name = $this->config->getKey($tokenName);
83
        $cacheKey = $this->creteCacheKey($name);
84
85
        if (null === $accessToken) {
86
            $this->pool->deleteItem($cacheKey);
87
88
            return;
89
        }
90
91
        $item = $this->pool->getItem($cacheKey)->set(new AccessToken($name, $accessToken));
92
        $this->pool->save($item);
93
    }
94
95
    /**
96
     * Get access token from storage.
97
     */
98
    protected function getAccessToken(string $tokenName = null): ?AccessToken
99
    {
100
        $cacheKey = $this->creteCacheKey($this->config->getKey($tokenName));
101
        $item = $this->pool->getItem($cacheKey);
102
103
        if (!$item->isHit()) {
104
            return null;
105
        }
106
107
        return $item->get();
108
    }
109
110
    /**
111
     * If we got a refresh token, use it to retrieve a good access token.
112
     */
113
    private function refreshToken(\Google_Client $client): bool
114
    {
115
        $accessToken = $client->getAccessToken();
116
        $data = \json_decode($accessToken, true);
117
118
        try {
119
            if (isset($data['refresh_token'])) {
120
                $client->refreshToken($data['refresh_token']);
121
122
                return true;
123
            }
124
        } catch (\Google_Auth_Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
125
        }
126
127
        return false;
128
    }
129
130
    private function creteCacheKey(string $name): string
131
    {
132
        return \sha1('happyr_google-site-authenticator_'.$name);
133
    }
134
}
135