Symfony — Azure Provider for OAuth 2.0 Client bundle

Provides a tiny wrapper for using thenetworg/oauth2-azure inside Symfony.

Jose Clemente García Rodríguez
3 min readFeb 17, 2024

The Azure Active Directory Provider for OAuth 2.0 Client package serves as a great tool to streamline OAuth 2.0 integration in PHP applications.

Leveraging the capabilities of TheNetworg/oauth2-azure library, I have implemented a Symfony wrapper that offers a swift and straightforward solution for handling Azure logins. This wrapper provides a clear abstraction layer, simplifying the configuration and utilization of Azure authentication in Symfony applications.

IInstallation 💻

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

symfony composer require m4n50n/oauth2-azure-bundle

Configuration 📄

Configuration of the bundle is simplified using a dedicated YAML file. Here, you can specify the necessary authentication parameters:

# config/packages/oauth2_azure.yaml

o_auth2_azure:
# Required
clientId: "%env(AUTH_CLIEN_ID)%"
clientSecret: "%env(AUTH_CLIENT_PASS)%"
tenant: "%env(AUTH_TENANT)%"
redirectUri: "%env(AUTH_REDIRECT_URI)%"

# Optional
redirectToUrl: "%env(bool:AUTH_REDIRECT_TO_URL)%" # Activate redirect after authentication
redirectUrl: "%env(AUTH_REDIRECT_URL)%"

Symfony Integration ⚙

This bundle provides an intuitive and clean interface for OAuth 2.0 authentication in Symfony. You can integrate authentication into your controllers or services by injecting the OAuth2AzureFactory.

use M4n50n\OAuth2AzureBundle\Factory\OAuth2AzureFactory;

final class LoginController extends AbstractController
{
public function __construct(private OAuth2AzureFactory $OAuth2AzureFactory)
{
}

#[Route(path: '/login/azure', name: 'login_azure', methods: ['GET'])]
public function user_azureLoginRequest(JWTTokenManagerInterface $JWTManager, UserPasswordHasherInterface $userPasswordHasher)
{
try {
// ...

$auth = $this->OAuth2AzureFactory->getAuth($this->request);
$ownerData = $auth->getOwnerData();

/* It returns an array with the following structure:

$ownerData = [
"aud" => "c3db02f0-401c-452c......",
"iss" => "https://login.microsoftonline.com/....../v2.0",
"iat" => 1360114,
"profileImage" => "", // base64_encode of the image binary
"email":"josegarciarodriguez89@hotmail.com",
"name":"Jose Garcia",

// ... (other fields)
];
*/

// ...
} catch (\Exception $exception) {
// ...
}

// ...
}
}

Wrapper’s Utility ❗

Integration: This bundle encapsulates the intricacies of Azure OAuth 2.0, allowing you to seamlessly integrate authentication into Symfony applications without getting bogged down by the underlying complexities.

  1. Simplicity: The YAML configuration file simplifies the setup process, allowing developers to focus on the essential authentication details without navigating through extensive documentation.
  2. Security: Leveraging the proven TheNetworg/oauth2-azure library, this bundle ensures a secure authentication process, mitigating potential vulnerabilities and enhancing the overall security posture of Symfony applications.
  3. Best Practices: Built with Symfony best practices in mind, the bundle aligns with Symfony’s architecture, ensuring compatibility and adherence to established development standards.

Conclusion ✅

In conclusion, this bundle offers robust and user-friendly solution for integrating OAuth 2.0 authentication into Symfony projects.

For a more in-depth understanding and comprehensive documentation, feel free to explore the detailed information provided in the README documentation on GitHub.

https://github.com/m4n50n/oauth2_azure_bundle

https://packagist.org/packages/m4n50n/oauth2-azure-bundle

Feel free to contact me if you have any questions or proposals😄.

You can follow me on LinkedIn and GitHub.

Thank you very much for coming here.

Love My Work? Here is How to Support Me!

--

--