renanivo/authoritarian

支持多种授权流程的PHP OAuth 2 客户端

0.0.3 2013-11-19 19:47 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:03:30 UTC


README

Build Status Code Climate Test Coverage

支持多种授权流程的PHP OAuth 2 客户端

安装

获取 composer 并执行

php composer.phar require renanivo/authoritarian

用法

只需设置您的流程并请求访问令牌

客户端凭证流程

<?php
use Authoritarian\OAuth2;
use Authoritarian\Flow\ClientCredentialsFlow;

$flow = new ClientCredentialsFlow();
$flow->setClientCredential('client id', 'client secret');

$oauth2 = new OAuth2('http://example.com/oauth/token');
$token = $oauth2->requestAccessToken($flow)->json();

授权码流程

在登录页面

<?php
use Authoritarian\OAuth2;
use Authoritarian\Flow\AuthorizationCodeFlow;

$flow = new AuthorizationCodeFlow();
$flow->setAuthorizationUrl('http://example.com/oauth/authorize');
$flow->setClientCredential('client id', 'client secret');
$flow->setRedirectUri('http://example.com/callback');

header('Location: ' . $flow->getAuthUrl());

在回调页面

<?php
use Authoritarian\OAuth2;
use Authoritarian\Flow\AuthorizationCodeFlow;

$flow = new AuthorizationCodeFlow();
$flow->setClientCredential('client id', 'client secret');
$flow->setCode($_GET['code']);

$oauth2 = new OAuth2('http://example.com/oauth/token');
$token = $oauth2->requestAccessToken($flow)->json();

资源所有者密码

<?php
use Authoritarian\OAuth2;
use Authoritarian\Flow\ResourceOwnerPasswordFlow;

$flow = new ResourceOwnerPasswordFlow(
    'username',
    'password'
);
$flow->setClientCredential('client id', 'client secret');

$oauth2 = new OAuth2('http://example.com/oauth/token');
$token = $oauth2->requestAccessToken($flow)->json();

生成文档

  • 下载 apigen.phar

    curl -sS http://apigen.org/installer | php
  • 运行 ApiGen

    php apigen.phar generate