boneybone / auth-server-sdk
BoneyBone Auth Server Laravel SDK
Requires
- firebase/php-jwt: ^6.4.0
- guzzlehttp/guzzle: ^7.3
- phpseclib/phpseclib: ^2.0
README
这是用于BB Auth Server引擎的Laravel应用程序的Auth Server SDK。
要使用此SDK,您需要创建一个BB Auth Server应用程序以获取客户端ID
和客户端密钥
。
重大更改
由于遵循PSR-4规范,一些类命名空间已更改,如下所示:
旧命名空间 | 新命名空间 |
---|---|
\BoneyBone\AuthServer\Middleware\AuthServerMiddleware | \BoneyBone\AuthServer\Http\Middleware\AuthServerMiddleware |
\BoneyBone\AuthServer\Middleware\AuthServerPermissionsMiddleware | \BoneyBone\AuthServer\Http\Middleware\AuthServerPermissionsMiddleware |
\BoneyBone\AuthServer\Providers\AuthServerServiceProvider | BoneyBone\AuthServer\AuthServerServiceProvider |
如果您正在使用上述类,请相应地进行更改。
安装
使用Composer安装此包。
composer require boneybone/auth-server-sdk
配置
使用vendor:publish
artisan命令发布配置文件。
php artisan vendor:publish --tag=auth-server
在config/auth.php
文件中将users
提供者的驱动程序更改为boneybone-auth-server
。
'providers' => [
'users' => [
'driver' => 'boneybone-auth-server',
],
],
....
使用您的凭据设置以下.env
变量。
AUTH_SERVER_CLIENT_ID="YOUR CLIENT ID"
AUTH_SERVER_CLIENT_SECRET="YOUR CLIENT SECRET"
AUTH_SERVER_REDIRECT_URI="YOUR CLIENT REDIRECT URI"
AUTH_SERVER_ENDPOINT="THE DOMAIN WHERE THE AUTH-SERVER WERE DEPLOYED"
在AppServiceProvider
上加载Public
和Private
密钥。
// Load the public key
AuthServer::loadPublicKey($pathToPublicKey);
// Load the private key.
AuthServer::loadPrivateKey($pathToPrivateKey);
完成!
可用方法
此SDK附带AuthServer
外观,它有助于您更轻松地与Auth Server交互。以下是AuthServer
外观的可用方法。
当您的应用程序需要执行服务器到服务器的操作,例如在更新用户实体时执行后台作业或在cron作业上运行任务,而不需要用户交互或用户JWT令牌时,您的应用程序需要客户端访问令牌
,可以使用getClientAccessToken()
方法请求。
AuthServer::getClientAccessToken();
// You need to set the client_id & client_secret first on your .env file, otherwise this method will return null
获取当前用户的访问令牌。
AuthServer::getAccessToken();
当您的前端应用程序需要交换从Auth Server回调获取的授权码时,您可以使用exhangeAuthorizationCode($code)
方法。
AuthServer::exchangeAuthorizationCode($code);
您可以使用refresh_token
字符串使用refreshAccessToken($refreshToken)
方法刷新访问令牌。
AuthServer::refreshAccessToken($refreshToken);
如果您想直接将Auth Server回调中的authorization_code
交换成JWT令牌,您可以使用exchangeCodeIntoToken($code)
方法。
AuthServer::exchangeCodeIntoToken($code);
如果您想获取用户数据,可以使用getUserInfo()
方法。
AuthUser::getUserInfo();
// CAUTION: This method will return null if the user access token hasn't been set yet, you can set the user access token either by directly set the token using the AuthServer::setAccessToken($token) or put your API under AuthServer middleware, the middleware will do the job for you.
设置成功认证后的重定向路径。
AuthServer::redirectTo('/home);
销毁当前用户会话。您可以将用户重定向到/auth/deauthorize
或调用logout()
方法。
AuthServer::logout();
如果您已使用此SDK将Public Key
和Private Key
设置为您的应用程序,您可以使用以下方法获取密钥内容。
AuthServer::getPublicKey(); // to get the contents of public key.
AuthServer::getPrivateKey() // to get the contents of private key.
作为后端服务器操作
您可以使用此SDK仅作为后端服务器,也许您有不同的前端堆栈,并且您只需要Laravel作为后端。
要使用此SDK仅作为后端服务器,您所有的API
都应该在\BoneyBone\AuthServer\Http\Middleware\AuthServerMiddleware
下受保护,这个中间件拦截请求,从请求的JWT令牌中提取用户,并将用户登录到您的API(即使API应该是无状态的,但为了方便)。
在您的app/Http/Kernel.php
文件中将此中间件添加到$routeMiddleware
数组中。
$routeMiddleware = [
...
'auth-server-api' => \BoneyBone\AuthServer\Http\Middleware\AuthServerMiddleware::class,
...
];
然后,在您的routes/api.php
或任何您打算用作API路由的路由文件中,使用我们刚刚注册的中间件。
Route::middleware(['auth-server-api'])->group(function() {
// Your Routes here.
});
在auth-server-api
中间件下的任何路由都可以通过$request->user()
或常规的Auth::user()
外观访问请求的用户。如果提供的JWT令牌有效,用户将是\BoneyBone\AuthServer\BoneyBoneUser
的实例,但这没关系,因为这些类实现了Illuminate\Contracts\Auth\Authenticatable
,因此您可以像访问App\User
实例一样访问这些用户对象。这是同一个实例。
获取用户的角色和权限
一旦您从$request->user()
或常规的Auth::user()
获取了用户,您可以使用以下代码获取角色和权限。
$user = Auth::user();
// Get the roles based on the current client.
$roles = $user->getRoles(); // The user roles array, based on the current client id.
// Get all assigned roles from all clients.
$roles = $user->getRoles(true); // The user roles array from all of clients that user assigned to.
// Get the user permissions based on the current client.
$permissions = $user->getPermissions(); // The array of assigned permission based on current client.
// Get all assigned permissions from all clients.
$permissions = $user->getPermissions(true); // The array of permissions from all clients that the user assigned to.
使用权限保护您的路由
您可能需要仅对具有特定权限的请求允许对某些端点的访问,例如,您有创建用户
功能,但只有具有create-user
权限的用户才能创建新用户。您可以使用AuthServerPermissionsMiddleware
来完成此操作。
在您的app/Http/Kernel.php
文件中添加以下中间件
// app/Http/Kernel.php
protected $routeMiddleware = [
...
'permissions' => \BoneyBone\AuthServer\Http\Middleware\AuthServerPermissionsMiddleware::class
...
];
然后您可以在您的路由上使用中间件,并附带所需的权限。
// Single permission
Route::post('/users', 'UserController@create')->middleware('permissions:create-user);
// Multiple permissions
Route::delete('/users/1', 'UserController@delete')->middleware('permissions:delete-user,manage-staff,manage-payroll);
关于
这是名为Auth Server
的集中式用户数据库的Laravel SDK,Auth Server是保存所有用户相关实体的地方。
目前,此SDK将为您的应用程序添加3个路由:/auth/authorize
、/auth/callback
和/auth/deauthorize
,其中/auth/authorize
路由是用户可以登录的地方,它将用户重定向到Auth Server,认证成功后,Auth Server将重定向您回到您的应用程序回调。
这是/auth/callback
将用于处理回调的地方(包括将授权代码交换为访问令牌、获取用户数据、处理Authenticatable以及最终登录用户)。
当您想注销用户时,您可以将用户重定向到/auth/deauthorize
来注销用户会话。
这是如何工作的?
在底层,Laravel认证是如何工作的,这是由于3个组合:UserProvider
、Authenticatable
和Guards
,每个部分都处理了每个请求的凭证、可以自认证的Who
以及可以从Where
获取用户的数据库、Redis等。
此SDK主要是扩展了UserProvider
和Authenticatable
,为什么不是Guard
?因为默认的Guard已经足够好地从Cookies
或Session
中获取凭证。
此SDK扩展的UserProvider
处理用户数据可以从中获取的Where
,在这种情况下是Auth Server API。
Authenticatable
是用户数据的格式化方式,例如获取正确的标识符、记住令牌字符串和标识符本身。