XXXXX
C/C++
XXXXX
C#/.net
XXXXX
js
java
java
开发导航 开发导航 www.endv.cn
天云

OAuth2 Demo PHP

OAuth2 Demo PHP

此应用程序的目的是演示OAuth2.0客户端和服务器之间的工作流。
如果这是你第一次来这里,试图尝试的现场演示让OAuth2.0流更好的感觉。

 experimenting with the live demo 

这个图书馆是oauth2服务器运行PHP库。

安装

使用 Composer 安装这个应用程序:

$ git clone git://github.com/bshaffer/oauth2-demo-php.git
$ cd oauth2-demo-php
$ curl -s http://getcomposer.org/installer | php
$ ./composer.phar install

 

WebHost Configuration

高手写的配置

配置一个Web服务器

Silex requires you to configure your web server to run it.

Be sure to run the command $ chmod -R 777 data/ in the project root so that the web server can create the sqlite file.

使用PHP的内置Web服务器

您可以使用php的 内置的web服务器 然而,您将需要旋转两个实例,指定其中的一个 数据/ parameters.json 为了防止服务器锁定。 客户端向服务器发出请求,因为PHP的内置web服务器是单线程的,这将导致死锁。

$ cd oauth2-demo-php
$ cp data/parameters.json.dist data/parameters.json
$ sed -i '' 's?"grant"?"http://localhost:8081/lockdin/token"?g' data/parameters.json
$ sed -i '' 's?"access"?"http://localhost:8081/lockdin/resource"?g' data/parameters.json

 

现在你要做的就是自旋向上两个单独的web服务器web目录

$ cd web
$ php -S localhost:8080 & php -S localhost:8081

 

浏览到 http://localhost:8080 在您的浏览器中,你都准备好了!

这个程序是做什么的? ?

这个应用程序模拟之间的交互OAuth2客户机(演示应用程序)和OAuth2服务器(锁)。 首先,访问演示应用程序的主页:

Demo Application Homepage

点击 授权  Authorize 将你锁会,模拟数据提供商(如twitter、facebook等)。 锁会在假定您已经签署,并要求如果你想演示应用程序授予访问您的信息:

Lock'd In Authorization Request

 

一旦你点击 是的,我批准这个请求 ,您将被重定向回和一个演示应用程序 授权代码 ,这 然后客户端交流 the client then exchanges 对于一个访问令牌。 演示应用程序然后让另一个调用锁会在api和使用访问令牌代表你检索数据。

如果成功,您的数据从锁在最后一页将显示:

 

Demo Application Granted

OAuth2客户端可以用来测试 任何 OAuth2.0服务器,可以配置为使用配置文件定义 下面 

OAuth2服务器

OAuth2服务器 创建 (见 设置 方法),然后使用 控制器类 以下哪一个端点实现:

  • /授权 authorize——授予演示应用程序一个端点 授权代码
  • /令牌 token——授予演示应用程序一个端点 access_token 当提供上述授权代码
  • /资源 resource——端点赠款演示应用程序访问受保护的资源(在这种情况下,你的朋友)当提供上面的访问令牌

这三个OAuth2服务器的主要功能(授权用户,授予用户令牌,并验证api调用)。 当你写OAuth2-compatible服务器,你的界面会是相似的。

注:以上url前缀 /服务器 名称空间的应用程序。

 

Note: the above urls are prefixed with /server to namespace the application.  注:以上url前缀 /服务器 名称空间的应用程序。

Test Your Own OAuth2 Server!

测试自己的OAuth2服务器!

您可以测试这个应用程序轻松地对自己的OAuth应用程序。 复制的 parameters.json.dist 文件 parameters.json :

$ cd /path/to/oauth2-demo-php
$ cp data/parameters.json.dist data/parameters.json

打开参数。 json文件,注意默认配置:

{
  "client_id": "demoapp",
  "client_secret": "demopass",
  "token_route": "grant",
  "authorize_route": "authorize",
  "resource_route": "access",
  "resource_method": "GET",
  "resource_params": {},
  "curl_options": {}
}

这是默认的配置 就锁在 OAuth2服务器。 对自己的测试,改变这些参数以适应api服务器你想测试:

{
  "client_id": "OAuth Demo Application",
  "client_secret": "a3b4b74330724a927bec",
  "token_route": "https://api.myapp.com/token",
  "authorize_route": "https://myapp.com/authorize",
  "resource_route": "https://api.myapp.com/profile",
  "resource_method": "POST",
  "resource_params": { "debug": true },
  "curl_options": { "http_port": 443, "verifyssl": false }
}

上面的例子使用了一个新客户对一个虚构的oauth身份验证服务器 myapp.com 。 这是测试您的应用程序在生产时非常有用

Note: The curl options are set to ignore an SSL certificate, and the resource_params define a fictional debug parameter. These are not required for your APIs, but is meant as an example what can be done with the configuration

注:卷曲的选项设置忽略SSL证书,和resource_params定义一个虚构的调试参数。这些不是您的API所必需的,但也可以作为配置的一个例子。

###Test多环境测试

此外,您可以使用 parameters.json 文件创建多个环境,和它们之间的切换:

{
    "LockdIn": {
      "client_id": "demoapp",
      "client_secret": "demopass",
      "token_route": "grant",
      "authorize_route": "authorize",
      "resource_route": "access",
      "resource_method": "GET",
      "resource_params": {},
      "curl_options": {}
    },
    "My App": {
      "client_id": "OAuth Demo Application",
      "client_secret": "a3b4b74330724a927bec",
      "token_route": "https://api.myapp.com/token",
      "authorize_route": "https://myapp.com/authorize",
      "resource_route": "https://api.myapp.com/profile",
      "resource_method": "POST",
      "resource_params": { "debug": true },
      "curl_options": { "http_port": 443, "verifyssl": false }
    }
}

这将在顶部,这将允许你切换环境和测试多个OAuth服务器提供一个下拉

Demo Application With Environment Select

Contact

Please contact Brent Shaffer (77811970<at> qq<dot> com) for more information

posted @ 2018-01-30 01:26  Endv  阅读(444)  评论(0编辑  收藏  举报