2016-05-15 21 views
0

好吧,我閱讀了如何在應用程序中實現Laravel socialite,這樣我就可以讓用戶使用Google或Facebook登錄。我讀了如何去做here。我遇到的這種方法遇到的問題是,如果用戶通過電子郵件[email protected]登錄Google,然後使用[email protected]從Facebook登錄,他們將登錄到同一個帳戶!安全問題正確。所以我認爲,在我讓他們登錄之前,我會檢查你可以得到的提供者ID,但是當我嘗試比較存儲在數據庫中的provider_id時,他們創建的帳戶與存儲在社交名稱用戶變量I中的provider_id得到這個錯誤:Laravel Socialite - 具有相同電子郵件的不同提供商 - 安全性如何修復

參數1傳遞給照亮\身份驗證\ SessionGuard ::登錄()必須實現接口照亮\ \合同驗證\可驗證,照亮例如\ HTTP \ RedirectResponse給出

這裏是所有的代碼我用於Socialite:

<?php 

namespace App\Http\Controllers; 
use Socialite; 
use App\User; 
use Auth; 
use Illuminate\Support\Facades\Redirect; 
use Flash; 
use Illuminate\Http\Request; 
use App\Http\Requests; 

class SocialiteController extends Controller 
{ 

    public function redirectToProvider($provider) 
    { 
     return Socialite::driver($provider)->redirect(); 
    } 

    public function handleProviderCallback($provider) 
    { 
     try 
     { 
      $social_user = Socialite::driver($provider)->user(); 
     } 
     catch(Exception $e) 
     { 
      return Redirect::to('auth/' . $provider); 
     } 
     $authUser = $this->findOrCreateUser($social_user); 
     Auth::login($authUser, true); 
     flash()->overlay('You have been logged in successfully!', 'Congratulations'); 
     return Redirect::to('/'); 

    } 
    //create a new user in our database or grab existing user 
    private function findOrCreateUser($social_user) 
    { 
     if ($authUser = User::where('email', $social_user->email)->first()) { 
      //this creates an error 
      if($authUser->provider_id == $social_user->id) 
       return $authUser; 
      else 
      { 
       flash()->overlay('An account for that email already exists!', 'Error'); 
       return Redirect::to('/'); 
      } 
     } 


     return User::Create([ 
      'provider_id' => $social_user->id, 
      'name' => $social_user->name, 
      'email' => $social_user->email, 
      'nickname' => $social_user->nickname, 
      'avatar_img' => $social_user->avatar, 
      'role_id' => 1, //set role to guest 
      'social_login' => true //tell the database they are logging in from oauth 

     ]); 

    } 
} 

回答

0

錯誤消息實際上是自我探測ained。 當你做User::where('provider_id', $social_user->id)你最終會得到實現的構建器對象

Illuminate\Database\Eloquent\Builder

你可以調用->get()它來得到結果 的集合(對象的集合,實現

Illuminate\Contracts\Auth\Authenticatable

你的情況

,這樣你就可以對他們進行迭代),或者像你一樣,你可以用->first()(實現的一個對象

Illuminate\Contracts\Auth\Authenticatable)得到第一個匹配。

您可以在Eloquent文檔中閱讀更多信息。

重點是,直到您撥打->get()->first()您正在使用構建器對象。 實際上也有->find()方法,用於通過pk檢索記錄,不能用它通過約束來搜索記錄(where),但它也返回模型對象。

+0

這個問題最初發布在這裏:http://stackoverflow.com/questions/37247380/laravel-socialite-user-getid – Magearlik

0

The problem I encounter with this approach is that if a user logs in from say Google with email [email protected] then logs in from Facebook using [email protected] they are logging into the same account! Security issue right.

在我看來,這是一個不正確的假設。這不是一個安全問題。

OAuth是作爲身份驗證委派,如果用戶控制在同一個電子郵件不同的供應商帳戶,它只是意味着他已經與驗證其身份的控制(電子郵件)

更多第三方的OAuth服務如果您限制我只能使用一個OAuth提供商登錄,並且禁止我使用同一封電子郵件來使用另一個OAuth提供商,我會將其視爲一個錯誤。

相關問題