Download the PHP package sweelix/yii2-oauth2-server without Composer
On this page you can find all versions of the php package sweelix/yii2-oauth2-server. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sweelix/yii2-oauth2-server
More information about sweelix/yii2-oauth2-server
Files in sweelix/yii2-oauth2-server
Package yii2-oauth2-server
Short Description PHP 5.6+ Oauth2 server integration for the Yii framework
License BSD-3-Clause
Informations about the package yii2-oauth2-server
Oauth2 Yii2 integration
This extension allow the developper to use Oauth2 server.
Installation
If you use Packagist for installing packages, then you can update your composer.json like this :
How to use it
Add extension to your configuration :
You also need to enable PrettyUrl:
Migrations (MySql only)
All the migrations needed can be found inside src/migrations. Be sure to configure the database connection before applying them.
Grant types
You can find examples and explanations about every grant types here and here.
For the Jwt Bearer Grant, you will need to create a Jwt entry in your database for the given client and subject.
Configure Module
Basic module parameters
backend
: can be redis or mysqldb
: id of the component or connection or connection configurationidentityClass
: user class used to link oauth2 authorization system default to user componentidentityClass
webUserParamId
: allow separation between main app user (session) and module app user, (default to __oauth2)identityCookieName
: allow separation between main app user (cookie) and module app user, (default to oauth2)webUser
: allow full management of module web user, (default to [])baseEndPoint
: base path for token and authorize endpoints default to''
- Token endpoint https://host.xxx/token
- Authorize endpoint https://host.xxx/authorize
overrideLayout
: override module layout to use another one (ex: @app/views/layouts/oauth2)overrideViewPath
: override view path to use specific one (ex: @app/views/oauth2)
Grants management
allowImplicit
: allow implicit grant (default to false)allowAuthorizationCode
: allow authorization code grant (default to true)allowClientCredentials
: allow client credentials grant (default to true)allowPassword
: allow user credentials / password grant (default to true)allowCredentialsInRequestBody
: allow credentials in request body (default to true)allowPublicClients
: allow public clients (default to true)alwaysIssueNewRefreshToken
: always issue refresh token (default to true)unsetRefreshTokenAfterUse
: unset refresh token after use (default to true)
JWT parameters (:warning: Not sure about the implementation. Use at your own risk !)
useJwtAccessToken
: send access tokens as JWT (default : false)allowAlgorithm
: available algorithm for JWT (default : ['RS256', 'RS384', 'RS512'])jwtAudience
: default to token endpointstoreEncryptedTokenString
: store encrypted token (default : true)
Time To Live
idTTL
: TTL of ID Token (default to 3600)accessTokenTTL
: TTL of access token (default to 3600)refreshTokenTTL
: TTL of refresh token (default to 14 24 3600)
Basic Oauth names
realm
: Realm value (default to Service)tokenQueryName
: name of the access token parameter (default to access_token)tokenBearerName
: name of authorization header (default to Bearer)
Enforce parameters
enforceState
: enforce state parameter (default to true)allowOnlyRedirectUri
: need exact redirect URI (default to true)
OpenID
allowOpenIdConnect
: enable openId connect (default : false) // not implemented yet
Authorization Code parameters
enforceRedirect
: enforce redirect parameter (default to false)authorizationCodeTTL
: TTL of authorization code (default to 30)
CORS
cors
: enableCORS
on the token endpoint (default : false) the CORS part can be defined using an array as described in Yii documentation
User identity and Web user
Configure the user component to link oauth2 system and user / identity management
IdentityClass
must implements sweelix\oauth2\server\interfaces\UserModelInterface
. You can use the trait
sweelix\oauth2\server\traits\IdentityTrait
to automagically implement
public function getRestrictedScopes()
public function setRestrictedScopes($scopes)
public static function findIdentityByAccessToken($token, $type = null)
you will have to implement the remaining methods :
public static function findByUsernameAndPassword($username, $password)
public static function findByUsername($username)
Creating specific view for OAuth2
In order to use your own views (instead of the builtin ones), you can override
layout
: module parameteroverrideLayout
viewPath
: module parameteroverrideViewPath
Overriding layout
You should create a classic layout like :
and link it to the module
Overriding views
You should create 3 views to allow oauth2 module to work as expected and link them to the module
Error view
This view is used to display a page when an error occurs
Login view
This view is used to display a login page when needed
Authorize view
This view is used to display an authorization page when needed
Exposed Models overview
The Oauth2 Yii2 extension expose severall models which can be used in your application. All models can be overloaded using Yii2 DI.
For example, if you want to overload the Client
model, you have to inject your own model in the DI using:
Client / ClientModelInterface
Client::findOne($id)
- Find client by IDClient::findAllByUserId($id)
- Find all clients accepted by user (userId)Client::findAll()
- Find all existing clients$client->save()
- Save client$client->delete()
- Delete client$client->hasUser($userId)
- Check if user (userId) has accepted the client$client->addUser($userId)
- Attach the user (userId) to the client$client->removeUser($userId)
- Dettach the user (userId) from the client
AccessToken / AccessTokenModelInterface
AccessToken::findOne($id)
- Find accessToken by IDAccessToken::findAllByUserId($id)
- Find all accessTokens for user (userId)AccessToken::findAllByClientId($id)
- Find all accessTokens for client (clientId)AccessToken::deleteAllByUserId($id)
- Delete all accessTokens for user (userId)AccessToken::deleteAllByClientId($id)
- Delete all accessTokens for client (clientId)AccessToken::findAll()
- Find all existing accessTokensAccessToken::deleteAllExpired()
- Delete all expired accessTokens$accessToken->save()
- Save accessToken$accessToken->delete()
- Delete accessToken
RefreshToken / RefreshTokenModelInterface
RefreshToken::findOne($id)
- Find accessToken by IDRefreshToken::findAllByUserId($id)
- Find all refreshTokens for user (userId)RefreshToken::findAllByClientId($id)
- Find all refreshTokens for client (clientId)RefreshToken::deleteAllByUserId($id)
- Delete all refreshTokens for user (userId)RefreshToken::deleteAllByClientId($id)
- Delete all refreshTokens for client (clientId)RefreshToken::findAll()
- Find all existing refreshTokensRefreshToken::deleteAllExpired()
- Delete all expired refreshTokens$refreshToken->save()
- Save refreshToken$refreshToken->delete()
- Delete refreshToken
AuthCode / AuthCodeModelInterface
AuthCode::findOne($id)
- Find authCode by ID$authCode->save()
- Save authCode$authCode->delete()
- Delete authCode
Scope / ScopeModelInterface
Scope::findOne($id)
- Find scope by IDScope::findAvailableScopeIds()
- Find all scopes IDsScope::findDefaultScopeIds()
- Find default scopes IDs$scope->save()
- Save scope$scope->delete()
- Delete scope
CypherKey / CypherKeyModelInterface
CypherKey::findOne($id)
- Find cypherKey by ID$cypherKey->save()
- Save cypherKey$cypherKey->delete()
- Delete cypherKey$cypherKey->generateKeys()
- Generate random keys for current cypherKey
Jti / JtiModelInterface (:warning: Not sure about the implementation. Use at your own risk !)
Jti::findOne($id)
- Find jti by IDJti::findAllBySubject($id)
- Find all jtis for user (userId)Jti::findAllByClientId($id)
- Find all jtis for client (clientId)Jti::deleteAllBySubject($id)
- Delete all jtis for user (userId)Jti::deleteAllByClientId($id)
- Delete all jtis for client (clientId)Jti::findAll()
- Find all existing jtisJti::deleteAllExpired()
- Delete all expired jtisJti::getFingerprint($clientId, $subject, $audience, $expires, $jti)
- Get a jti fingerprint for given params$jti->save()
- Save jti$jti->delete()
- Delete jti
Jwt / JwtModelInterface (:warning: Not sure about the implementation. Use at your own risk !)
Jwt::findOne($id)
- Find jwt by IDJwt::getFingerprint($clientId, $subject)
- Get jwt fingerprint for given clientId and subject$jwt->save()
- Save jwt$jwt->delete()
- Delete jwt
Linking RBAC and Scope systems
Using sweelix\oauth2\server\web\User
class will automagically link rbac
system and oauth2
system.
Permission system will be slightly modified to allow fine grained checks :
-
Yii::$app->user->can('read')
will check- if scope
read
is allowed for current client - if rbac permission
read
is allowed for current user
- if scope
-
Yii::$app->user->can('rbac:read')
will check only if rbac permissionread
is allowed for current user Yii::$app->user->can('oauth2:read')
will check only if scoperead
is allowed for current client
Running the tests
Before running the tests, you should edit the file tests/config/BACKEND.php and change the config to match your environment.
CLI System
Several commands are available to manage oauth2 system
php protected/yii.php oauth2:client/create
php protected/yii.php oauth2:client/update
php protected/yii.php oauth2:client/delete
php protected/yii.php oauth2:jwt/create
php protected/yii.php oauth2:jwt/update
php protected/yii.php oauth2:jwt/delete
php protected/yii.php oauth2:key/create
php protected/yii.php oauth2:key/update
php protected/yii.php oauth2:key/delete
php protected/yii.php oauth2:scope/create
php protected/yii.php oauth2:scope/update
php protected/yii.php oauth2:scope/delete
php protected/yii.php oauth2:cronjob/remove-expired
- Run this one with your cron managerphp protected/yii.php oauth2:migrate-redis/migrate
- Migration command for redis
All versions of yii2-oauth2-server with dependencies
ext-openssl Version *
yiisoft/yii2 Version ~2.0.13
yiisoft/yii2-redis Version ~2.0
bshaffer/oauth2-server-php Version ~1.8
yiisoft/yii2-bootstrap Version ~2.0