wappcode / gql-pdss-auth
3.1.3
2024-06-27 05:45 UTC
Requires
- firebase/php-jwt: ^6.3
Requires (Dev)
- phpunit/phpunit: ^9.6
- wappcode/gqlpdss: dev-master
- wappcode/graphql-basic-client: ^1.0
README
为PHP项目添加认证的库。
与wappcode/gqlpdss库兼容
使用GQLPDSS安装
在wappcode/gqlpdss项目中执行以下命令
composer require wappcode/gql-pdss-auth
添加实体 doctrine
// archivo config/doctrine.entities.php
<?php
return [
// ...
"GPDAuth\Entities" => __DIR__ . "/../vendor/wappcode/gql-pdss-auth/GPDAuth/src/Entities"
// ...
];
执行更新数据库的命令
vendor/bin/doctrine orm:schema-tool:update --force
通过文件设置配置
// config/local.config.php
<?php
use GPDAuth\Library\AuthConfig;
use GPDAuth\Library\IAuthService;
return [
// configuración de otros módulos
// .....
AuthConfig::JWT_SECURE_KEY => "secret_key",
AuthConfig::AUTH_SESSION_KEY => "gpd_auth_session_key", // key o indice asociativo del array $_SESSION para authentificación
AuthConfig::JWT_EXPIRATION_TIME_KEY => 1200, // Tiempo en segundos
AuthConfig::JWT_ALGORITHM_KEY => 'HS256',
AuthConfig::AUTH_METHOD_KEY => IAuthService::AUTHENTICATION_METHOD_SESSION_OR_JWT,
AuthConfig::AUTH_ISS_KEY => $_SERVER["SERVER_NAME"] ?? "localhost",
AuthConfig::JWT_ISS_CONFIG => [
// se agregan los datos de los iss usando el nombre como clave
"https://issurl" => [ // url o id del iss
AuthConfig::JWT_SECURE_KEY => "secure_key_to_iss",// opcional si no esta definido utiliza la clave de la configuración local
AuthConfig::JWT_ALGORITHM_KEY => "HS256", // opcional si no esta definido utiliza el algoritmo de la configuración local
AuthConfig::AUTH_ISS_ALLOWED_ROLES => [ // array con los roles permitidos para el iss se permite el mapeo de un rol del iss a uno del sistema
"iss_admin_role" => "admin",
"custom_role" => "custom_role"
]
]
]
// .....
];
通过环境变量设置配置(可选)
变量
GPDAUTH_CONFIG__JWT_SECURE_KEY
GPDAUTH_CONFIG__GPDAUTH_CONFIG__AUTH_SESSION_KEY
GPDAUTH_CONFIG__GJWT_EXPIRATION_TIME_KEY
GPDAUTH_CONFIG__JWT_ALGORITHM_KEY
GPDAUTH_CONFIG__AUTH_METHOD_KEY
GPDAUTH_CONFIG__AUTH_ISS_KEY
添加模块
// public/index.php
<?php
//...
use GPDAuth\GPDAuthModule;
//...
$app->addModules([
GPDAuthModule::class, // se agrega módulo de autentificación
// Otros módulos
//...
AppModule::class,
]);
//...
为了给解析器或路由添加安全性,请使用 AuthService 服务
<?php
//...
$auth = $this->context->getServiceManager()->get(AuthService::class);
// Revisa si el usuario esta firmado
$auth->isSigned();
AuthService 方法
AuthService 服务包含一些有用的方法来决定用户是否有访问资源的授权
isSigned
如果用户已登录,则返回 true
login
登录用户
$auth->login("username","passwoerd");
logout
登出用户
如果使用JWT,则清除会话,但JWT仍然有效,直到过期
$auth->login("username","passwoerd");
hasRole
如果用户具有特定角色,则返回 true
// Revisa si el usuario tiene el rol de admin
$auth->hasRole("admin");
hasSomeRoles
如果用户具有任意一个角色,则返回 true
// Revisa si el usuario tiene alguno de los roles (staff, publisher)
$auth->hasSomeRoles(["staff", "publisher"]);
hasAllRoles
如果用户具有所有分配的角色,则返回 true
// Revisa si el usuario tiene todos los roles (staff, publisher)
$auth->hasAllRoles(["staff", "publisher"]);
hasPermission
如果用户具有权限,则返回 true
权限可以是针对特定用户、角色的或全局的。优先级按此顺序应用(用户权限、角色权限、全局权限)。
范围可以用来标识用户是否有访问资源的权限,但有限制,例如,用户可能有访问POST资源的权限,但只能编辑属于他自己的资源。
$auth->hasPermission("resource_post","VIEW"); // retorna true si el usuario tiene permiso para ver post sin importar el scope
$auth->hasPermission("resource_post","VIEW","OWNER"); // retorna true si el usuario tiene permiso para ver post pero con scope OWNER
$auth->hasPermission("resource_post","VIEW","ALL"); // retorna true si el usuario tiene permiso para ver post pero con scope ALL
hasSomePermissions
如果用户有一个或多个权限,则返回 true
可以传递多个资源、权限和范围,将它们组合起来以确定是否具有其中任何一个。
$auth->hasSomePermissions(["resource_post"],["CREATE","UPDATE"],["ALL"]);
hasAllPermissions
如果用户具有所有权限,则返回 true
可以传递多个资源、权限和范围,将它们组合起来以确定是否具有所有权限。
$auth->hasSomePermissions(["resource_post"],["VIEW",CREATE","UPDATE","DELETE"],["ALL"]);
不使用GQLPDSS使用
安装
composer require wappcode/gql-pdss-auth
将模块的实体添加到 doctrine 路由中
__DIR__ . "/../vendor/wappcode/gql-pdss-auth/GPDAuth/src/Entities"
更新数据库
vendor/bin/doctrine orm:schema-tool:update --force
创建 AuthService 类的实例,并使用其方法进行登录、检查角色和检查权限
<?php
//...
$auth = new AuthService(
$entityManager,
$_SERVER["SERVER_NAME"] ?? "localhost",
"JWT", // o SESSION
);
$auth->setJwtAlgoritm("HS256");
$auth->setjwtExpirationTimeInSeconds(1200);// En segundos
$auth->setJwtSecureKey("secret_key");
$auth->initSession();
//...