Skip to content

Commit

Permalink
feat: add google auth login
Browse files Browse the repository at this point in the history
  • Loading branch information
MickaelCa committed Mar 11, 2024
1 parent 34e223a commit f05789f
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 89 deletions.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ OAUTH_BITBUCKET_CLIENT_ID=
OAUTH_BITBUCKET_CLIENT_SECRET=
OAUTH_BUDDY_CLIENT_ID=
OAUTH_BUDDY_CLIENT_SECRET=
OAUTH_GOOGLE_CLIENT_ID=
OAUTH_GOOGLE_CLIENT_SECRET=
###< oauth ###

###> google analytics ###
Expand Down
2 changes: 2 additions & 0 deletions .env.docker
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ OAUTH_BITBUCKET_CLIENT_ID=
OAUTH_BITBUCKET_CLIENT_SECRET=
OAUTH_BUDDY_CLIENT_ID=
OAUTH_BUDDY_CLIENT_SECRET=
OAUTH_GOOGLE_CLIENT_ID=
OAUTH_GOOGLE_CLIENT_SECRET=
###< oauth ###

###> google analytics ###
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"league/flysystem-bundle": "^1.5",
"league/flysystem-cached-adapter": "^1.1",
"league/oauth2-github": "^3.0",
"league/oauth2-google": "^4.0",
"m4tthumphrey/php-gitlab-api": "^11.0",
"munusphp/munus": "^0.4.0",
"nelmio/api-doc-bundle": "^4.3",
Expand Down
151 changes: 62 additions & 89 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions config/packages/knpu_oauth2_client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ knpu_oauth2_client:
redirect_route: register_buddy_check
redirect_params: {}
use_state: true
google:
type: google
client_id: '%env(OAUTH_GOOGLE_CLIENT_ID)%'
client_secret: '%env(OAUTH_GOOGLE_CLIENT_SECRET)%'
redirect_route: register_google_check
redirect_params: {}
1 change: 1 addition & 0 deletions config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ security:
- Buddy\Repman\Security\GitLabAuthenticator
- Buddy\Repman\Security\BitbucketAuthenticator
- Buddy\Repman\Security\BuddyAuthenticator
- Buddy\Repman\Security\GoogleAuthenticator
entry_point: Buddy\Repman\Security\LoginFormAuthenticator
logout:
path: app_logout
Expand Down
4 changes: 4 additions & 0 deletions config/routes/annotations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@ login_bitbucket_check:
login_buddy_check:
path: /auth/buddy/check
schemes: ['%url_scheme%']

login_google_check:
path: /auth/google/check
schemes: ['%url_scheme%']
1 change: 1 addition & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ services:
gitlab: '%env(OAUTH_GITLAB_CLIENT_ID)%'
bitbucket: '%env(OAUTH_BITBUCKET_CLIENT_ID)%'
buddy: '%env(OAUTH_BUDDY_CLIENT_ID)%'
google: '%env(OAUTH_GOOGLE_CLIENT_ID)%'

Buddy\Repman\Service\Security\SecurityChecker\SensioLabsSecurityChecker:
arguments:
Expand Down
57 changes: 57 additions & 0 deletions src/Controller/OAuth/GoogleController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace Buddy\Repman\Controller\OAuth;

use KnpU\OAuth2ClientBundle\Client\Provider\GoogleClient;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

final class GoogleController extends OAuthController
{
/**
* @Route("/register/google", name="register_google_start", methods={"GET"})
*/
public function register(): Response
{
$this->ensureOAuthRegistrationIsEnabled();

return $this->oauth->getClient('google')->redirect([
'openid',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
], []);
}

/**
* @Route("/auth/google", name="auth_google_start", methods={"GET"})
*/
public function auth(): Response
{
return $this->oauth
->getClient('google')
->redirect([
'openid',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
], ['redirect_uri' => $this->generateUrl('login_google_check', [], UrlGeneratorInterface::ABSOLUTE_URL)])
;
}

/**
* @Route("/register/google/check", name="register_google_check", methods={"GET"})
*/
public function registerCheck(Request $request, GoogleClient $api): Response
{
$this->ensureOAuthRegistrationIsEnabled();

return $this->createAndAuthenticateUser(
'google',
fn () => $api->fetchUserFromToken($this->oauth->getClient('google')->getAccessToken()->getToken()),
$request
);
}
}
42 changes: 42 additions & 0 deletions src/Security/GoogleAuthenticator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Buddy\Repman\Security;

use KnpU\OAuth2ClientBundle\Client\ClientRegistry;
use KnpU\OAuth2ClientBundle\Client\Provider\GoogleClient;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\Badge\UserBadge;
use Symfony\Component\Security\Http\Authenticator\Passport\PassportInterface;
use Symfony\Component\Security\Http\Authenticator\Passport\SelfValidatingPassport;

final class GoogleAuthenticator extends OAuthAuthenticator
{
private GoogleClient $googleClient;

public function __construct(ClientRegistry $clientRegistry, GoogleClient $googleClient, RouterInterface $router, UserProvider $userProvider)
{
$this->clientRegistry = $clientRegistry;
$this->googleClient = $googleClient;
$this->userProvider = $userProvider;
$this->router = $router;
}

public function supports(Request $request): bool
{
return $request->attributes->get('_route') === 'login_google_check';
}

public function authenticate(Request $request): PassportInterface
{
$email = $this->googleClient->fetchUserFromToken($this->fetchAccessToken($this->clientRegistry->getClient('google'), $request->attributes->get('_route')))->getEmail();
$user = $this->userProvider->loadUserByIdentifier($email);

return new SelfValidatingPassport(new UserBadge($email, function () use ($user): UserInterface {
return $user;
}));
}
}
3 changes: 3 additions & 0 deletions symfony.lock
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@
"league/oauth2-github": {
"version": "2.0.0"
},
"league/oauth2-google": {
"version": "4.0.0"
},
"m4tthumphrey/php-gitlab-api": {
"version": "9.17.0"
},
Expand Down
6 changes: 6 additions & 0 deletions templates/security/login.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
Buddy
</a>
{% endif %}

{% if oauth_enabled('google') %}
<a href="{{ url('auth_google_start') }}" class="btn btn-github btn-sm">
{% include 'svg/google-icon.svg' %} Google
</a>
{% endif %}
</div>

<hr />
Expand Down
1 change: 1 addition & 0 deletions templates/svg/google-icon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit f05789f

Please sign in to comment.