Simple case of building oau2tp based on TP and case study of building oauth2tp _ PHP

Source: Internet
Author: User
Tags autoloader oauth
A simple case of building oau2tp based on TP and a case of building oauth2tp. Oau2auth is a simple case based on TP. oauth2tp is an open network standard for authorization. oauth2auth is a simple case based on TP, oauth2tp build case

Notice: understanding oau22.

OAuth is an open network standard for authorization. it is widely used all over the world. The current version is version 2.0. Today, I tried to set up the environment here as a learning record;

Reference Source:

Http://oauth.net/2/

Http://bshaffer.github.io/oauth2-server-php-docs/cookbook/

Prepare Data tables:

---- TABLE structure 'Oss _ access_tokens' -- create table if not exists 'Oss _ 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; -- the structure of the authorization TABLE 'Oss _ authorization_codes '-- create table if not exists 'Oss _ authorization_codes' ('authorization _ code' text, 'Client _ id' text, 'user _ id' text, 'redirect _ url' text, 'expires' timestamp not null default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, 'scope 'text, 'id _ token' text) ENGINE = InnoDB default charset = utf8; -- the structure of the tables TABLE 'Oss _ clients '-- create table if not exists 'Oss _ clients' ('Client _ id' text, 'client _ secret' text, 'redirect _ url' text) ENGINE = InnoDB default charset = utf8; ---- data in the table 'Oss _ clients '-- insert into 'Oss _ clients' ('Client _ id', 'Client _ secret', 'redirect _ uri ') VALUES ('demoapp ', 'demopass ',' http://127.0.0.1/tp/index.php '); -- Struct TABLE structure 'Oss _ public_keys' -- create table if not exists 'Oss _ 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; -- structure of the tables TABLE 'Oss _ refresh_tokens' -- create table if not exists 'Oss _ 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; -- users TABLE structure 'Oss _ scopes '-- create table if not exists 'Oss _ scopes' ('process' text, 'is _ default' tinyint (1) default NULL) ENGINE = InnoDB default charset = utf8; -- struct TABLE structure 'Oss _ users' -- create table if not exists 'Oss _ 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 'Oss _ users' -- alter table 'Oss _ users' add primary key ('Username ');

Database address: https://github.com/bshaffer/oauth2-server-php

Here I put it in the Vendor/oau2;

Authorization request class:

 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();    }}

The request of the oau2store is encapsulated in Org/oau2;

 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();    }}

  

Test class:

  true, 'message' => 'You accessed my APIs!'));    }    public function getToken()    {        echo json_encode(['token' => $this->tokenData]);    }}

Configuration File:

Require_once (VENDOR_PATH. '/oau2/ Autoloader. php '); oau2\ Autoloader: register (); return array (// 'config maps '=> 'configuration value' AUTOLOAD _ NAMESPACE' => array ('Oss _ 2' => VENDOR_PATH. 'Oss/'), // List of extended modules 'dsn' => 'MySQL: host = localhost; dbname = os22', 'username' => 'root ', 'password' => '',);

  

Http://www.bkjia.com/PHPjc/1114012.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1114012.htmlTechArticleOAuth2 is based on TP to build a simple case, oauth2tp to build a case read note: Understanding oau2oauth is an open network standard on authorization, has been widely used in the world...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.