thyseus/yii2-auth0

Yii2 Auth0

安装量:1,102

依赖: 0

建议者: 0

安全性: 0

星标: 1

分支: 2

类型:yii2-extension

2.1.0 2020-01-23 12:39 UTC

This package is auto-updated.

Last update: 2024-09-23 23:21:02 UTC


README

Yii2 Auth0

致谢

这是一个废弃的anli/yii2-auth0项目的现代化分支。旧版本使用了一个非常旧的Auth0版本,因此我决定进行硬分支并对其进行现代化。

它不再使用auth0-lock,而是使用纯PHP注册。

安装

安装此扩展的首选方式是通过composer

运行以下命令:

php composer.phar require --prefer-dist thyseus/yii2-auth0 "*"

或者在您的composer.json文件的require部分添加:

"thyseus/yii2-auth0": "*"

将以下内容添加到您的composer.json文件中。这是yii2-auth0检测您的应用程序用户模型所必需的。

 "autoload": {
        "psr-4": {
            "app\\models\\": "models/"
        }
    },

配置

确保在您的应用程序中配置了Yii::$app->user

您还需要一个至少包含以下属性的app\models\User.php文件,以便此扩展正常工作:

用户名 邮箱 密码 源 created_at updated_at

配置这些属性,以便此扩展可以正常工作。yii2-auth0会将字符串'auth0'放入'source'属性中,以标记该用户为Auth0用户。

更新modules部分:

    [
     'auth0' => require __DIR__ . '/auth0.php',
    ],

添加一个config/auth0.php。您可以在其中处理您的开发密钥。

<?php
$config = [
        'class' => 'thyseus\auth0\Module',
        'adminEmails' => ['admin@example.com'],
    ];

$filenameLocal = __DIR__ . '/auth0_local.php';

if (file_exists($filenameLocal)) {
    return array_merge($config, require $filenameLocal);
}

return $config;

对于生产密钥,您可以在config/auth0_local.php中创建一个新的文件。

<?php
    return [
        'serviceId' => '',
        'domain' => '', // just domain, without protocol (without https://)
        'client_id' => '',
        'client_secret' => '',
        'redirect_uri' => '',
        'redirect_uri_logout' => '', // @see https://auth0.com/docs/quickstart/webapp/php/#logout
        'api_tokens' => [
            'users_read' => '',
            'users_update' => '',
        ]
    ];

并将其添加到您的.gitignore文件中,以便实时密钥不会被推送到您的仓库。

/config/auth0_local.php

登录到Auth0 (https://manage.auth0.com/dashboard) 并更新设置页面中的Allowed Callback Urls

使用方法

将登录按钮的url部分更新为[/auth0/user/login]

将登出按钮的url部分更新为[/auth0/user/logout]

要显示登录用户,请使用:

Html::encode(Yii::$app->user->identity->username);

常见问题解答

如果您遇到以下错误:

\JWT not found

firebase/php-jwt版本更改为v2.2.0

cd @vendor/firebase/php-jwt
git checkout v2.2.0

更新@vendor/composer/autoload_classmap.php为:

'BeforeValidException' => $vendorDir . '/firebase/php-jwt/Exceptions/BeforeValidException.php',
'JWT' => $vendorDir . '/firebase/php-jwt/Authentication/JWT.php',

如果您遇到以下错误:

Cannot handle token prior to 2015-08-05T10:42:34+0200

并将系统时间向前调整几分钟。

如果您遇到以下错误:

cURL error 60: SSL certificate problem: self signed certificate in certificate chain

下载CA到:

C:\xampp\php\ca\cacert.pem

并在C:\xampp\php\php.ini中更新以下内容:

curl.cainfo=C:\xampp\php\ca\cacert.pem

重新启动您的apache2服务器。