duncan3dc / oauth
处理基本的OAuth/OAuth2身份验证,并提供常用服务的类
1.4.0
2022-09-09 14:16 UTC
Requires
- php: ^7.2 || ^8.0
- ext-oauth: *
- duncan3dc/serial: ^1.0
- duncan3dc/sql-class: ^1.8
Requires (Dev)
- phpunit/phpunit: ^8.5.29
README
处理非基于Web的OAuth/OAuth2身份验证,并提供常用服务的提供者
我真的不认为你会觉得这些类很有用,如果你坚持的话,别说我没警告过你
你可能正在寻找 thephpleague/oauth1-client 或 thephpleague/oauth2-client
需求
- 这些类需要使用 sql-class 项目(用于存储授权凭证)
- 活动数据库必须包含一个名为oauth的表(或者通过Sql类中的定义了解它)
- 该表必须有以下模式
`type` varchar(20),
`username` varchar(100),
`state` int(11),
`token` varchar(100),
`secret` varchar(100),
UNIQUE KEY `type, username` (`type`,`username`)
OAuth2
OAuth2类需要手动干预来初始授权,但一旦你的令牌/密钥在数据库中,它们就像OAuth类一样工作。
示例
基本的Twitter示例
use duncan3dc\OAuth\Twitter; use duncan3dc\SqlClass\Sql; Sql::addServer("twitter", [ "hostname" => "example.com", "username" => "my_twitter_app", "password" => "secret_password", "database" => "twitter", ]); $twitter = new Twitter([ "username" => "my_handle", "authkey" => "XfHrRTY25FgkyqxDfbpe", "secret" => "gwj8c29GHDWdphmQhGtHPx4GybwRfhXplT3CD0VG1n", ]); # The authorise method returns null if we are already authorised. Otherwise it returns a url to grant at if ($url = $twitter->authorise()) { throw new \Exception("Authorsation failed, grant permission here: " . $url); } $userData = $twitter->user("my_handle"); print_($userData);
如果你扩展了Sql类(或者使用兼容的接口项目),你可以让OAuth类如此使用该类
use duncan3dc\OAuth\Twitter; use MrCoder\MyCustom\Sql; \duncan3dc\OAuth\Sql::useClass(Sql::class); $twitter = new Twitter([
如果你的oauth表不在活动数据库中,则可以使用以下方式让Sql类知道它的位置
use duncan3dc\OAuth\Twitter; use duncan3dc\SqlClass\Sql; Sql::addServer("twitter", [ "hostname" => "example.com", "username" => "my_twitter_app", "password" => "secret_password", "database" => "twitter", "definitions" => ["oauth" => "my_other_database"], ]); $twitter = new Twitter([