【问题标题】:Custom OAuth2 user database自定义 OAuth2 用户数据库
【发布时间】:2015-11-08 12:46:02
【问题描述】:

我目前正在我的网站中实施 OAuth2,以保持用户在 Android 应用中的登录状态。我想将默认的 OAuth2 用户数据库更改为我自己的用户数据库。不幸的是,我不知道该怎么做。应该可以通过覆盖类而不改变核心库中的代码,但是怎么做呢?

这是我在server.php 文件中的内容:

// Autoloading (composer is preferred, but for this example let's just do this)
require_once('/src/OAuth2/Autoloader.php');
OAuth2\Autoloader::register();

$storage = new OAuth2\Storage\Pdo(array('dsn' => 'mysql:host=xxxx;dbname=xxxx', 'username' => 'xxxx', 'password' => 'xxxx'));

// Pass a storage object or array of storage objects to the OAuth2 server class
$server = new OAuth2\Server($storage);

// Add the "Password / User Credentials" grant type
$server->addGrantType(new OAuth2\GrantType\UserCredentials($storage));

所以在这里我想使用我自己的用户表而不是默认的oauth_users 表。而且因为密码是加盐的,所以我也需要进行不同的密码检查。我正在使用 BShaffer OAuth2 库:https://github.com/bshaffer/oauth2-server-php

【问题讨论】:

  • 您好,我也有同样的问题,您有答案了吗?

标签: php database oauth oauth-2.0


【解决方案1】:

有了这个库,编写自定义代码变得很容易,因此您不必触及这个库的核心。 对于这个问题,您必须创建一个实现UserCredentialsInterface 的自定义存储类。这里有两个方法需要你自己实现

public function checkUserCredentials()

public function getUserDetails()

在这里,您可以实现检查用户详细信息和获取用户详细信息的逻辑。 在此之后,您需要将此存储添加到 oAuth 服务器,如下所示:

$server = new OAuth2\Server($storage);
$userStorage = new YourCustomUserStorage();
$server->addStorage($userStorage, 'user_credentials');

您还需要将此存储传递给您要添加到服务器的任何授予类型,在您的情况下,它看起来像这样:

$server->addGrantType(new OAuth2\GrantType\UserCredentials($userStorage));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-25
    • 2020-08-17
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    • 2018-07-27
    • 2017-02-13
    • 2013-04-17
    相关资源
    最近更新 更多