restricted/authchain

Laravel 4 链式认证(LDAP、IMAP、IP、Eloquent)

1.0.8 2015-01-30 00:00 UTC

This package is not auto-updated.

Last update: 2024-09-24 01:02:07 UTC


README

警告:项目不再维护!

支持本地数据库、LDAP、IMAP和IP地址多域名单点登录认证。

对于LDAP和IMAP认证,您需要安装ldapimap PHP扩展。

安装

通过Composer安装此包。编辑您的项目composer.json文件以需要restricted/authchain

"require": {
	"laravel/framework": "4.2.*",
	"restricted/authchain": ">=1.0.6"
}

从终端更新Composer

composer update

此操作完成后,下一步是添加服务提供者。打开app/config/app.php,并将新项目添加到providers数组中。

'providers' => [
    // Your Laravel providers here...
    'Restricted\Authchain\AuthchainServiceProvider'
]

从终端创建示例配置文件

php artisan config:publish restricted/authchain

将默认认证提供者更改为authchain

打开app/config/auth.php并将driver部分更改为authchain

return array(

	'driver': 'authchain'

	// Related stuff...
);

有关完整配置说明,请参阅app/config/packages/restricted/authchain/config.php

您需要在app/models中创建用户模型并创建迁移。

有关模型和迁移的详细信息,您可以在vendor/restricted/authchain/quickstart中查看。

您可以直接将vendor/restricted/authchain/quickstart/models/文件夹中的内容复制到app/models

可以通过命令php artisan migrate --package="restricted/authchain"执行示例迁移。

注意:迁移不包括时间戳。

如果您不使用IP地址认证,请将app/config/packages/restricted/authchain/config.php中的['defaults']['ip']设置为false

快速入门

安装laravel(见https://laravel.net.cn/docs/quick

composer create-project laravel/laravel your-project-name --prefer-dist

安装authchain提供者(见安装

app/config/packages/restricted/authchain/config.php中配置您的域名

从以下位置复制文件

cp -r vendor/restricted/authchain/quickstart/models/* app/models
cp -r vendor/restricted/authchain/quickstart/controllers/* app/controllers/
cp -r vendor/restricted/authchain/quickstart/views/* app/views

将您的app/filters.php文件中的auth路由过滤器替换为从vendor/restricted/authchain/quickstart/filters.php中的内容。

cat vendor/restricted/authchain/quickstart/filters.php >> app/filters.php

app/routes.php中的内容添加到vendor/restricted/authchain/quickstart/routes.php

cat vendor/restricted/authchain/quickstart/routes.php >> app/routes.php 

从终端运行应用程序:php artisan serve

转到https://:8000/并享受吧!

需要社区反馈

  • 需要实现OAuth2吗?
  • 其他提供者?

贡献

欢迎任何建议

您可以为authchain轻松编写自己的认证提供者

自定义提供者示例(见src/Restricted/Authchain/Provider/Domain/CustomProviderExample

namespace Restricted\Authchain\Provider\Domain;

use Restricted\Authchain\Config\Loader;
use Restricted\Authchain\Provider\Provider;
use Restricted\Authchain\Provider\ProviderInterface;

class CustomProviderExample extends Provider implements ProviderInterface
{
    // Authentication logic
    // $this->username is username provided by user
    // $this->password is password from form

    // @return UserInterface|null

    public function authenticate()
    {
    	// Loading users from config for domain $this->domain
    	
        $users = Loader::domain($this->domain)['users'];

	// If user not found in array, return null
	
        if (!isset($users[$this->username])) {
            return null;
        }
	
	// Grab user password from config

        $password = $users[$this->username];

	// Check password

        if (\Hash::check($this->password, $password)) {
        
            $newUser                       = $this->model();
            $newUser->{Loader::username()} = $this->username;
            $newUser->{Loader::password()} = \Hash::make($password);
            $newUser->enabled              = true;

            $newUser->save();

            return $newUser;
        }

        return null;
    }
    
    // Must return name of the provider, for example 'custom'
    // In app/config/packages/restricted/authchain/config.php
    // you can regiter new provider in 'providers' array and pass config variables to it

    public function provides()
    {
        return 'custom';
    }

}

app/config/packages/restricted/authchain/config.php中为自定义提供者创建配置

providers部分注册自定义提供者

'providers' => array(
    // ...
    'Restricted\Authchain\Provider\Domain\CustomProviderExample',
)

domains部分

'localhost' => array(
    'provider' => 'custom', // See method provides()
        'users' => array(
            'demo@localhost' => '$2y$10$/Ij0dzDL49OaODli.1GcveefSdEapt2vgb8shplVI7RIJadPmL6km' // Encrypted password
    )
)

现在,所有来自域名localhost的用户都通过自定义提供者和本地提供者(Eloquent)进行认证。

  • 如有问题,请创建带有您的问题的问题。
  • 如有功能请求,请创建带有功能详细说明的问题。

许可证

在MIT许可证的条款下分发。