Skip to content
This repository was archived by the owner on Aug 17, 2022. It is now read-only.

Commit 0636ef2

Browse files
committed
added scalar type hints; minor fixes
1 parent 2077b9f commit 0636ef2

19 files changed

+112
-181
lines changed

AuthenticationMethod/AbstractAuthenticationMethod.php

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
declare(strict_types=1);
4+
45
/*
56
* This file is part of the Zikula package.
67
*
@@ -12,9 +13,11 @@
1213

1314
namespace Zikula\OAuthModule\AuthenticationMethod;
1415

16+
use Exception;
1517
use League\OAuth2\Client\Provider\AbstractProvider;
1618
use League\OAuth2\Client\Provider\ResourceOwnerInterface;
1719
use League\OAuth2\Client\Token\AccessToken;
20+
use LogicException;
1821
use Symfony\Component\HttpFoundation\RequestStack;
1922
use Symfony\Component\HttpFoundation\Session\Session;
2023
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
@@ -72,15 +75,6 @@ abstract class AbstractAuthenticationMethod implements ReEntrantAuthenticationMe
7275
*/
7376
protected $provider;
7477

75-
/**
76-
* AbstractAuthenticationMethod constructor.
77-
*
78-
* @param TranslatorInterface $translator
79-
* @param RequestStack $requestStack
80-
* @param RouterInterface $router
81-
* @param MappingRepository $repository
82-
* @param VariableApi $variableApi
83-
*/
8478
public function __construct(
8579
TranslatorInterface $translator,
8680
RequestStack $requestStack,
@@ -96,23 +90,14 @@ public function __construct(
9690
$this->variableApi = $variableApi;
9791
}
9892

99-
/**
100-
* @param string $redirectUri
101-
*/
102-
abstract protected function setProvider($redirectUri);
93+
abstract protected function setProvider(string $redirectUri): void;
10394

104-
/**
105-
* @return AbstractProvider
106-
*/
107-
public function getProvider()
95+
public function getProvider(): AbstractProvider
10896
{
10997
return $this->provider;
11098
}
11199

112-
/**
113-
* @return array
114-
*/
115-
protected function getAuthorizationUrlOptions()
100+
protected function getAuthorizationUrlOptions(): array
116101
{
117102
return [];
118103
}
@@ -121,22 +106,17 @@ protected function getAuthorizationUrlOptions()
121106
* Method called during `authenticate` method after token is set.
122107
* Allows author to take actions which require the token.
123108
*/
124-
protected function setAdditionalUserData()
109+
protected function setAdditionalUserData(): void
125110
{
126111
}
127112

128-
/**
129-
* Authenticate the user to the provider.
130-
* @param array $data
131-
* @return integer|null if Zikula Uid is set for provider ID, this is returned, else null
132-
*/
133-
public function authenticate(array $data = [])
113+
public function authenticate(array $data = []): ?int
134114
{
135115
$redirectUri = $data['redirectUri'] ?? $this->router->generate('zikulausersmodule_access_login', [], UrlGeneratorInterface::ABSOLUTE_URL);
136116
$this->setProvider($redirectUri);
137117
$request = $this->requestStack->getCurrentRequest();
138-
$state = $request->query->get('state', null);
139-
$code = $request->query->get('code', null);
118+
$state = null !== $request ? $request->query->get('state') : null;
119+
$code = null !== $request ? $request->query->get('code') : null;
140120

141121
if (!isset($code)) {
142122
// If no authorization code then get one
@@ -145,18 +125,20 @@ public function authenticate(array $data = [])
145125

146126
header('Location: ' . $authUrl);
147127
exit;
128+
}
148129

149130
// Check given state against previously stored one to mitigate CSRF attack
150-
} elseif (empty($state) || ($state !== $this->session->get('oauth2state'))) {
131+
if (empty($state) || $state !== $this->session->get('oauth2state')) {
151132
$this->session->remove('oauth2state');
152133
$this->session->getFlashBag()->add('error', 'Invalid State');
153134

154135
return null;
155136
}
137+
156138
// Try to get an access token (using the authorization code grant)
157139
$this->token = $this->getProvider()->getAccessToken('authorization_code', [
158-
'code' => $code
159-
]);
140+
'code' => $code
141+
]);
160142

161143
try {
162144
// get the user's details
@@ -169,34 +151,31 @@ public function authenticate(array $data = [])
169151
$registrationUrl = $this->router->generate('zikulausersmodule_registration_register');
170152
$this->session->remove('oauth2state');
171153
$registerLink = '<a href="' . $registrationUrl . '">' . $this->translator->__('create a new account') . '</a>';
172-
$this->session->getFlashBag()->add('error',
173-
$this->translator->__f(
174-
'This user is not locally registered. You must first %registerLink on this site before logging in with %displayName', [
175-
'%registerLink' => $registerLink,
176-
'%displayName' => $this->getDisplayName()
177-
]
178-
)
179-
);
154+
$errorMessage = $this->translator->__f('This user is not locally registered. You must first %registerLink on this site before logging in with %displayName', [
155+
'%registerLink' => $registerLink,
156+
'%displayName' => $this->getDisplayName()
157+
]);
158+
$this->session->getFlashBag()->add('error', $errorMessage);
180159
}
181160

182161
return $uid;
183-
} catch (\Exception $exception) {
162+
} catch (Exception $exception) {
184163
$this->session->getFlashBag()->add('error', $this->translator->__('Could not obtain user details from external service.') . ' (' . $exception->getMessage() . ')');
185164

186165
return null;
187166
}
188167
}
189168

190-
public function getId()
169+
public function getId(): string
191170
{
192171
if (!$this->user) {
193-
throw new \LogicException($this->translator->__('User must authenticate first.'));
172+
throw new LogicException($this->translator->__('User must authenticate first.'));
194173
}
195174

196175
return $this->user->getId();
197176
}
198177

199-
public function register(array $data)
178+
public function register(array $data = []): bool
200179
{
201180
$mapping = new MappingEntity();
202181
$mapping->setMethod($this->getAlias());

AuthenticationMethod/FacebookAuthenticationMethod.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
declare(strict_types=1);
4+
45
/*
56
* This file is part of the Zikula package.
67
*
@@ -18,35 +19,35 @@
1819

1920
class FacebookAuthenticationMethod extends AbstractAuthenticationMethod
2021
{
21-
public function getAlias()
22+
public function getAlias(): string
2223
{
2324
return OAuthConstant::ALIAS_FACEBOOK;
2425
}
2526

26-
public function getDisplayName()
27+
public function getDisplayName(): string
2728
{
2829
return $this->translator->__('Facebook');
2930
}
3031

31-
public function getDescription()
32+
public function getDescription(): string
3233
{
3334
return $this->translator->__('Login using Facebook via OAuth.');
3435
}
3536

36-
public function getUname()
37+
public function getUname(): string
3738
{
3839
return $this->user->getName();
3940
}
4041

41-
public function getEmail()
42+
public function getEmail(): string
4243
{
4344
return $this->user->getEmail();
4445
}
4546

46-
protected function setProvider($redirectUri)
47+
protected function setProvider(string $redirectUri): void
4748
{
4849
$settings = $this->variableApi->get('ZikulaOAuthModule', OAuthConstant::ALIAS_FACEBOOK);
49-
if (!isset($settings['id']) || !isset($settings['secret'])) {
50+
if (!isset($settings['id'], $settings['secret'])) {
5051
throw new InvalidProviderConfigException($this->translator->__('Invalid settings for Facebook OAuth provider.'));
5152
}
5253

AuthenticationMethod/GithubAuthenticationMethod.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,40 +23,40 @@ class GithubAuthenticationMethod extends AbstractAuthenticationMethod
2323
*/
2424
private $email;
2525

26-
public function getAlias()
26+
public function getAlias(): string
2727
{
2828
return OAuthConstant::ALIAS_GITHUB;
2929
}
3030

31-
public function getDisplayName()
31+
public function getDisplayName(): string
3232
{
3333
return $this->translator->__('Github');
3434
}
3535

36-
public function getDescription()
36+
public function getDescription(): string
3737
{
3838
return $this->translator->__('Login using Github via OAuth.');
3939
}
4040

41-
public function getUname()
41+
public function getUname(): string
4242
{
4343
return $this->user->getNickname();
4444
}
4545

46-
public function getEmail()
46+
public function getEmail(): string
4747
{
4848
return $this->email;
4949
}
5050

51-
protected function getAuthorizationUrlOptions()
51+
protected function getAuthorizationUrlOptions(): array
5252
{
5353
return [
5454
'state' => 'OPTIONAL_CUSTOM_CONFIGURED_STATE',
5555
'scope' => ['user:email']
5656
];
5757
}
5858

59-
protected function setAdditionalUserData()
59+
protected function setAdditionalUserData(): void
6060
{
6161
// this method is needed to get the user's email because github doesn't provide it as a standard
6262
// part of the data that is returned as a 'user'.
@@ -74,10 +74,10 @@ protected function setAdditionalUserData()
7474
}
7575
}
7676

77-
protected function setProvider($redirectUri)
77+
protected function setProvider(string $redirectUri): void
7878
{
7979
$settings = $this->variableApi->get('ZikulaOAuthModule', OAuthConstant::ALIAS_GITHUB);
80-
if (!isset($settings['id']) || !isset($settings['secret'])) {
80+
if (!isset($settings['id'], $settings['secret'])) {
8181
throw new InvalidProviderConfigException($this->translator->__('Invalid settings for Github OAuth provider.'));
8282
}
8383

AuthenticationMethod/GoogleAuthenticationMethod.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,35 @@
1818

1919
class GoogleAuthenticationMethod extends AbstractAuthenticationMethod
2020
{
21-
public function getAlias()
21+
public function getAlias(): string
2222
{
2323
return OAuthConstant::ALIAS_GOOGLE;
2424
}
2525

26-
public function getDisplayName()
26+
public function getDisplayName(): string
2727
{
2828
return $this->translator->__('Google');
2929
}
3030

31-
public function getDescription()
31+
public function getDescription(): string
3232
{
3333
return $this->translator->__('Login using Google via OAuth.');
3434
}
3535

36-
public function getUname()
36+
public function getUname(): string
3737
{
3838
return $this->user->getName();
3939
}
4040

41-
public function getEmail()
41+
public function getEmail(): string
4242
{
4343
return $this->user->getEmail();
4444
}
4545

46-
protected function setProvider($redirectUri)
46+
protected function setProvider(string $redirectUri): void
4747
{
4848
$settings = $this->variableApi->get('ZikulaOAuthModule', OAuthConstant::ALIAS_GOOGLE);
49-
if (!isset($settings['id']) || !isset($settings['secret'])) {
49+
if (!isset($settings['id'], $settings['secret'])) {
5050
throw new InvalidProviderConfigException($this->translator->__('Invalid settings for Google OAuth provider.'));
5151
}
5252

AuthenticationMethod/LinkedInAuthenticationMethod.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,35 @@
1818

1919
class LinkedInAuthenticationMethod extends AbstractAuthenticationMethod
2020
{
21-
public function getAlias()
21+
public function getAlias(): string
2222
{
2323
return OAuthConstant::ALIAS_LINKEDIN;
2424
}
2525

26-
public function getDisplayName()
26+
public function getDisplayName(): string
2727
{
2828
return $this->translator->__('LinkedIn');
2929
}
3030

31-
public function getDescription()
31+
public function getDescription(): string
3232
{
3333
return $this->translator->__('Login using LinkedIn via OAuth.');
3434
}
3535

36-
public function getUname()
36+
public function getUname(): string
3737
{
3838
return $this->user->getFirstname() . ' ' . $this->user->getLastName();
3939
}
4040

41-
public function getEmail()
41+
public function getEmail(): string
4242
{
4343
return $this->user->getEmail();
4444
}
4545

46-
protected function setProvider($redirectUri)
46+
protected function setProvider(string $redirectUri): void
4747
{
4848
$settings = $this->variableApi->get('ZikulaOAuthModule', OAuthConstant::ALIAS_LINKEDIN);
49-
if (!isset($settings['id']) || !isset($settings['secret'])) {
49+
if (!isset($settings['id'], $settings['secret'])) {
5050
throw new InvalidProviderConfigException($this->translator->__('Invalid settings for LinkedIn OAuth provider.'));
5151
}
5252

0 commit comments

Comments
 (0)