21
21
class GoogleAuthorizer extends Authorizer
22
22
{
23
23
use CredentialFieldsCheckTrait;
24
-
25
- /**
26
- * @var RepositoryInterface
27
- */
28
- private $ user_repository ;
29
-
30
- /**
31
- * @var Google_Client
32
- */
33
- private $ google_client ;
34
-
35
- /**
36
- * @var string
37
- */
38
- private $ client_id ;
39
-
40
- /**
41
- * @var array
42
- */
43
- private $ user_profile ;
24
+ private RepositoryInterface $ user_repository ;
25
+ private Google_Client $ google_client ;
26
+ private string $ client_id ;
27
+ private array $ user_profile ;
44
28
45
29
/**
46
30
* GoogleAuthorizer constructor.
47
31
*
48
- * @param RepositoryInterface $user_repository
49
- * @param Google_Client $google_client
50
- * @param $client_id
32
+ * @param RepositoryInterface $user_repository
33
+ * @param Google_Client $google_client
34
+ * @param string $client_id
51
35
* @param ExceptionHandlerInterface|null $exception_handler
52
36
*/
53
- public function __construct (RepositoryInterface $ user_repository , Google_Client $ google_client , $ client_id , ExceptionHandlerInterface $ exception_handler = null )
54
- {
37
+ public function __construct (
38
+ RepositoryInterface $ user_repository ,
39
+ Google_Client $ google_client ,
40
+ string $ client_id ,
41
+ ExceptionHandlerInterface $ exception_handler = null
42
+ ) {
55
43
$ this ->user_repository = $ user_repository ;
56
44
$ this ->google_client = $ google_client ;
57
45
$ this ->client_id = $ client_id ;
@@ -65,16 +53,15 @@ public function __construct(RepositoryInterface $user_repository, Google_Client
65
53
*
66
54
* {@inheritdoc}
67
55
*/
68
- public function verifyCredentials (array $ credentials )
56
+ public function verifyCredentials (array $ credentials ): ? AuthenticatedUserInterface
69
57
{
70
- $ this ->verifyRequiredFields ($ credentials , ['token ' , 'username ' ]);
71
-
58
+ $ this ->verifyRequiredFields ($ credentials , ['token ' ]);
72
59
$ token = $ credentials ['token ' ];
73
- $ username = $ credentials ['username ' ];
74
60
75
61
$ payload = $ this ->google_client ->verifyIdToken ($ token );
62
+ $ username = $ payload ['email ' ] ?? $ credentials ['username ' ];
76
63
77
- $ this ->verifyGoogleProfile ($ payload , $ username );
64
+ $ this ->verifyGoogleProfile ($ payload , $ credentials [ ' username ' ] ?? null );
78
65
$ this ->user_profile = $ payload ;
79
66
80
67
$ user = $ this ->user_repository ->findByUsername ($ username );
@@ -86,16 +73,16 @@ public function verifyCredentials(array $credentials)
86
73
/**
87
74
* @return array
88
75
*/
89
- public function getUserProfile ()
76
+ public function getUserProfile (): array
90
77
{
91
78
return $ this ->user_profile ;
92
79
}
93
80
94
81
/**
95
- * @param array $payload
96
- * @param string $username
82
+ * @param array $payload
83
+ * @param string|null $username
97
84
*/
98
- private function verifyGoogleProfile (array $ payload , $ username )
85
+ private function verifyGoogleProfile (array $ payload , ? string $ username ): void
99
86
{
100
87
if ($ this ->client_id !== $ payload ['aud ' ]) {
101
88
throw new RuntimeException ('Unrecognized google_client ' );
@@ -105,15 +92,15 @@ private function verifyGoogleProfile(array $payload, $username)
105
92
throw new RuntimeException ('Wrong issuer ' );
106
93
}
107
94
108
- if ($ username !== $ payload ['email ' ]) {
95
+ if ($ username && $ username !== $ payload ['email ' ]) {
109
96
throw new RuntimeException ('Email is not verified by Google ' );
110
97
}
111
98
}
112
99
113
100
/**
114
101
* @param AuthenticatedUserInterface|null $user
115
102
*/
116
- private function verifyUser (AuthenticatedUserInterface $ user = null )
103
+ private function verifyUser (AuthenticatedUserInterface $ user = null ): void
117
104
{
118
105
if (!$ user || !$ user ->canAuthenticate ()) {
119
106
throw new UserNotFoundException ();
@@ -123,7 +110,7 @@ private function verifyUser(AuthenticatedUserInterface $user = null)
123
110
/**
124
111
* @return array
125
112
*/
126
- private function getDomains ()
113
+ private function getDomains (): array
127
114
{
128
115
return ['accounts.google.com ' , 'https://accounts.google.com ' ];
129
116
}
0 commit comments