oauth2-server是使用PHP实现的OAuth2 Server,对OAuth2的实现比较完备。本文介绍如何在Win10上安装调试它。
首先需要使用scoop安装以下几个软件:
scoop install composer
scoop install php
scoop install openssl
scoop install curl
composer是PHP的包管理软件,有点像nodejs的npm或者yarn。composer默认需要开启php的openssl,参考The openssl extension is required for SSL/TLS protection。
使用php --ini
查看默认的php.ini的位置,然后打开以下扩展:
extension=openssl
extension=mbstring
extension=fileinfo
使用scoop安装php,默认的php.ini会在%PHP_INI_SCAN_DIR%
中找到,这个环境变量的默认值是 ~\scoop\apps\php\current\cli;~\scoop\apps\php\current\cli\conf.d;
。参考Custom PHP configuration。
为了方便国内使用composer,可以将软件包源改成阿里云 Composer 全量镜像的地址,命令如下:
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
取消设置只需执行:
composer config -g --unset repos.packagist
测试slim
slim是一个简单易用的php web框架,遵循PSR-7: HTTP message interfaces - PHP-FIG。
安装slim:
composer require league/oauth2-server
composer require slim/slim:"4.*"
composer require slim/psr7
在public目录下创建一个index.php,内容如下:
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
$app = AppFactory::create();
$app->get('/', function (Request $request, Response $response, $args) {
$response->getBody()->write("Hello world!");
return $response;
});
$app->run();
然后使用PHP的Built-in web server来运行Slim:
php -S localhost:8000 -t public/
运行oauth2-server的示例
oauth2-server的示例在:https://github.com/thephpleague/oauth2-server/tree/master/examples
首先克隆这个仓库:git clone --depth=1 https://github.com/thephpleague/oauth2-server.git
将examples目录单独拷出来,重新取一个名字,进入那个目录,并执行:
composer install
composer require league/oauth2-server
openssl genrsa -out private.key 2048
openssl rsa -in private.key -pubout > public.key
打开composer.json文件,将下面这段代码:
"psr-4": {
"OAuth2ServerExamples\\": "src/",
"League\\OAuth2\\Server\\": "../src/"
}
改成
"psr-4": {
"OAuth2ServerExamples\\": "src/"
}
这是因为League\\OAuth2\\Server\\
将由league/oauth2-server
提供。
然后执行:
php -S localhost:4444 -t public
来启动php的内建服务器。
使用下面的命令进行测试:
curl -X "POST" "http://localhost:4444/client_credentials.php/access_token" ^
-H "Content-Type: application/x-www-form-urlencoded" ^
-H "Accept: 1.0" ^
--data-urlencode "grant_type=client_credentials" ^
--data-urlencode "client_id=myawesomeapp" ^
--data-urlencode "client_secret=abc123" ^
--data-urlencode "scope=basic email"
其他参考
- https://aaronparecki.com/oauth-2-simplified/
- Slim a micro framework for PHP
- 11 Best PHP Frameworks for Modern Web Developers in 2020
- 10 Popular PHP frameworks in 2020
- Lightweight web app server for PHP?
- Best lightweight web server (only static content) for Windows [closed]
- How can I insert a new line in a cmd.exe command?
- How can I echo a newline in a batch file?
- OAuth Libraries for PHP
其他Oauth相关的软件:
- https://github.com/ory/hydra
- https://github.com/oauthjs/node-oauth2-server
- https://github.com/simov/grant
- https://github.com/IdentityServer/IdentityServer4
- https://github.com/dexidp/dex
- https://github.com/ory/fosite
类似的Python库:
- https://authlib.org/, Authlib: Python Authentication¶, Announcement of Authlib
- https://github.com/oauthlib/oauthlib
- Flask-oauthlib or authlib in production?
- https://oauth.net/code/python/
其他PHP框架
- PHP Laravel VS Symfony: A Detailed Comparison of Web Development Frameworks
- https://github.com/laravel/laravel
- https://github.com/symfony/symfony
- https://github.com/bcit-ci/CodeIgniter
- https://github.com/sebastianbergmann/phpunit
- https://github.com/yiisoft/yii2
- https://github.com/slimphp/Slim
- https://github.com/phalcon/cphalcon
- https://github.com/mockery/mockery
(完)