Installation:
composer require hwi/oauth-bundle
Enable the bundle in config/bundles.php:
return [
// ...
HWI\OAuthBundle\HWIOAuthBundle::class => ['all' => true],
];
Configure Providers (e.g., GitHub):
Add to config/packages/hwi_oauth.yaml:
hwi_oauth:
connect:
account_connect_form_class: App\Form\ConnectForm
account_connect_handler:
class: HWI\Bundle\OAuthBundle\Security\Core\Authentication\Handler\OAuthAuthenticationSuccessHandler
providers:
github:
skip_user_setup: true
client_id: "your_client_id"
client_secret: "your_client_secret"
scope: "repo,gist"
First Use Case:
/connect/github (or your provider).$user->getOAuthToken() or $user->getOAuthRawAccessToken().config/packages/hwi_oauth.yaml (or app/config/config.yml in older Symfony).security.yaml for firewall rules (e.g., anonymous: ~ → main).templates/HWIOAuthBundle/Connect/connect.html.twig (customize login/logout buttons).HWI\Bundle\OAuthBundle\Security\Core\User\OAuthAwareUserProvider if you need custom user mapping.hwi_oauth_connect route (e.g., /connect/github).OAuthAuthenticationSuccessHandler to redirect users or fetch additional data:
// src/Security/OAuthSuccessHandler.php
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
$user = $token->getUser();
// Fetch extra data (e.g., GitHub repos) and save to user entity.
return new RedirectResponse('/dashboard');
}
Register in security.yaml:
firewalls:
main:
oauth:
resource_owners:
github: "/login/github-check"
login_path: /login
use_forward: false
failure_path: /login
oauth_user_provider:
class: App\Security\OAuthUserProvider
Extend OAuthAwareUserProvider to map OAuth data to your user entity:
// src/Security/OAuthUserProvider.php
class OAuthUserProvider extends OAuthAwareUserProvider
{
public function loadUserByOAuthUserResponse(OAuthUserResponse $response, $providerKey)
{
$email = $response->getEmail();
$username = $response->getUsername();
$user = $this->userManager->findUserBy(['email' => $email]);
if (!$user) {
$user = $this->userManager->createUser([
'email' => $email,
'username' => $username,
'provider' => $providerKey,
'provider_id' => $response->getId(),
]);
}
return $user;
}
}
Configure multiple providers in hwi_oauth.yaml:
providers:
github:
client_id: "..."
client_secret: "..."
google:
client_id: "..."
client_secret: "..."
Use hwi_oauth_connect route with provider suffix (e.g., /connect/github, /connect/google).
For providers supporting it (e.g., Google), refresh tokens automatically:
hwi_oauth:
connect:
providers:
google:
options:
access_type: "offline"
approval_prompt: "force"
Extend OAuthUserResponse to handle refresh:
$token = $response->getAccessToken();
if ($token->isExpired()) {
$token->refresh();
}
HWI\Bundle\OAuthBundle\Form\Type\ConnectType for login forms.Guzzle or Symfony\Component\HttpClient with the OAuth token:
$client = new Client();
$response = $client->request('GET', 'https://api.github.com/user/repos', [
'auth' => [$user->getOAuthToken(), 'x-oauth-basic']
]);
/**
* @ORM\Column(type="string", nullable=true)
*/
private $provider;
/**
* @ORM\Column(type="string", nullable=true)
*/
private $provider_id;
/**
* @ORM\Column(type="json", nullable=true)
*/
private $oauth_data;
hwi_oauth_logout route or clear tokens manually:
$user->setOAuthToken(null);
$user->setOAuthRawAccessToken(null);
$this->entityManager->flush();
Token Storage:
OAuthUserResponse or use a custom user provider).getAccessToken() in your user provider to fetch from DB:
$token = $this->tokenRepository->findOneBy(['user' => $user, 'provider' => $providerKey]);
CSRF Issues:
client_id and client_secret are correctly set.skip_user_setup: false in config if using OAuth1.Provider-Specific Quirks:
default_graph_version (e.g., v13.0) in config.access_type: offline for refresh tokens.repo may require additional permissions.scope in config.User Provider Conflicts:
FOSUserBundle, ensure OAuthUserProvider is loaded after FOSUserBundle's provider.security.yaml:
providers:
fos_userbundle:
id: fos_user.user_provider.username_email
hwi_oauth:
id: hwi_oauth.user_provider
Debugging:
bin/console debug:config hwi_oauth to inspect config.Authorization Code flow vs. Implicit flow (deprecated).monolog to log OAuth responses:
monolog:
handlers:
main:
level: debug
dump() or dd() in custom handlers to inspect $response:
public function onAuthenticationSuccess(Request $request, TokenInterface $token)
{
$user = $token->getUser();
dump($user->getOAuthResponse()); // Inspect raw OAuth data
}
Custom Providers:
Add unsupported providers by extending HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface:
class CustomProviderResponse extends UserResponseInterface
{
public function getEmail()
{
return $this->response['email'];
}
}
Register in services.yaml:
services:
App\OAuth\CustomProviderResponse:
tags:
- { name: hwi_oauth.user_response.class, provider: "custom" }
Event Listeners:
Listen to hwi_oauth.connect.success or hwi_oauth.connect.fail:
// src/EventListener/OAuthListener.php
class OAuthListener implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'hwi_oauth.connect.success' => 'onConnectSuccess',
];
}
public function onConnectSuccess(ConnectEvent $event)
{
$user = $event->getUser();
How can I help you explore Laravel packages today?