php - 自定义 OAuth2 用户数据库

标签 php database oauth oauth-2.0

我目前正在我的网站中实现 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

最佳答案

有了这个库,您可以轻松编写自定义代码,因此您不必触及这个库的核心。 对于这个问题,您必须创建一个自定义存储类来实现 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));

关于php - 自定义 OAuth2 用户数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32023991/

相关文章:

php - 如何在CodeIgniter上上传图像并将其显示在另一个页面上

php - HTML 获取 URL 参数

php - Bootstrap 4 navwalker - 多级菜单

数据库:删除或不删除记录

android - 如何/在哪里存储 Android 上的刷新 token ?

php - 动态表单、PHP 和 MYSQL

database - Delphi SQLite3 使用ZeosLib,如何创建数据库和表?

sql-server - 在远程 SQL Server 引擎上创建数据库

ios - Square API + iOS : OAuth not working?

php - Symfony HWIOAuthBundle 访问 token