0

我正在创建一个需要通过 microsoft 进行 oauth 授权的站点。在 yii/authclient 中只有实时客户端,它不再工作了。

我试着自己写,但出了点问题。据我了解,我的 AuthAction 没有看到 clientId 并返回没有文本的 404 异常。这是我的身份验证客户端代码。

我得到了什么 我得到了什么

AuthAction 类方法运行(默认) AuthAction 类方法运行(默认)

class Office365OAuth extends OAuth2
{
    public $authUrl = 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize';
    public $tokenUrl = 'https://login.microsoftonline.com/common/oauth2/v2.0/token';
    public $apiBaseUrl = 'https://login.microsoftonline.com/common/oauth2/v1.0';

    public $scope = null;

    public function init()
    {
        parent::init();
        if ($this->scope === null)
        {
            $this->scope = 'https://graph.microsoft.com/User.Read';
        }
    }

    /**
     * Overrides default function to fix malformed url
     */
    public function getReturnUrl()
    {
        return $this->returnUrl;
    }

    protected function defaultName()
    {
        return 'office365';
    }

    protected function defaultTitle()
    {
        return 'Office365';
    }

    /**
     * For popup mode
     */
    protected function defaultViewOptions()
    {
        return [
            'popupWidth' => 800,
            'popupHeight' => 500,
        ];
    }

    /**
     * Gets new auth token to replace expired one.
     */
    protected function initUserAttributes()
    {
        return $this->api('me', 'GET');
    }
}

那么,如何通过 MS 图形进行身份验证?

4

1 回答 1

0

yii\authclient包需要使用具有请求参数的 returnUrl authclient=live,例如https://example.com/site/auth?authclient=live

但是,Azure 禁止在returnUrl. 因此,要yii\authclient使用 Azure,即 returnUrl 为https://example.com/site/auth/live. 您需要使用请求参数美化网址,如下所示:

config/main.php

'components' => [
     'urlManager' => [
         'class' => 'yii\web\UrlManager',
         'enablePrettyUrl' => true,
         'rules' => [
             'site/auth/<authclient>' => 'site/auth'
         ]
     ]
]

controllers/SiteController.php,

public function actions()
{
    return [
        'auth' => [
            'class' => 'yii\authclient\AuthAction',
            'successCallback' => [$this, 'onAuthSuccess']
        ]
    ];
}

...

public function onAuthSuccess($client) {
    // get user data from client
    $userAttributes = $client->getUserAttributes();
    // DO YOUR THING
}
于 2020-12-03T03:16:36.977 回答