2017-03-27 9 views
0

Passportで保護されたLaravel APIを使用するSPAをセットアップしようとしています。Laravel Passportパスワード授与で返される無効な資格情報例外

私はまずこのために新しいLaravelアプリケーションを作成し始めました。次に、パスポートを設定し、パスワード許可クライアントを設定する手順に従いました。

新しいユーザーを作成してデータベースに保存し、ユーザーをログインさせることができます。その後、新しく作成されたユーザーの情報とパスワード付与クライアントのIDとシークレットを使用してアクセスを作成しようとしますトークン。この時点で私は例外を受け取ります。

私はログを読んで、例外がスローされた場所を見ました。これを見て

if ($user instanceof UserEntityInterface === false) { 
      $this->getEmitter()->emit(new RequestEvent(RequestEvent::USER_AUTHENTICATION_FAILED, $request)); 

      throw OAuthServerException::invalidCredentials(); 
     } 

私は私のユーザモデルにUserEntityInterfaceを実装しgetIdentifier方法を実装しますが、私はまだ例外を受け取る:League\OAuth2\Server\Grant\PasswordGrant内部validateUser方法は、以下があります。私は本当にあまりにもここからどこに行くか分からない、どんな助けも大いに感謝されるだろう。以下は私のコードの一部です。ここで

は私の登録コントローラです:

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']; 
    } 
+0

あなたが得る例外メッセージを投稿します。 – Learner

+0

あなたはローカルでAPIを消費していますか? (IEは同じアプリケーションのSPA部分ですか?) – Joe

+0

はい、私はAPIをローカルで使用しています。私は問題を理解した。私はaccess_tokenを要求するときにハッシュされたパスワードを渡していましたが、PasswordGrantはハッシュされていないパスワードを期待していました。すべて確定しました。 –

答えて

0

だから私はこの問題を考え出しました。私がアクセストークンを要求していたとき、私はユーザーの電子メールとパスワードを渡していましたが、ハッシュされていないパスワードを渡す必要があるときにハッシュされたパスワードを渡していました。

アクセストークンのための私の要求は、このように見えた。このようなハッシュ解除済みパスワードを使用して機能するための要求を渡すことによって

$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(), //Here is the problem 
       'scope' => '*' 
      ]]); 

問題を解決:

$response = $http->post('homestead.app/oauth/token', [ 
      'form_params' => [ 
       'grant_type' => 'password', 
       'client_id' => 6, 
       'client_secret' => $client->getAttribute('secret'), 
       'username' => $request['email'], 
       'password' => $request['password'], 
       'scope' => '*' 
      ]]); 
関連する問題