nti / keycloak-client
轻松连接到keycloak管理员API
0.0.5
2023-09-21 12:56 UTC
Requires
- php: >=5.6.0
- guzzlehttp/guzzle: ^6.0 || ^7.0
- guzzlehttp/guzzle-services: ^1.0
This package is auto-updated.
Last update: 2024-09-21 14:59:14 UTC
README
简介
这是一个无需烦恼即可连接到keycloak管理员REST API的PHP客户端。
功能
- 易于使用
- 无需获取或生成令牌 - 客户端已处理
- 无需指定除基础URI以外的任何URL
- 无需对JSON进行编码/解码,只需按预期提供数据
与Keycloak 7.0+管理员REST API兼容。
https://keycloak.com.cn/documentation -> "Administration REST API"
如何使用
1. 创建新客户端
$client = NTI\KeycloakClient\Admin\KeycloakClient::factory([ 'realm' => 'master', 'username' => 'admin', 'password' => '1234', 'client_id' => 'admin-cli', 'client_secret' => 'sfdasd', 'baseUri' => 'http://127.0.0.1:8180', ]);
从版本0.30开始,如果您的Keycloak基础URL以auth/
开头,请将其添加到baseUri
(例如,http://127.0.0.1:8180/auth)。Keycloak版本7到16的基础URL系统性地使用auth/
。在Keycloak 17+上,这取决于您的设置。
2. 使用它
$client->getUsers(); //Result // Array of users /* [ [ "id" => "39839a9b-de08-4d2c-b91a-a6ce2595b1f3", "createdTimestamp" => 1571663375749, "username" => "admin", "enabled" => true, "totp" => false, "emailVerified" => false, "disableableCredentialTypes" => [ "password", ], "requiredActions" => [], "notBefore" => 0, "access" => [ "manageGroupMembership" => true, "view" => true, "mapRoles" => true, "impersonate" => true, "manage" => true, ], ], ] */ $client->getUser([ 'id' => '39839a9b-de08-4d2c-b91a-a6ce2595b1f3' ]); $client->createUser([ 'username' => 'test', 'email' => 'test@test.com', 'enabled' => true, 'credentials' => [ [ 'type'=>'password', 'value'=>'1234', ], ], ]); $client->updateUser([ 'id' => '39839a9b-de08-4d2c-b91a-a6ce2595b1f3', 'username' => 'test', 'email' => 'test@test.com', 'enabled' => true, 'credentials' => [ [ 'type'=>'password', 'value'=>'1234', ], ], ]); $client->deleteUser([ 'id' => '39839a9b-de08-4d2c-b91a-a6ce2595b1f3' ]);
自定义
支持的凭据
您可以通过更改keycloak客户端的配置来更改用于身份验证的凭据类型。
目前,支持以下凭据
- 密码凭据,默认使用
- 用于通过用户帐户进行身份验证
$client = NTI\KeycloakClient\Admin\KeycloakClient::factory([ ... 'grant_type' => 'password', 'username' => 'admin', 'password' => '1234', ]);
- 客户端凭据
- 用于通过客户端服务帐户进行身份验证
$client = NTI\KeycloakClient\Admin\KeycloakClient::factory([ ... 'grant_type' => 'client_credentials', 'client_id' => 'admin-cli', 'client_secret' => '84ab3d98-a0c3-44c7-b532-306f222ce1ff', ]);
注入中间件
您可以使用middlewares
关键字在keycloak客户端配置中注入Guzzle客户端中间件。
例如
use GuzzleHttp\Middleware; use Psr\Http\Message\RequestInterface; $client = NTI\KeycloakClient\Admin\KeycloakClient::factory([ ... 'middlewares' => [ // throws exceptions when request fails Middleware::httpErrors(), // other custom middlewares Middleware::mapRequest(function (RequestInterface $request) { return $request; }), ], ]);
更改令牌的保存和存储方式
默认情况下,令牌在运行时保存。这意味着在创建新客户端时不会使用之前的令牌。
您可以通过实现自己的TokenStorage
接口来自定义如何在客户端配置中存储令牌,该接口描述了令牌的存储和检索方式。
class CustomTokenStorage implements TokenStorage { public function getToken() { // TODO } public function saveToken(array $token) { // TODO } } $client = NTI\KeycloakClient\Admin\KeycloakClient::factory([ ... 'token_storage' => new CustomTokenStorage(), ]);
自定义Keycloak端点
您可以使用custom_operations
关键字在keycloak客户端配置中注入Guzzle服务操作。这样,您就可以通过自定义扩展内置支持的端点。
$client = KeycloakClient::factory([ ... 'custom_operations' => [ 'getUsersByAttribute' => [ 'uri' => '/auth/realms/{realm}/userapi-rest/users/search-by-attr', 'description' => 'Get users by attribute Returns a list of users, filtered according to query parameters', 'httpMethod' => 'GET', 'parameters' => [ 'realm' => [ 'location' => 'uri', 'description' => 'The Realm name', 'type' => 'string', 'required' => true, ], 'attr' => [ 'location' => 'query', 'type' => 'string', 'required' => true, ], 'value' => [ 'location' => 'query', 'type' => 'string', 'required' => true, ], ], ], ] ]);
支持的API
攻击检测
身份验证管理
客户端属性证书
客户端初始访问
客户端注册策略
客户端角色映射
客户端作用域
客户端
组件
组
身份提供者
密钥
协议映射器
注意:Ids以clientScopeId或clientId发送,mapperId以及其他所有内容均与keycloak文档相同