m4n50n/oauth2-azure-bundle

此包提供了一个用于在Symfony中调用thenetworg/oauth2-azure的微小包装器。

安装: 187

依赖: 0

建议者: 0

安全: 0

星星: 2

关注者: 1

分支: 2

开放问题: 0

类型:symfony-bundle

0.1 2024-02-17 11:45 UTC

This package is auto-updated.

Last update: 2024-09-23 20:18:01 UTC


README

Latest Stable Version License Total Downloads

此Symfony包作为Azure Active Directory Provider for OAuth 2.0 Client的一个微小包装器。您可以在官方仓库中找到更多文档。

安装

打开命令行,进入您的项目目录,并执行以下命令以下载此包的最新稳定版本

symfony composer require m4n50n/oauth2-azure-bundle

启用包

通过将其添加到项目config/bundles.php文件中注册的包列表中来启用该包。

// config/bundles.php

return [
    // ...    
    M4n50n\OAuth2AzureBundle\OAuth2AzureBundle::class => ['all' => true],
];

配置包

config/packages/oauth2_azure.yaml文件中配置该包

# config/packages/oauth2_azure.yaml

o_auth2_azure:
  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)%" # URL to redirect after authentication
# .env

AUTH_CLIEN_ID="c3db02f0-401c-452c......"
AUTH_CLIENT_PASS="LfR8Q~yTXB5ozRejLrqE6oYqp......"
AUTH_TENANT="5fa120f8-1ee1-49e3-9b......"
AUTH_REDIRECT_URI="https://endpoint.com/api/login/azure"
AUTH_REDIRECT_TO_URL=true
AUTH_REDIRECT_URL="https://endpoint-client.com"

如果您希望在/config文件夹内自动创建配置/环境文件,您可以通过在composer.json中添加以下配置来包含我的私有配方仓库:

"extra": {
  "symfony": {
      "endpoint": [
          "https://api.github.com/repos/m4n50n/symfony_flex_recipes/contents/index.json",
          "flex://defaults"
      ]
  }
}

使用

OAuth2AzureFactory注入到您的Service或Controller中,并用Request作为参数调用getAuth()方法。

如果存在redirectToUrl配置参数,并且其值为true,则将在认证后重定向到设置的redirectUrl。否则,将返回包含getOwnerData()方法的AuthResponse对象,该方法返回Azure认证账户的数据。

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) {
            // ...
        }

        // ...
    }
}

方法

此包装器定义了以下方法

  • OAuth2AzureFactory:使用getAuth()开始用户认证流程。
  • OAuth2AzureFactory:使用getConfig()返回整个包配置对象。
  • AuthResponse:使用isError()返回认证过程中是否发生错误。

贡献

有关更多信息,请参阅CONTRIBUTING

安全

有关更多信息,请参阅SECURITY

许可

请参阅此仓库中包含的LICENSE,以获取MIT许可证的完整副本,该项目据此许可证授权。