OAuth 2.0 Server PHP实现示例
需求
实现三方OAuth2.0授权登录
使用OAuth服务
OAuth 2.0 Server PHP
环境
nginx
mysql
php
框架
Yii
一 安装
项目目录下安装应用
composer.phar require bshaffer/oauth2-server-php "^1.10"
二 构建数据结构
注意 user表需要自定义
CREATE TABLE oauth_clients (
client_id VARCHAR(80) NOT NULL,
client_secret VARCHAR(80),
redirect_uri VARCHAR(2000),
grant_types VARCHAR(80),
scope VARCHAR(4000),
user_id VARCHAR(80),
PRIMARY KEY (client_id)
); CREATE TABLE oauth_access_tokens (
access_token VARCHAR(40) NOT NULL,
client_id VARCHAR(80) NOT NULL,
user_id VARCHAR(80),
expires TIMESTAMP NOT NULL,
scope VARCHAR(4000),
PRIMARY KEY (access_token)
); CREATE TABLE oauth_authorization_codes (
authorization_code VARCHAR(40) NOT NULL,
client_id VARCHAR(80) NOT NULL,
user_id VARCHAR(80),
redirect_uri VARCHAR(2000),
expires TIMESTAMP NOT NULL,
scope VARCHAR(4000),
id_token VARCHAR(1000),
PRIMARY KEY (authorization_code)
); CREATE TABLE oauth_refresh_tokens (
refresh_token VARCHAR(40) NOT NULL,
client_id VARCHAR(80) NOT NULL,
user_id VARCHAR(80),
expires TIMESTAMP NOT NULL,
scope VARCHAR(4000),
PRIMARY KEY (refresh_token)
); CREATE TABLE oauth_users (
username VARCHAR(80),
password VARCHAR(80),
first_name VARCHAR(80),
last_name VARCHAR(80),
email VARCHAR(80),
email_verified BOOLEAN,
scope VARCHAR(4000),
PRIMARY KEY (username)
); CREATE TABLE oauth_scopes (
scope VARCHAR(80) NOT NULL,
is_default BOOLEAN,
PRIMARY KEY (scope)
); CREATE TABLE oauth_jwt (
client_id VARCHAR(80) NOT NULL,
subject VARCHAR(80),
public_key VARCHAR(2000) NOT NULL
);
三 代码实现
<?php
/**
* Created by PhpStorm.
* User: parker
* Date: 2020/9/8
* Time: 7:08 下午
*
* 使用 OAuth 2.0 Server PHP 搭建三方授权服务
* 相关文档地址 https://bshaffer.github.io/oauth2-server-php-docs/
* 本服务使用 授权码形式
* 授权码(authorization code)方式,指的是第三方应用先申请一个授权码,然后再用该码获取令牌。
* 这种方式是最常用的流程,安全性也最高,它适用于那些有后端的 Web 应用。授权码通过前端传送,令牌则是储存在后端,而且所有与资源服务器的通信都在后端完成。这样的前后端分离,可以避免令牌泄漏。
*
*/ namespace backend\controllers\api; use common\lib\LController;
use common\lib\LError;
use common\models\admin\AdminModel;
use OAuth2\GrantType\AuthorizationCode; use OAuth2\Request;
use OAuth2\Response;
use OAuth2\Server;
use OAuth2\Storage\Pdo;
use yii\helpers\Json;
use Yii; class OauthController extends LController
{ public $enableCsrfValidation = false;
/** @var Pdo $storage */
public $storage;
/** @var Server $server */
public $server;
/** @var Request $request */
public $request;
/** @var Response $response */
public $response; public function init()
{ $this->storage = new Pdo([
'dsn' => 'mysql:host=10.0.80.10;dbname=ylsrc_admin',
'username' => 'root',
'password' => '2Q5@a5X6fh'
], [
'client_table' => 'src_oauth_clients',
'access_token_table' => 'src_oauth_access_tokens',
'refresh_token_table' => 'src_oauth_refresh_tokens',
'code_table' => 'src_oauth_authorization_codes',
'user_table' => 'src_admin',
'scope_table' => 'src_oauth_scopes',
'public_key_table' => 'src_oauth_public_keys',
]);
$this->server = new Server($this->storage);
$this->request = Request::createFromGlobals();
$this->response = new Response();
} /**
* 获取授权码(测试使用生产环境服务添加到登录接口)
*
* 获取链接
* GET http://usrc.com/api/oauth/get-code?response_type=code&client_id=1&state=xyz
* response_type 必填 请求类型
* client_id 必填 三方授权id
* state 必填 回调验证字段
*
* 响应方式为跳转到src_oauth_clients表对应id条目设置的redirect_url跳转连接返回第三方应用, 并且携带code
* 示例:对应的redirect_url为 https://host.com 那么对应的跳转连接为(state未使用)
* https://host.com?code=d53d363349951c29593b722a7d2fb05c054f5e65&state=xyz
*
*/
public function actionGetCode()
{
$uid = Yii::$app->user->id;
if($uid){
$this->server->addGrantType(new AuthorizationCode($this->storage)); // or any grant type you like!
$this->server->validateAuthorizeRequest($this->request, $this->response);
$this->server->handleAuthorizeRequest($this->request, $this->response, true, $uid);
$this->response->send();
}else{
$this->ajaxReturn( LError::NO_PERMISSION, LError::getErrMsgByCode( LError::NO_PERMISSION ), [] );
} } /**
* 获取access_toke
* 获取链接
* POST http://usrc.com/api/oauth/get-token
* data
* grant_type 必填 请求类型 authorization_code
* client_id 必填 三方授权id
* client_secret 必填 三方授权秘钥
* code 必填 上一步获取的授权码
*
* return
*
* {
* "access_token": "b2d91c2764bdde79e4f1e92349b969e8ee031e8a",
* "expires_in": 3600,
* "token_type": "Bearer",
* "scope": null,
* "refresh_token": "d9293e82c22523ad6500d6b584b484cc3aeb4736"
* }
*/
public function actionGetToken()
{
$response = $this->server->handleTokenRequest($this->request);
$response->send();
} /**
* 使用token获取用户信息
* 获取连接
* GET http://usrc.com/api/oauth/get-user-info?access_token=b2d91c2764bdde79e4f1e92349b969e8ee031e8a
*
* access_token 上一步获取的token
*
*/
public function actionGetUserInfo()
{
if (!$this->server->verifyResourceRequest($this->request)) {
$this->server->getResponse()->send();
}else{
$token = $this->server->getAccessTokenData($this->request);
$user = AdminModel::findIdentity($token['user_id']);
$data = [
'id' => $user->id,
'username' => $user->account_name,
'mobile' => $user->mobile,
'email' => $user->email,
]; echo Json::encode($data);
} }
}
参考文献
OAuth2.0 官方文档 https://oauth.net/2/
OAuth 2.0 Server PHP 官方文档 https://bshaffer.github.io/oauth2-server-php-docs/
OAuth2.0的一个简单解释 阮一峰 http://www.ruanyifeng.com/blog/2019/04/oauth_design.html
OAuth2.0的四种方式 阮一峰 http://www.ruanyifeng.com/blog/2019/04/oauth-grant-types.html
OAuth 2.0 Server PHP实现示例的更多相关文章
- PHP OAuth 2.0 Server
PHP OAuth 2.0 Server PHP OAuth 2.0 Server ⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️⚠️ ...
- 每周开源项目分享-年轻人的第一个OAuth2.0 Server:hydra
年轻人的第一个OAuth2.0 Server:hydra hydra 是什么呢? OpenID Connect certified OAuth2 Server - cloud native, secu ...
- Identity Server 4 预备知识 -- OAuth 2.0 简介
OAuth 2.0 简介 OAuth有一些定义: OAuth 2.0是一个委托协议, 它可以让那些控制资源的人允许某个应用以代表他们来访问他们控制的资源, 注意是代表这些人, 而不是假冒或模仿这些人. ...
- 【实例图文详解】OAuth 2.0 for Web Server Applications
原文链接:http://blog.csdn.net/hjun01/article/details/42032841 OAuth 2.0 for Web Server Applicatio ...
- Identity Server 4 原理和实战(完结)_----选看 OAuth 2.0 简介(上)
https://www.yuque.com/yuejiangliu/dotnet/cg95ni 代表资源所有者的凭据 授权 Authorization Grant 授权是一个代表着资源所有者权限的凭据 ...
- ASP.NET WebApi OWIN 实现 OAuth 2.0
OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用. OAuth 允许用户提供一个令牌, ...
- [转]OAuth 2.0 - Authorization Code授权方式详解
本文转自:http://www.cnblogs.com/highend/archive/2012/07/06/oautn2_authorization_code.html I:OAuth 2.0 开发 ...
- oAuth 2.0 笔记
OAuth 2.0规范于2012年发布,很多大型互联网公司(比如:微信.微博.支付宝)对外提供的SDK中,授权部分基本上都是按这个规范来实现的. OAuth 2.0提供了4种基本的标准授权流程,最为复 ...
- NET WebApi OWIN 实现 OAuth 2.0
NET WebApi OWIN 实现 OAuth 2.0 OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和 ...
随机推荐
- 网络协议HTTP、TCP/IP、Socket
网络协议HTTP.TCP/IP.Socket 网络七层由下往上分别为物理层.数据链路层.网络层.传输层.会话层.表示层和应用层. 其中物理层.数据链路层和网络层通常被称作媒体层,是网络工程师所研究的 ...
- 官网安装Python包太慢?教你三种下载安装方式-PiP、conda、轮子,教你三种Pytorch的下载安装方式,保证你再也不用出现Error
上一期我们介绍了CUDA下载安装以及其总结,这一期教大家如何在Anaconda中使用CUDA来进行加速.神经网络依赖cuDNN的下载安装,以及下载和安装Pytorch-GPU安装包的三种方式(cond ...
- window.location.href跳转无效
window.location.href跳转无效 问题情况 JS中设置window.location.href跳转无效 原因是 a标签的href跳转会执行在window.location. ...
- AWD攻防技战法
round1 弱口令 cat /etc/passwd 查看用户信息 修改用户密码(passwd username) 通过ssh弱口令批量getshell (通过msf的auxiliary/sca ...
- HTML绘制表格
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 虚虚实实,亦假亦真的 ValueTuple,绝对能眩晕你
一:背景 1. 讲故事 前几天在写一个api接口,需要对衣物表进行分页查询,查询的output需要返回两个信息,一个是 totalCount,一个是 clothesList,在以前我可能需要封装一个 ...
- Catalina 动态壁纸相关设置
关闭SIP 重启,在开机时一直按Command+r进入recovery模式. 打开终端,如图所示: 在终端中输入命令,回车: csrutil disable 然后重启 设置动态壁纸 首先需在Dynam ...
- 二、Git安装--Windows
Git安装配置 在使用Git前我们需要先安装 Git.Git 目前支持 Linux/Unix.Solaris.Mac和 Windows 平台上运行. Git 各平台安装包下载地址为:http://gi ...
- netty学习心得1
也不是系统学习,工作需求,一点点抠的,需要自己笼统学习下. 首先功能实现: serverBootstrap.group(boss, work).channel(NioServerSocketChann ...
- kali学习wiki
https://github.com/Keybird0/Kali-learning-notes/wiki