我正在使用运行良好的Yii2 Filsh Oauth 服务器,但是当我登录时,它会生成带有默认字段的 AccessToken,即
{
"access_token": "f3389e81c234276967079b2293795fc9104a2fac",
"expires_in": 86400,
"token_type": "Bearer",
"user_id": 9,
"scope": null,
"refresh_token": "851464a210f56bb831da378a43e1016bd3e765d7",
}
但我需要在其响应中添加用户信息,例如
{
"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",
}
}
我想出了一个奇怪的解决方法,我自定义了 bshaffer 核心库文件(这不是一个好方法)以满足我的要求,我做了什么,我在用户模型中更改了这一行:
return ['user_id' => $user->getId()];
对此
return ['user_id' => [$user->getId(), $userObject]]; //I get all user info in $userObject and passed an array with two fields
因为我传递的是一个数组而不是单个数组,$user->getId()
因此我需要修改 bshaffer 库文件 AccessToken.php,该文件在此路径中可用:vendor/bshaffer/oauth2-server-php/src/OAuth2/ResponseType/AccessToken.php
在第 76 行
我改变了这个:
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;
}
对此:
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;
}
现在一切正常,问题是因为我在运行composer时修改了bshaffer核心文件,它再次覆盖了它的默认代码,并且每次运行composer后我的更改都会被清除,我再次需要修改同一个文件。我需要一个适当的解决方法,可能是component
我覆盖这个 calss/method 并放置我的更改,以便在运行 composer 后它运行相同。