webiny/login
Composer 安装命令:
composer require webiny/login
包简介
Login application that uses Webiny Framework Security component.
README 文档
README
This is an application that provides additional control layer to the Webiny Framework Security component. The application standardizes the login process and user stateless token storage, making it ideal for RESTful and mobile applications.
Some of the built-in features:
- sessions are stored in database and can be revoked at any point
- authorized devices are also stored in database and can be revoked at any point
- login whitelist and blacklist based on client IP
- rate limit control
- stateless login validation for RESTful application
- only whitelisted devices can log-in (optional)
Sample config
Login: SecurityFirewall: Admin ValidateDevice: true BlockThreshold: 6 BlockTimelimit: 10 DeviceTtl: 90 RateLimitBlacklist: - 123.123.123.123 RateLimitWhitelist: - 127.0.0.1 - 192.168.1.1 - 10.0.2.2
- SecurityFirewall: defines which
Security.Firewallto use for user authentication - ValidateDevice: does the device need to be whitelisted before user can login
- BlockThreshold: after how many bad login attempts should the client be blocked from submitting any new login requests (client is identified as username+ip combination)
- BlockTimelimit: for how many minutes should the client be blocked from submitting any additional login attempts
- DeviceTtl: how long should the device session be valid (used only if ValidateDevice is turned on)
- RateLimitBlacklist: list of IPs that are permanently blocked from submitting login requests
- RateLimitWhitelist: list of IPs that are excluded from the rate limit control
Setup
The Login app requires following Webiny Framework components:
Mongo Indexes
Create the following indexes on your Mongo Database:
db.getCollection('LoginMeta').createIndex({username: 1}); db.getCollection('LoginRateControl').createIndex({ip: 1});
Example setup:
\Webiny\Component\Security\Security::setConfig('./securityConfig.yaml'); \Webiny\Component\Mongo\Mongo::setConfig('./mongoConfig.yaml'); \Webiny\Component\Entity\Entity::setConfig('./entityConfig.yaml'); $security = \Webiny\Component\Security\Security::getInstance(); $loginConfig = \Webiny\Component\Config\Config::getInstance()->yaml('./loginConfig.yaml'); $login = new \Webiny\Login\Login($security, $loginConfig);
Once you have the login instance, you can access the methods inside the class directly:
// check if we have the auth cookie and device cookie $authCookie = \Webiny\Component\Http\Cookie::getInstance()->get('auth-token'); $deviceToken = \Webiny\Component\Http\Cookie::getInstance()->get('device-token'); if ($authCookie && $deviceToken) { try { $user = $login->getUser($authCookie, $deviceToken); } catch (\Webiny\Login\LoginException $le) { } catch (\Exception $e) { } }else{ // process login try { $login->processLogin($username, $deviceToken, $authProvider); // if login is successful, return device and auth tokens $authToken = $login->getAuthToken(); return [ 'authToken' => $authToken, 'deviceToken' => $deviceToken ]; } catch (LoginException $le) { $errorMsg = $le->getMessage(); } catch (\Exception $e) { return $e; } }
Security setup
Note that the Security component needs to implement Stateless token storage:
Security: Tokens: Stateless: StorageDriver: \Webiny\Component\Security\Token\Storage\Stateless # storage driver needs to be set to stateless SecurityKey: SecretKey Firewall: Admin: Token: Stateless
Login services
You can use the Login app as a RESTful service by extending the \Webiny\Login\LoginServices abstract class and implementing
it into Webiny Framework Rest component. (view the app/services.php folder for sample implementation)
POST processLogin
This method processes the login request and returns either a login error, or in case of a success, authToken and deviceToken.
The method takes the following parameters via POST:
- username
- password
- authProvider (optional - defines the name of auth provider inside
Security.Firewallthat should be used to process the request) - deviceToken (optional - required only if ValidateDevice is turned on)
Login error codes:
-
- Rate limit reached.
-
- User account is blocked.
-
- Invalid credentials.
-
- User hasn't confirmed his account.
-
- The current device is not on the allowed list.
-
- Either username or password is missing.
POST getDeviceValidationToken
For the provided username, returns deviceValidationToken.
The device validation token is something that can be emailed or sent to the user via SMS or some other form of communication.
The method takes the following parameters via POST:
- username
POST validateDeviceValidationToken
Validates the provided deviceValidationToken for the given username. If the token matches, deviceToken is returned.
This device token needs to be provided to the processLogin method in order to pass the ValidateDevice.
The method takes the following parameters via POST:
- username
- deviceValidationToken
POST getAccountActivationToken
In case users account is not activated, you need to request an activation token. Usually this token is then emailed to the user via an activation link.
The method takes the following parameters via POST:
- username
POST validateAccountActivationToken
Method that validates the provided activation token and either returns a success message, or an error that the token in not valid.
The method takes the following parameters via POST:
- username
- accountActivationToken
POST logout
Invalidates the provided auth token for the given user.
The method takes the following parameters via POST:
- username
- authToken (the auth token returned by processLogin)
POST generateForgotPasswordResetToken
Generates a forgot password link for the given username.
The method takes the following parameters via POST:
- username
What doesn't it do
The Login app doesn't:
- store any cookies or sessions, so all
remember mefeatures need to be done on your end - it doesn't need to know about your users passwords, this is done via the
Securityclass - doesn't email any links like forgot password, activate account, 2FA tokens - login only generates the tokens, the delivery is up to you
- doesn't do any authorization, only authentication
- doesn't provide any visuals, only a class and a RESTful service
webiny/login 适用场景与选型建议
webiny/login 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 757 次下载、GitHub Stars 达 1, 最近一次更新时间为 2015 年 09 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「authorization」 「Authentication」 「login」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webiny/login 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webiny/login 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webiny/login 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
WeChat OAuth SDK
Automatically logs-in users if they are already authenticated by a remote source. (e.g. environment variable REMOTE_USER)
GraphQL authentication for your headless Craft CMS applications.
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
Debugging a problem and need to login as one of your customers? This allows you to authenticate as any of your customers.
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
统计信息
- 总下载量: 757
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 9
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-09-04