vinhhoang / oauth2-azure
Oauth2 Azure 登录
v1.0.4
2023-11-08 04:51 UTC
Requires
- php: >=5.6
- firebase/php-jwt: ~3.0||~4.0
- league/oauth2-client: ~2.0
This package is auto-updated.
Last update: 2024-09-08 09:09:49 UTC
README
本软件包基于以下源代码构建 Azure Active Directory Provider for OAuth 2.0 Client.
安装
要安装,请使用 composer
composer require vinhhoang/oauth2-azure
配置
安装 Socialite 库后,在您的 config/app.php 配置文件中注册 VinhHoang\OAuth2\AzureServiceProvider
'providers' => [ // Other service providers... VinhHoang\OAuth2\AzureServiceProvider::class, ],
同时,将 Azure 外观添加到您的 app 配置文件中的 aliases 数组
'Azure' => VinhHoang\OAuth2\Facades\Azure::class
然后,运行此命令
php artisan vendor:publish --provider="VinhHoang\OAuth2\AzureServiceProvider"
您还需要添加应用程序使用的 OAuth 服务的凭据。这些凭据应放在您的 config/oauth2azure.php 配置文件中
[
'clientId' => 'your-client-id',
'clientSecret' => 'your-client-secret',
'redirectUri' => 'your-redirect-uri',
'tenant' => 'your-tenant',
],
基本用法
<?php namespace App\Http\Controllers; use Azure; class LoginController { public function login() { return Azure::redirect(); } public function handleCallback() { $token = Azure::getAccessToken('authorization_code', [ 'code' => $_GET['code'], 'resource' => 'https://graph.windows.net', ]); try { // We got an access token, let's now get the user's details $me = Azure::get("me", $token); } catch (\Exception $e) { // } // Use this to interact with an API on the users behalf echo $token->getToken(); } public function logout() { $redirect_url = "http://example.com"; return redirect(Azure::getLogoutUrl($redirect_url)); } }
您需要定义控制器方法的路由
Route::get('login', 'LoginController@login'); Route::get('login/callback', 'LoginController@handleCallback'); Route::get('logout', 'LoginController@logout');
资源所有者
从版本 1.1.0 开始,资源所有者信息由 Azure Active Directory 通过 access_token 中的 JWT 解析。它公开了少量属性和一个函数。
示例
$resourceOwner = Azure::getResourceOwner($token); echo 'Hello, '.$resourceOwner->getFirstName().'!';
公开的属性和函数包括
getId()- 获取用户的对象 id - 每个用户都是唯一的getEmail()- 获取用户的电子邮件 - 每个用户都是唯一的getFirstName()- 获取用户的首名getLastName()- 获取用户的姓氏getTenantId()- 获取用户所属租户的 idgetUpn()- 获取用户的主用户名称,也可以用作用户的电子邮件地址claim($name)- 从 JWT 获取任何其他声明(指定为$name),完整列表可在此处找到 这里