How to override Yii2 default answer - Filsh Oauth Server

I use Yii2 Filsh Oauth Server , which works fine, however, when I log in, it creates an AccessToken with its default fields ie

{
  "access_token": "f3389e81c234276967079b2293795fc9104a2fac",
  "expires_in": 86400,
  "token_type": "Bearer",
  "user_id": 9,
  "scope": null,
  "refresh_token": "851464a210f56bb831da378a43e1016bd3e765d7",
}

But I needed to add user information in my answer something like

{
  "access_token": "f3389e81c234276967079b2293795fc9104a2fac",
  "expires_in": 86400,
  "token_type": "Bearer",
  "scope": null,
  "refresh_token": "851464a210f56bb831da378a43e1016bd3e765d7",
  "user": {
    "id": 9,
    "first_name": "Test",
    "last_name": "Test2",
    "username": "test",
    "email": "test@gmail.com",
    "status": 1,
    "dob": "20-08-1990",
    "gender": "Male",
   }
}

I came up with a strange workaround. I configured the bshaffer main library file (which is not very convenient) to satisfy my requirements, what I did, I changed this line in the User model:

return ['user_id' => $user->getId()];

THIS

return ['user_id' => [$user->getId(), $userObject]]; //I get all user info in $userObject and passed an array with two fields

since I am passing an array instead of a single one $user->getId(), so I needed to modify the bshaffer AccessToken.php library file, which is available along this path: vendor/bshaffer/oauth2-server-php/src/OAuth2/ResponseType/AccessToken.phpon line 76

I CHANGED THIS:

public function createAccessToken($client_id, $user_id, $scope = null, $includeRefreshToken = true)
{
  $token = array(
        "access_token" => $this->generateAccessToken(),
        "expires_in" => $this->config['access_lifetime'],
        "token_type" => $this->config['token_type'],
        "user_id" => $user_id,
        "scope" => $scope
    );

  $this->tokenStorage->setAccessToken($token["access_token"], $client_id, $user_id, $this->config['access_lifetime'] ? time() + $this->config['access_lifetime'] : null, $scope);

  if ($includeRefreshToken && $this->refreshStorage) {
        $token["refresh_token"] = $this->generateRefreshToken();
        $expires = 0;
        if ($this->config['refresh_token_lifetime'] > 0) {
            $expires = time() + $this->config['refresh_token_lifetime'];
        }
        $this->refreshStorage->setRefreshToken($token['refresh_token'], $client_id, $user_id, $expires, $scope);
    }
  return $token;
}

THIS:

public function createAccessToken($client_id, $user_id, $scope = null, $includeRefreshToken = true)
{
  $token = array(
        "access_token" => $this->generateAccessToken(),
        "expires_in" => $this->config['access_lifetime'],
        "token_type" => $this->config['token_type'],
        "scope" => $scope,
        "user" => $user_id[1] //NOTE: I added new user field and passed second index of array which is user node
    );

//NOTE: Here I passed $user_id[0] since $user_id is array hence I am using its 0 index here which has id
$this->tokenStorage->setAccessToken($token["access_token"], $client_id, $user_id[0], $this->config['access_lifetime'] ? time() + $this->config['access_lifetime'] : null, $scope);

  if ($includeRefreshToken && $this->refreshStorage) {
        $token["refresh_token"] = $this->generateRefreshToken();
        $expires = 0;
        if ($this->config['refresh_token_lifetime'] > 0) {
            $expires = time() + $this->config['refresh_token_lifetime'];
        }
        //NOTE: Same goes here passing $user_id[0]
        $this->refreshStorage->setRefreshToken($token['refresh_token'], $client_id, $user_id[0], $expires, $scope);
    }

  return $token; 

}

pefect , bshaffer composer, , . workarount, component, calss/method , ramians .

+4
1

, - vendor, .

, , , , , . . :

1) / bshaffer/oauth2-server-php. , Filsh/yii2-oauth2-server , bshaffer/oauth2-server-php ( , , /pull Filsh/yii2-oauth2-server). .

2) , . composer.json, Packagist. . Composer.

3) , , vendor.

4) . PHP , , ​​ - antecedent/patchwork.

.

, , . - :

replace(\OAuth2\ResponseType:AccessToken:class. '::createAccessToken', function ($client_id, $user_id, $scope = null, $includeRefreshToken = true) {
    // Redefine method behavior as you want here
});

1 , , . 2 3 .

:

  • ;

  • ;

  • , , .

4, , , .

UPDATE:

Yii2.

5) Yii:: $classMap() . , .

vendor/bshaffer/oauth2-server-php/src/OAuth2/ResponseType/AccessToken.php , , common/components. createAccessToken, ( Class not found).

bootstrap:

Yii::$classMap['OAuth2\ResponseType\AccessToken'] = '@common/components/AccessToken.php';

index.php :

$application = new yii\web\Application($config);
$application->run();

bootstrap, BootstrapInterface, .

, OAuth2\ResponseType\AccessToken, . , , , .

SO.

+3

Source: https://habr.com/ru/post/1658785/


All Articles