-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
199 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Packeton\Attribute; | ||
|
||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
class AsIntegration | ||
{ | ||
public function __construct(public readonly string $nameOrClass) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Packeton\Integrations\Factory; | ||
|
||
use Symfony\Component\DependencyInjection\Attribute\Exclude; | ||
|
||
#[Exclude] | ||
class OAuth2Factory implements OAuth2FactoryInterface | ||
{ | ||
use OAuth2FactoryTrait; | ||
|
||
public function __construct(private readonly string $key, private readonly string $class) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Packeton\Integrations\Google; | ||
|
||
use Okvpn\Expression\TwigLanguage; | ||
use Packeton\Attribute\AsIntegration; | ||
use Packeton\Integrations\Base\BaseIntegrationTrait; | ||
use Packeton\Integrations\LoginInterface; | ||
use Packeton\Integrations\Model\OAuth2State; | ||
use Psr\Log\LoggerInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface as UG; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
#[AsIntegration('google')] | ||
class GoogleOAuth2Login implements LoginInterface | ||
{ | ||
use BaseIntegrationTrait; | ||
|
||
protected $name; | ||
protected $separator = ' '; | ||
protected $defaultScopes = ['openid', 'email']; | ||
|
||
public function __construct( | ||
protected array $config, | ||
protected HttpClientInterface $httpClient, | ||
protected RouterInterface $router, | ||
protected OAuth2State $state, | ||
protected TwigLanguage $twigLanguage, | ||
protected LoggerInterface $logger, | ||
) { | ||
$this->name = $config['name']; | ||
|
||
if (empty($this->config['default_roles'])) { | ||
$this->config['default_roles'] = ['ROLE_MAINTAINER', 'ROLE_SSO_GOOGLE']; | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function redirectOAuth2Url(Request $request = null, array $options = []): Response | ||
{ | ||
return $this->getAuthorizationResponse('https://accounts.google.com/o/oauth2/v2/auth', $options); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getAccessToken(Request $request, array $options = []): array | ||
{ | ||
if (!$request->get('code') || !$this->checkState($request->get('state'))) { | ||
throw new BadRequestHttpException('No "code" and "state" parameter was found (usually this is a query parameter)!'); | ||
} | ||
|
||
$route = $this->state->getStateBag()->get('route'); | ||
$redirectUrl = $this->router->generate($route, ['alias' => $this->name], UG::ABSOLUTE_URL); | ||
$query = [ | ||
'client_id' => $this->config['client_id'], | ||
'client_secret' => $this->config['client_secret'], | ||
'code' => $request->get('code'), | ||
'grant_type' => 'authorization_code', | ||
'redirect_uri' => $redirectUrl, | ||
]; | ||
|
||
$response = $this->httpClient->request('POST', 'https://oauth2.googleapis.com/token', ['body' => $query]); | ||
|
||
return $response->toArray(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function fetchUser(array|Request $request, array $options = [], array &$accessToken = null): array | ||
{ | ||
$accessToken ??= $request instanceof Request ? $this->getAccessToken($request) : $request; | ||
|
||
$response = $this->httpClient->request('GET', 'https://openidconnect.googleapis.com/v1/userinfo', $this->getAuthorizationHeaders($accessToken)); | ||
|
||
$response = $response->toArray(); | ||
if (false === ($response['email_verified'] ?? null)) { | ||
throw new BadRequestHttpException('Google email_verified is false!'); | ||
} | ||
|
||
$response['user_name'] = explode('@', $response['email'])[0]; | ||
$response['user_identifier'] = $response['email']; | ||
$response['external_id'] = isset($response['sub']) ? $this->name . ':' . $response['sub'] : null; | ||
|
||
return $response; | ||
} | ||
|
||
protected function getAuthorizationHeaders(array $token): array | ||
{ | ||
return array_merge_recursive($this->config['http_options'] ?? [], [ | ||
'headers' => [ | ||
'Authorization' => "Bearer {$token['access_token']}", | ||
] | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<svg xmlns="http://www.w3.org/2000/svg" height="1em" viewBox="0 0 488 512"><!--! Font Awesome Free 6.4.2 by @fontawesome - https://fontawesome.com License - https://fontawesome.com/license (Commercial License) Copyright 2023 Fonticons, Inc. --><path d="M488 261.8C488 403.3 391.1 504 248 504 110.8 504 0 393.2 0 256S110.8 8 248 8c66.8 0 123 24.5 166.3 64.9l-67.5 64.9C258.5 52.6 94.3 116.6 94.3 256c0 86.5 69.1 156.6 153.7 156.6 98.2 0 135-70.4 140.8-106.9H248v-85.3h236.1c2.3 12.7 3.9 24.9 3.9 41.4z"/></svg> |