webiny/login 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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.Firewall to 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.Firewall that should be used to process the request)
  • deviceToken (optional - required only if ValidateDevice is turned on)

Login error codes:

    1. Rate limit reached.
    1. User account is blocked.
    1. Invalid credentials.
    1. User hasn't confirmed his account.
    1. The current device is not on the allowed list.
    1. 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 me features need to be done on your end
  • it doesn't need to know about your users passwords, this is done via the Security class
  • 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 webiny/login 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 757
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 9
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 9
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-09-04