eyerim/oauth2-azure-bundle

此包为在 Symfony 中使用 thenetworg/oauth2-azure 提供了一个小巧的包装器。

安装次数: 2,074

依赖项: 0

建议者: 0

安全性: 0

星标: 0

关注者: 0

分支: 2

类型:symfony-bundle

dev-master 2024-05-29 11:18 UTC

This package is auto-updated.

Last update: 2024-09-29 12:05:46 UTC


README

Latest Stable Version License Total Downloads

这个 Symfony 包作为 Azure Active Directory 提供商 OAuth 2.0 客户端的包装器。更多文档可以在官方仓库中找到。

安装

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

symfony composer require m4n50n/oauth2-azure-bundle

启用 Bundle

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

// config/bundles.php

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

配置 Bundle

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 中包含我的私有 Symfony Flex 菜谱仓库,添加以下配置

"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 许可证副本,该项目根据该许可证进行许可。