lambdacasserole / minim
此包已被放弃,不再维护。未建议替代包。
PHP中最小化单用户认证。
v1.6
2017-03-08 10:03 UTC
Requires
- php: >=5.3.0
- defuse/php-encryption: ^2.0
- ircmaxell/password-compat: ^1.0
- mustangostang/spyc: ^0.5.1
- paragonie/random_compat: ^2.0
README
PHP中最小化单用户认证。
有时,你会构建一个需要
- 无需数据库运行
- 仅管理员后端可访问
- 仅一个用户可访问
Minim就是为了这个目的而设计的;它是一个安全的单用户认证系统,不会做任何愚蠢的事情,比如泄露用户密码(或以明文存储)或在非安全(非HTTPS)连接上操作(除非你想要这样做)。
安装
使用Composer安装Minim,如下所示
composer require semibreve/minim
或者,如果你使用的是PHAR(确保php.exe
可执行文件在您的PATH中)
php composer.phar require semibreve/minim
配置
Minim将需要你创建一个类似以下内容的配置文件
# Don't commit this file to source control, it contains your secret settings. admin_email: me@example.com # The e-mail address of the user, used as a username. admin_password_hash: $2y$10$x8.kXrWv4lXFpObosuwQ0uoiQAUeFAlEL.oi0tN5pnM.72hoK9e8K # The user's password hash. secret_key: 7WCPTI3of3cp # The secret key the application uses for symmetric encryption token_length: 32 # The length, in bytes, of any generated authentication tokens. token_ttl: 1200 # The time to live for authentication tokens, in seconds. cookie_name: minim_auth # The name of the authentication cookie. session_file_name: /var/www/minim/token.dat # The name of the session file on-disk. cookie_ssl_only: false # Whether or not cookies are enabled for HTTPS only. If enabled, non-HTTPS requests will fail. cookie_http_only: true # Whether to restrict cookies to HTTP only and disallow access by client-side script.
上述文件指定了一些默认凭据
Email: me@example.com
Password: demo
这些凭据必须在您进入生产环境之前更改,所以您需要执行以下操作
- 将上述演示配置文件复制到您的项目中。确保它被任何版本控制系统忽略。
- 在您最喜欢的文本编辑器中打开它。
- 将
admin_email
字段更改为您的电子邮件地址 - 将
admin_password_hash
字段更改为您选择密码的bcrypt散列。使用捆绑的minim-genhash
实用程序生成散列,在项目根目录中执行php vendor/bin/minim-genhash <password>
。 - 将
secret_key
字段更改为至少12个字符长的随机字符串。 - 将
salt
字段更改为至少12个字符长的随机字符串。 token_length
字段的默认值32对于大多数应用程序来说是足够的。token_ttl
字段的默认值1200秒(20分钟)对于大多数应用程序来说是足够的。- 将
session_file_name
字段更改为Minim可以读取和写入的、服务器不会提供的文件的绝对路径。 - 如果您通过HTTPS操作,请将
cookie_ssl_only
字段更改为true
。如果您不是,请仔细审视您的应用程序,并问自己为什么在存在像Let's Encrypt这样的免费工具时,还要考虑通过不安全的连接请求用户凭据。 - 将
cookie_http_only
保持为true
,以便使认证cookie只能通过HTTP读取,而不能通过客户端脚本读取。
要查看Minim的示例用法,请查看演示存储库。
用法
像这样加载您的Minim配置文件
$auth = new Authenticator(new Configuration('my-config-file.yml'));
从这里您可以为用户登录
$auth->authenticate('email', 'password'); // Authenticate user, true on success false on failure.
或根据他们是否已登录将用户重定向到另一页面
// Check if user is authenticated. if (!$auth->isAuthenticated()) { header('Location: /forbidden.php'); // Not logged in, go to jail. die(); }
限制
不要依赖Minim默认安全,并始终进行自己的渗透测试。