Passed
Push — developer ( 4e3135...f5c82a )
by Radosław
30:25 queued 12:59
created

MSAzure::getClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 13
rs 9.9666
c 1
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * Basic Azure OAuth provider - file.
4
 *
5
 * @copyright YetiForce S.A.
6
 * @license YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
7
 * @author Radosław Skrzypczak <[email protected]>
8
 */
9
10
namespace App\Integrations\OAuth;
11
12
use TheNetworg\OAuth2\Client\Provider\Azure;
0 ignored issues
show
Bug introduced by
The type TheNetworg\OAuth2\Client\Provider\Azure was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
14
/**
15
 * Basic Azure OAuth provider - class.
16
 */
17
class MSAzure extends AbstractProvider
18
{
19
	/**
20
	 * OAuth provider label.
21
	 *
22
	 * @var string
23
	 */
24
	protected $label = 'MS Azure - Outlook.com (Office 365)';
25
26
	/** @var string Icon for authorization button */
27
	protected $icon = 'fab fa-microsoft';
28
29
	/**
30
	 * List of scopes that will be used for authentication.
31
	 *
32
	 * @var array
33
	 *
34
	 * @see https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-permissions-and-consent
35
	 */
36
	protected $scopes;
37
	protected $scopesForAction = ['MailAccount' => ['openid', 'https://outlook.office.com/IMAP.AccessAsUser.All', 'offline_access', 'email',  'https://outlook.office.com/SMTP.Send']];
38
39
	/**
40
	 * @var string If set, this will be sent to google as the "access_type" parameter.
41
	 *
42
	 * @see https://developers.google.com/identity/protocols/OpenIDConnect#authenticationuriparameters
43
	 */
44
	protected $accessType = 'offline';
45
46
	/**
47
	 * @var string The client ID string that you obtain from the API Console.
48
	 *
49
	 * @see https://developers.google.com/identity/protocols/oauth2/openid-connect#getcredentials
50
	 */
51
	protected $clientId;
52
53
	/** @var string Secret known only to the application and the authorization server */
54
	protected $clientSecret;
55
56
	/**
57
	 * Determines where the response is sent.
58
	 * The value of this parameter must exactly match one of the authorized redirect values that you set in the API Console.
59
	 *
60
	 * @var string
61
	 */
62
	protected $redirectUri;
63
	/**
64
	 * Undocumented variable.
65
	 *
66
	 * @var [type]
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
67
	 */
68
	private $client;
69
70
	protected $refreshToken;
71
	protected $accessToken;
72
	protected $expireTime;
73
74
	public function getClient(array $options = [])
75
	{
76
		if (!$this->client) {
77
			$options = array_merge(['clientId' => $this->clientId,
78
				'clientSecret' => $this->clientSecret,
79
				'redirectUri' => $this->getRedirectUri(),
80
				'accessType' => $this->accessType,
81
				'scopes' => $this->scopes,
82
				'defaultEndPointVersion' => \TheNetworg\OAuth2\Client\Provider\Azure::ENDPOINT_VERSION_2_0], $options);
83
			$this->client = new \TheNetworg\OAuth2\Client\Provider\Azure($options);
84
		}
85
86
		return $this->client;
87
	}
88
89
	public function getRedirectUri(): string
90
	{
91
		return $this->redirectUri;
92
	}
93
94
	/**
95
	 * Requests an access token using a specified grant and option set.
96
	 *
97
	 * @param mixed $grant
98
	 * @param array $options
99
	 *
100
	 * @return string
101
	 */
102
	public function getAccessToken($grant, array $options = [])
103
	{
104
		try {
105
			$token = $this->getClient()->getAccessToken($grant, $options);
106
			$this->accessToken = $token->getToken();
107
			$this->expireTime = $token->getExpires();
108
			if ($token->getRefreshToken()) {
109
				$this->refreshToken = $token->getRefreshToken();
110
			}
111
			$this->token = $token;
0 ignored issues
show
Bug Best Practice introduced by
The property token does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
112
		} catch (\Throwable $th) {
113
			\App\Log::error($th->getMessage());
114
			throw $th;
115
		}
116
117
		return $token->getToken();
118
	}
119
120
	public function refreshToken()
121
	{
122
		$grant = new \League\OAuth2\Client\Grant\RefreshToken();
0 ignored issues
show
Bug introduced by
The type League\OAuth2\Client\Grant\RefreshToken was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
123
		$this->getAccessToken($grant, ['refresh_token' => $this->getRefreshToken()]);
124
125
		return $this;
126
	}
127
}
128