我正在嘗試設置一個使用Passport保護的Laravel API的SPA。Laravel Passport密碼授予返回無效憑證異常
我開始創建一個新的專門爲此的Laravel應用程序,然後我按照說明設置護照並設置密碼授權客戶端。
我可以成功創建一個新用戶,將用戶保存到數據庫並登錄用戶。之後,我嘗試使用新創建的用戶信息以及密碼授予客戶端ID和密碼來創建一個訪問令牌。在這一點上,我收到了例外。
我通過日誌讀取,我看到異常被拋出的地方。裏面League\OAuth2\Server\Grant\PasswordGrant
的validateUser
方法有以下幾點:
if ($user instanceof UserEntityInterface === false) {
$this->getEmitter()->emit(new RequestEvent(RequestEvent::USER_AUTHENTICATION_FAILED, $request));
throw OAuthServerException::invalidCredentials();
}
看到這一幕我實現了我的用戶模型UserEntityInterface
並實施了getIdentifier
方法,但我仍然收到異常。我真的不太確定從哪裏去,任何幫助將不勝感激。以下是我的一些代碼。
這裏是我的登記控制器:
class RegisterController extends Controller
{
private $tokenService;
public function __construct(AccessTokenService $tokenService)
{
//$this->middleware('guest');
$this->tokenService = $tokenService;
}
public function register(Request $request)
{
$this->validateWith($this->validator($request->all()));
Log::debug('Validated');
$user = $this->create($request->all());
$this->guard()->login($user);
$this->tokenService->boot(Auth::user());
return response()->json($this->tokenService->getNewAccessToken(), 200);
}
protected function guard()
{
return Auth::guard();
}
protected function validator(array $data)
{
return Validator::make($data, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|min:6|confirmed',
'password_confirmation' => 'required'
]);
}
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
而這些都是AccessTokenService的相關部分:
public function getNewAccessToken() {
$http = new Client();
$client = \Laravel\Passport\Client::where('id', 6)->first();
Log::debug($client->getAttribute('secret'));
Log::debug($this->user->getAttribute('email'));
Log::debug($this->user->getAuthPassword());
$response = $http->post('homestead.app/oauth/token', [
'form_params' => [
'grant_type' => 'password',
'client_id' => 6,
'client_secret' => $client->getAttribute('secret'),
'username' => $this->user->getAttribute('email'),
'password' => $this->user->getAuthPassword(),
'scope' => '*'
]]);
unset($client);
$status = $response->getStatusCode();
$body = $response->getBody();
Log::debug($body->getContents());
Log::debug($status);
switch($status)
{
case 200:case 201:
case 202:
$tokens = array(
"user_id" => $this->user->getAttribute('id'),
"access_token" => $body['access_token'],
"refresh_token" => $body['refresh_token']
);
$output = ["access_token" => $this->storeTokens($tokens), 'status_code' => $status];
break;
default:
$output = ["access_token" => '', 'status_code' => $status];
break;
}
return $output;
}
private function storeTokens(array $tokens) {
UserToken::create([
"user_id" => $tokens['user_id'],
"access_token" => bcrypt($tokens['access_token']),
"refresh_token" => bcrypt($tokens['refresh_token'])
]);
return $tokens['access_token'];
}
後你得到的異常信息。 – Learner
你在本地使用api嗎? (IE是同一應用程序的SPA部分嗎?) – Joe
是的,我在本地使用API。我解決了這個問題。在請求access_token時,我正在傳遞哈希密碼,並且PasswordGrant期待着一個非哈希密碼。現在全部修好了。 –