oauth2 基于tp 搭建简单案例,oauth2tp搭建案例阅读须知:理解oauth2
oauth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版。今天就试着把环境搭建一下在此仅作为学习记录;
参考资料来源:
http://oauth.net/2/
http://bshaffer.github.io/oauth2-server-php-docs/cookbook/
数据表准备:
---- 表的结构 `oauth_access_tokens`--create table if not exists `oauth_access_tokens` ( `access_token` text, `client_id` text, `user_id` text, `expires` timestamp not null default current_timestamp on update current_timestamp, `scope` text) engine=innodb default charset=utf8;-- ------------------------------------------------------------ 表的结构 `oauth_authorization_codes`--create table if not exists `oauth_authorization_codes` ( `authorization_code` text, `client_id` text, `user_id` text, `redirect_uri` text, `expires` timestamp not null default current_timestamp on update current_timestamp, `scope` text, `id_token` text) engine=innodb default charset=utf8;-- ------------------------------------------------------------ 表的结构 `oauth_clients`--create table if not exists `oauth_clients` ( `client_id` text, `client_secret` text, `redirect_uri` text) engine=innodb default charset=utf8;---- 转存表中的数据 `oauth_clients`--insert into `oauth_clients` (`client_id`, `client_secret`, `redirect_uri`) values('demoapp', 'demopass', 'http://127.0.0.1/tp/index.php');-- ------------------------------------------------------------ 表的结构 `oauth_public_keys`--create table if not exists `oauth_public_keys` ( `client_id` varchar(80) default null, `public_key` varchar(8000) default null, `private_key` varchar(8000) default null, `encryption_algorithm` varchar(80) default 'rs256') engine=innodb default charset=utf8;-- ------------------------------------------------------------ 表的结构 `oauth_refresh_tokens`--create table if not exists `oauth_refresh_tokens` ( `refresh_token` text, `client_id` text, `user_id` text, `expires` timestamp not null default current_timestamp on update current_timestamp, `scope` text) engine=innodb default charset=utf8;-- ------------------------------------------------------------ 表的结构 `oauth_scopes`--create table if not exists `oauth_scopes` ( `scope` text, `is_default` tinyint(1) default null) engine=innodb default charset=utf8;-- ------------------------------------------------------------ 表的结构 `oauth_users`--create table if not exists `oauth_users` ( `username` varchar(255) not null, `password` varchar(2000) default null, `first_name` varchar(255) default null, `last_name` varchar(255) default null) engine=innodb default charset=utf8;---- indexes for table `oauth_users`--alter table `oauth_users` add primary key (`username`);
oauth2 库地址:https://github.com/bshaffer/oauth2-server-php
这里我把它放在vendor/oauth2里;
授权请求类:
oauth_server->validateauthorizerequest($this->oauth_request, $this->oauth_response)) { $this->oauth_response->send(); die; }// print the authorization code if the user has authorized your client $this->oauth_server->handleauthorizerequest($this->oauth_request, $this->oauth_response, true); // this is only here so that you get to see your code in the curl request. otherwise, we'd redirect back to the client $code = substr($this->oauth_response->gethttpheader('location'), strpos($this->oauth_response->gethttpheader('location'), 'code=') + 5, 40); echo json_encode(['code' => $code]); //$this->oauth_response->send(); } public function token() { $this->oauth_server->handletokenrequest(\oauth2\request::createfromglobals())->send(); }}
oauth2 库的请求封装放在:org/oauth2里;
oauth_storage = new \oauth2\storage\pdo(array('dsn' => c('dsn'), 'username' => c('username'), 'password' => c('password'))); // pass a storage object or array of storage objects to the oauth2 server class $this->oauth_server = new \oauth2\server($this->oauth_storage); // add the client credentials grant type (it is the simplest of the grant types) $this->oauth_server->addgranttype(new \oauth2\granttype\clientcredentials($this->oauth_storage)); // add the authorization code grant type (this is where the oauth magic happens) $this->oauth_server->addgranttype(new \oauth2\granttype\authorizationcode($this->oauth_storage)); $this->oauth_request = \oauth2\request::createfromglobals(); $this->oauth_response = new \oauth2\response(); }}oauth_server->verifyresourcerequest(\oauth2\request::createfromglobals())) { $this->oauth_server->getresponse()->send(); die; } $this->tokendata = $this->oauth_server->getresourcecontroller()->gettoken(); }}
测试类:
true, 'message' => 'you accessed my apis!')); } public function gettoken() { echo json_encode(['token' => $this->tokendata]); }}
配置文件:
require_once(vendor_path . '/oauth2/autoloader.php');oauth2\autoloader::register();return array( //'配置项'=>'配置值' 'autoload_namespace' => array('oauth2' => vendor_path . 'oauth2/'), //扩展模块列表 'dsn' => 'mysql:host=localhost;dbname=oauth2', 'username' => 'root', 'password' => '',);
http://www.bkjia.com/phpjc/1114012.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/1114012.htmltecharticleoauth2 基于tp 搭建简单案例,oauth2tp搭建案例 阅读须知:理解oauth2 oauth 是一个关于授权( authorization )的开放网络标准,在全世界得到广泛...