2013-05-28 46 views
2

你如何告訴laravel auth::attempt密碼字段是以明文存儲的,而不是假設它被散列?laravel 4 auth ::以明文形式嘗試密碼

:在該guard.php

public function attempt(array $credentials = array(), $remember = false, $login = true) 
{ 
    $this->fireAttemptEvent($credentials, $remember, $login); 

    $user = $this->provider->retrieveByCredentials($credentials); 

    // If an implementation of UserInterface was returned, we'll ask the provider 
    // to validate the user against the given credentials, and if they are in 
    // fact valid we'll log the users into the application and return true. 
    if ($user instanceof UserInterface) 
    { 
     if ($this->provider->validateCredentials($user, $credentials)) 
     { 
      if ($login) $this->login($user, $remember); 

      return true; 
     } 
    } 

    return false; 
} 

或更好,但我只是有2列,password_secured一個作爲明文等。

如果我嘗試後,我怎麼告訴嘗試的密碼列名是password_secured.

因爲我想這一點,並得到了一個錯誤Undefined index: password

$user = array(
     'user_id'   => Input::get('username'), 
     'password_secured' => Input::get('password'), 
     'checklogin'  => 0, 
    ); 

    if (Auth::attempt($user)) { 
     return 'login success'; 
    } 

事情是我移植的應用程序,而不是從頭開始構建的,我真的需要密碼,因爲另一個應用程序正在使用DB存儲在純文本(它是活的)和編碼爲以明文形式讀取密碼。

回答

3

考慮運行一個腳本來散列所有的密碼:以明文存儲應該永遠得到授權,甚至考慮(即使你繼承系統),因爲這些密碼立刻失去的那一刻你的數據庫內容被泄露。黑客發生。想象一下,如果你的客戶發現你沒有按照標準處理他們的數據,那麼你可以想象訴訟......

現在,假設你不想聽到這個警告,那麼做的方法很駭人,但是很有效。 Guard,如從看到的源(查找__construct),給出實現UserProviderInterface的對象。

你有一堆合適的物體。選擇一個你想要的,並擴展它。我們會對DatabaseUserProvider有一些樂趣,儘管這種擴展方法對所有人都很方便和可行。

我們要延長的方法是public function validateCredentials(UserInterface $user, array $credentials)。如下:

namespace Illuminate\Auth; 
class MyUserInterface extends DatabaseUserProvider { 
    public function validateCredentials(UserInterface $user, array $credentials) { 
     $plain = $credentials['password']; 
     return ($plain === $user->getAuthPassword()); 
    } 
} 

作爲MyUserInterface延伸DatabaseUserProvider其本身提供UserProviderInterfaceMyUserInterface現在依賴-注射在Guard作爲提供者。我們完成了一半的工作。下一步是實際告訴Guard加載你的東西。我不熟悉Laravel4加載Guard實現的方式,但在某處配置的某處,您可以將MyUserInterface設置爲選擇的Guard接口。我不能比這更具體。

順便說一下,該類需要與Auth的其他接口實現位於相同的位置。

+0

謝謝。知道了所有的設置,但我仍然在尋找負載實施。我無法散列所有密碼,因爲使用相同數據庫的另一個APP正在以明文方式讀取它,我無法修改該應用程序,也無法訪問它。我只是在網絡方面。該應用程序是一些C++可執行文件。 – user2415940

+0

我發現它在Guard.php的這一行if($ user instanceof MyUserInterface)' – user2415940

+0

OH。這更奇怪它仍然無法登錄。 – user2415940

相關問題