zacksleo/yii2-oauth2

yii2 oauth2 module

Installs: 7 385

Dependents: 0

Suggesters: 0

Security: 0

Stars: 4

Watchers: 3

Forks: 0

Open Issues: 0

Type:yii2-extension

1.0.8 2018-11-28 10:29 UTC

This package is auto-updated.

Last update: 2024-04-08 06:09:44 UTC


README

yii2 oauth2 module based on yii2-oauth2-server

Latest Stable Version Total Downloads License StyleCI Code Climate Build Status Scrutinizer Code Quality Code Coverage Build Status

Install

composer install zacksleo/yii2-oauth2

Migration Database

./yii migrate --migrationPath=@vendor/zacksleo/yii2-oauth2/migrations

Config module

for API or frontend

 'modules' => [
     'oauth2' => [
         'class' => 'filsh\yii2\oauth2server\Module',
         /* Fix Yii2 2.0.13+ Incompatible Issue
          * @see https://github.com/Filsh/yii2-oauth2-server/issues/132
          */
         'components' => [
            'request' => function () {
                return \filsh\yii2\oauth2server\Request::createFromGlobals();
            },
            'response' => [
                'class' => \filsh\yii2\oauth2server\Response::class,
            ],
         ],
         'tokenParamName' => 'access_token',
         'tokenAccessLifetime' => 3600 * 24 * 7,
         'storageMap' => [
             'user_credentials' => 'common\models\User',
             'access_token' => 'zacksleo\yii2\oauth2\common\models\storage\AccessToken',
         ],
         'grantTypes' => [
             'client_credentials' => [
                 'class' => 'OAuth2\GrantType\ClientCredentials',
                 'allow_public_clients' => false
             ],
             'user_credentials' => [
                 'class' => 'OAuth2\GrantType\UserCredentials',
             ],
             'refresh_token' => [
                 'class' => 'OAuth2\GrantType\RefreshToken',
                 'always_issue_new_refresh_token' => true,
             ],
             'authorization_code' => [
                 'class' => 'OAuth2\GrantType\AuthorizationCode',
                 'require_exact_redirect_uri' => false,
             ]
         ]
     ]
 ]

for backend

  'modules' => [
      'oauth2' => [
          'class' => 'zacksleo\yii2\oauth2\backend\Module',
      ]
  ]

Token Controller

<?php

namespace api\modules\v1\controllers;

class TokenController extends \zacksleo\yii2\oauth2\api\controllers\TokenController
{

}

Api with Token Authorization

class ResourceController extends \zacksleo\yii2\oauth2\api\controllers\Oauth2Controller 
{

}

Config common\models\User

<?php

namespace common\models;

use OAuth2\Storage\UserCredentialsInterface;
use yii;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
use yii\behaviors\TimestampBehavior;
use filsh\yii2\oauth2server\exceptions\HttpException;
use zacksleo\yii2\oauth2\common\helpers\Predis;

/**
 * User model
 *
 * @property integer $id
 * @property string $phone
 * @property string $created_at
 * @property string $updated_at
 * @property string $union_id
 */
class User extends ActiveRecord implements IdentityInterface, UserCredentialsInterface
{
    /**
     * @inheritdoc
     */
    public static function findIdentity($id)
    {
        return static::findOne($id);
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null)
    {
        $oauthAccessToken = Predis::getInstance()->getClient()->getToken($token);
        if (empty($oauthAccessToken)) {
            throw new yii\web\UnauthorizedHttpException('Unauthorized');
        }
        $model = static::findOne(['union_id' => $oauthAccessToken['union_id']]);
        return $model;
    }

    /**
     * Implemented for Oauth2 Interface
     * @param $username
     * @param $password
     * @return bool
     * @throws HttpException
     */
    public function checkUserCredentials($username, $password)
    {
    }

    /**
     * Implemented for Oauth2 Interface
     * @param $username
     * @return array
     */
    public function getUserDetails($username)
    {
     }

    /**
     * @inheritdoc
     */
    public function getId()
    {
        return $this->getPrimaryKey();
    }

    public function getUnionId()
    {
        return $this->union_id;
    }
}