百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
Y

yii2-oauth2-server

> 编程语言
开源

用于实现 OAuth2 服务器的封装(https://GitHub.com/bshaffer/oauth2-server-php)

331 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

用于实现 OAuth2 服务器的封装(https://GitHub.com/bshaffer/oauth2-server-php)

yii2-oauth2-server

A wrapper for implementing an OAuth2 Server(https://github.com/bshaffer/oauth2-server-php)

Installation

The preferred way to install this extension is through composer.

Either run

bash
php composer.phar require --prefer-dist filsh/yii2-oauth2-server "*"

or add

json
"filsh/yii2-oauth2-server": "^2.0"

to the require section of your composer.json.

To use this extension, simply add the following code in your application configuration:

…

common\models\User - user model implementing an interface \OAuth2\Storage\UserCredentialsInterface, so the oauth2 credentials data stored in user table

The next step you should run migration

php
yii migrate --migrationPath=@vendor/filsh/yii2-oauth2-server/src/migrations

this migration creates the oauth2 database scheme and insert test user credentials testclient:testpass for http://fake/

add url rule to urlManager

php
'urlManager' => [
    'rules' => [
        'POST oauth2/' => 'oauth2/rest/',
        ...
    ]
]

Configuration

You can pass additional OAuth2 Server options by setting options property on the module. These options configure as the underlying OAuth2 Server also as various parts/components of bshaffer/oauth2-server-php. As an example, you can configure authorization code lifetime in a response by setting auth_code_lifetime option. Some of them are implemented as standalone properties on the module: tokenParamName => use_jwt_access_tokens, tokenAccessLifetime => token_param_name, useJwtToken => access_lifetime. Full list of options are supported by the underlying OAuth2 Server main component - source code. Options for various components spread across bshaffer/oauth2-server-php source code.

Usage

To use this extension, simply add the behaviors for your base controller:

…

Create action authorize in site controller for Authorization Code

https://api.mysite.com/authorize?response_type=code&client_id=TestClient&redirect_uri=https://fake/

see more

…

Also, if you set allow_implicit => true in the options property of the module, you can use Implicit Grant Type - see more

Request example:

https://api.mysite.com/authorize?response_type=token&client_id=TestClient&redirect_uri=https://fake/cb

With redirect response:

https://fake/cb#access_token=2YotnFZFEjr1zCsicMWpAA&state=xyz&token_type=bearer&expires_in=3600

JWT Tokens

If you want to get Json Web Token (JWT) instead of conventional token, you will need to set 'useJwtToken' => true in module and then define two more configurations: 'public_key' => 'app\storage\PublicKeyStorage' which is the class that implements PublickKeyInterface and 'access_token' => 'OAuth2\Storage\JwtAccessToken' which implements JwtAccessTokenInterface.php

For Oauth2 base library provides the default access_token which works great except. Just use it and everything will be fine.

and public_key

php
pvk =  file_get_contents('privkey.pem', true);
        $this->pbk =  file_get_contents('pubkey.pem', true); 
    }

    public function getPublicKey($client_id = null){ 
        return  $this->pbk;
    }

    public function getPrivateKey($client_id = null){ 
        return  $this->pvk;
    }

    public function getEncryptionAlgorithm($client_id = null){
        return 'RS256';
    }

}

For more, see https://github.com/bshaffer/oauth2-server-php

Authors & Contributors

The original author of this package Igor Maliy . At the time the project maintainer is Vardan Pogosian.

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

PHP

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言