База пользовательской аутентификации пользователя в ответ на вызов API

Описание:

Сейчас я использую Laravel для кучи проекта. Реализация аутентификации пользователей проста в Laravel. Теперь структура, с которой я имею дело, немного отличается – у меня нет database или таблицы users локально. Я должен сделать вызов API, чтобы запросить то, что мне нужно.


я пробовал

 public function postSignIn(){ $username = strtolower(Input::get('username')); $password_api = VSE::user('password',$username); // abc <----- $password = Input::get('password'); // abc <----- if ( $password == $password_api ) { //Log user in $auth = Auth::attempt(); // Stuck here <---- } if ($auth) { return Redirect::to('/dashboard')->with('success', 'Hi '. $username .' ! You have been successfully logged in.'); } else { return Redirect::to('/')->with('error', 'Username/Password Wrong')->withInput(Request::except('password'))->with('username', $username); } } 

обновленный

Я подключаюсь к API с помощью простой команды shell_exec в своем классе VSE

 public static function user($attr, $username) { $data = shell_exec('curl '.env('API_HOST').'vse/accounts'); $raw = json_decode($data,true); $array = $raw['data']; return $array[$attr]; } 

Мне жаль, что я не могу показать это вам здесь, но он находится на виртуальной машине на моей локальной машине, поэтому, пожалуйста, оставайтесь со мной здесь. В основном, это

казнить

curl http://172.16.67.137:1234/vse/accounts <— обновлено

отклик

 Object data:Array[2] 0:Object DBA:"" account_id:111 account_type:"admin" address1:"111 Park Ave" address2:"Floor 4" address3:"Suite 4011" city:"New York" customer_type:2 display_name:"BobJ" email_address:"bob@xyzcorp.com" first_name:"Bob" last_name:"Jones" last_updated_utc_in_secs:200200300 middle_names:"X." name_prefix:"Mr" name_suffix:"Jr." nation_code:"USA" non_person_name:false password:"abc" phone1:"212-555-1212" phone2:"" phone3:"" postal_code:"10022" state:"NY" time_zone_offset_from_utc:-5 1:Object DBA:"" account_id:112 account_type:"mbn" address1:"112 Park Ave" address2:"Floor 3" address3:"Suite 3011" city:"New York" customer_type:2 display_name:"TomS" email_address:"tom@xyzcorp.com" first_name:"Tom" last_name:"Smith" last_updated_utc_in_secs:200200300 middle_names:"Z." name_prefix:"Mr" name_suffix:"Sr." nation_code:"USA" non_person_name:false password:"abd" phone1:"212-555-2323" phone2:"" phone3:"" postal_code:"10022" state:"NY" time_zone_offset_from_utc:-5 message:"Success" status:200 . Object data:Array[2] 0:Object DBA:"" account_id:111 account_type:"admin" address1:"111 Park Ave" address2:"Floor 4" address3:"Suite 4011" city:"New York" customer_type:2 display_name:"BobJ" email_address:"bob@xyzcorp.com" first_name:"Bob" last_name:"Jones" last_updated_utc_in_secs:200200300 middle_names:"X." name_prefix:"Mr" name_suffix:"Jr." nation_code:"USA" non_person_name:false password:"abc" phone1:"212-555-1212" phone2:"" phone3:"" postal_code:"10022" state:"NY" time_zone_offset_from_utc:-5 1:Object DBA:"" account_id:112 account_type:"mbn" address1:"112 Park Ave" address2:"Floor 3" address3:"Suite 3011" city:"New York" customer_type:2 display_name:"TomS" email_address:"tom@xyzcorp.com" first_name:"Tom" last_name:"Smith" last_updated_utc_in_secs:200200300 middle_names:"Z." name_prefix:"Mr" name_suffix:"Sr." nation_code:"USA" non_person_name:false password:"abd" phone1:"212-555-2323" phone2:"" phone3:"" postal_code:"10022" state:"NY" time_zone_offset_from_utc:-5 message:"Success" status:200 - Object data:Array[2] 0:Object DBA:"" account_id:111 account_type:"admin" address1:"111 Park Ave" address2:"Floor 4" address3:"Suite 4011" city:"New York" customer_type:2 display_name:"BobJ" email_address:"bob@xyzcorp.com" first_name:"Bob" last_name:"Jones" last_updated_utc_in_secs:200200300 middle_names:"X." name_prefix:"Mr" name_suffix:"Jr." nation_code:"USA" non_person_name:false password:"abc" phone1:"212-555-1212" phone2:"" phone3:"" postal_code:"10022" state:"NY" time_zone_offset_from_utc:-5 1:Object DBA:"" account_id:112 account_type:"mbn" address1:"112 Park Ave" address2:"Floor 3" address3:"Suite 3011" city:"New York" customer_type:2 display_name:"TomS" email_address:"tom@xyzcorp.com" first_name:"Tom" last_name:"Smith" last_updated_utc_in_secs:200200300 middle_names:"Z." name_prefix:"Mr" name_suffix:"Sr." nation_code:"USA" non_person_name:false password:"abd" phone1:"212-555-2323" phone2:"" phone3:"" postal_code:"10022" state:"NY" time_zone_offset_from_utc:-5 message:"Success" status:200 - Object data:Array[2] 0:Object DBA:"" account_id:111 account_type:"admin" address1:"111 Park Ave" address2:"Floor 4" address3:"Suite 4011" city:"New York" customer_type:2 display_name:"BobJ" email_address:"bob@xyzcorp.com" first_name:"Bob" last_name:"Jones" last_updated_utc_in_secs:200200300 middle_names:"X." name_prefix:"Mr" name_suffix:"Jr." nation_code:"USA" non_person_name:false password:"abc" phone1:"212-555-1212" phone2:"" phone3:"" postal_code:"10022" state:"NY" time_zone_offset_from_utc:-5 1:Object DBA:"" account_id:112 account_type:"mbn" address1:"112 Park Ave" address2:"Floor 3" address3:"Suite 3011" city:"New York" customer_type:2 display_name:"TomS" email_address:"tom@xyzcorp.com" first_name:"Tom" last_name:"Smith" last_updated_utc_in_secs:200200300 middle_names:"Z." name_prefix:"Mr" name_suffix:"Sr." nation_code:"USA" non_person_name:false password:"abd" phone1:"212-555-2323" phone2:"" phone3:"" postal_code:"10022" state:"NY" time_zone_offset_from_utc:-5 message:"Success" status:200 . Object data:Array[2] 0:Object DBA:"" account_id:111 account_type:"admin" address1:"111 Park Ave" address2:"Floor 4" address3:"Suite 4011" city:"New York" customer_type:2 display_name:"BobJ" email_address:"bob@xyzcorp.com" first_name:"Bob" last_name:"Jones" last_updated_utc_in_secs:200200300 middle_names:"X." name_prefix:"Mr" name_suffix:"Jr." nation_code:"USA" non_person_name:false password:"abc" phone1:"212-555-1212" phone2:"" phone3:"" postal_code:"10022" state:"NY" time_zone_offset_from_utc:-5 1:Object DBA:"" account_id:112 account_type:"mbn" address1:"112 Park Ave" address2:"Floor 3" address3:"Suite 3011" city:"New York" customer_type:2 display_name:"TomS" email_address:"tom@xyzcorp.com" first_name:"Tom" last_name:"Smith" last_updated_utc_in_secs:200200300 middle_names:"Z." name_prefix:"Mr" name_suffix:"Sr." nation_code:"USA" non_person_name:false password:"abd" phone1:"212-555-2323" phone2:"" phone3:"" postal_code:"10022" state:"NY" time_zone_offset_from_utc:-5 message:"Success" status:200 - Object data:Array[2] 0:Object DBA:"" account_id:111 account_type:"admin" address1:"111 Park Ave" address2:"Floor 4" address3:"Suite 4011" city:"New York" customer_type:2 display_name:"BobJ" email_address:"bob@xyzcorp.com" first_name:"Bob" last_name:"Jones" last_updated_utc_in_secs:200200300 middle_names:"X." name_prefix:"Mr" name_suffix:"Jr." nation_code:"USA" non_person_name:false password:"abc" phone1:"212-555-1212" phone2:"" phone3:"" postal_code:"10022" state:"NY" time_zone_offset_from_utc:-5 1:Object DBA:"" account_id:112 account_type:"mbn" address1:"112 Park Ave" address2:"Floor 3" address3:"Suite 3011" city:"New York" customer_type:2 display_name:"TomS" email_address:"tom@xyzcorp.com" first_name:"Tom" last_name:"Smith" last_updated_utc_in_secs:200200300 middle_names:"Z." name_prefix:"Mr" name_suffix:"Sr." nation_code:"USA" non_person_name:false password:"abd" phone1:"212-555-2323" phone2:"" phone3:"" postal_code:"10022" state:"NY" time_zone_offset_from_utc:-5 message:"Success" status:200 . Object data:Array[2] 0:Object DBA:"" account_id:111 account_type:"admin" address1:"111 Park Ave" address2:"Floor 4" address3:"Suite 4011" city:"New York" customer_type:2 display_name:"BobJ" email_address:"bob@xyzcorp.com" first_name:"Bob" last_name:"Jones" last_updated_utc_in_secs:200200300 middle_names:"X." name_prefix:"Mr" name_suffix:"Jr." nation_code:"USA" non_person_name:false password:"abc" phone1:"212-555-1212" phone2:"" phone3:"" postal_code:"10022" state:"NY" time_zone_offset_from_utc:-5 1:Object DBA:"" account_id:112 account_type:"mbn" address1:"112 Park Ave" address2:"Floor 3" address3:"Suite 3011" city:"New York" customer_type:2 display_name:"TomS" email_address:"tom@xyzcorp.com" first_name:"Tom" last_name:"Smith" last_updated_utc_in_secs:200200300 middle_names:"Z." name_prefix:"Mr" name_suffix:"Sr." nation_code:"USA" non_person_name:false password:"abd" phone1:"212-555-2323" phone2:"" phone3:"" postal_code:"10022" state:"NY" time_zone_offset_from_utc:-5 message:"Success" status:200 - Object data:Array[2] 0:Object DBA:"" account_id:111 account_type:"admin" address1:"111 Park Ave" address2:"Floor 4" address3:"Suite 4011" city:"New York" customer_type:2 display_name:"BobJ" email_address:"bob@xyzcorp.com" first_name:"Bob" last_name:"Jones" last_updated_utc_in_secs:200200300 middle_names:"X." name_prefix:"Mr" name_suffix:"Jr." nation_code:"USA" non_person_name:false password:"abc" phone1:"212-555-1212" phone2:"" phone3:"" postal_code:"10022" state:"NY" time_zone_offset_from_utc:-5 1:Object DBA:"" account_id:112 account_type:"mbn" address1:"112 Park Ave" address2:"Floor 3" address3:"Suite 3011" city:"New York" customer_type:2 display_name:"TomS" email_address:"tom@xyzcorp.com" first_name:"Tom" last_name:"Smith" last_updated_utc_in_secs:200200300 middle_names:"Z." name_prefix:"Mr" name_suffix:"Sr." nation_code:"USA" non_person_name:false password:"abd" phone1:"212-555-2323" phone2:"" phone3:"" postal_code:"10022" state:"NY" time_zone_offset_from_utc:-5 message:"Success" status:200 

Как вы можете видеть, пароль для Bob – это abc а для Tom is abd

Следуя приведенным ниже инструкциям, вы можете настроить свой собственный драйвер проверки подлинности, который обрабатывает выборку и проверку учетных данных пользователя с помощью вызова API:

1. Создайте собственный пользовательский провайдер в app/Auth/ApiUserProvider.php со следующим содержимым:

 namespace App\Auth; use Illuminate\Contracts\Auth\UserProvider; use Illuminate\Contracts\Auth\Authenticatable as UserContract; class ApiUserProvider implements UserProvider { /** * Retrieve a user by the given credentials. * * @param array $credentials * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveByCredentials(array $credentials) { $user = $this->getUserByUsername($credentials['username']); return $this->getApiUser($user); } /** * Retrieve a user by their unique identifier. * * @param mixed $identifier * @return \Illuminate\Contracts\Auth\Authenticatable|null */ public function retrieveById($identifier) { $user = $this->getUserById($identifier); return $this->getApiUser($user); } /** * Validate a user against the given credentials. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param array $credentials * @return bool */ public function validateCredentials(UserContract $user, array $credentials) { return $user->getAuthPassword() == $credentials['password']; } /** * Get the api user. * * @param mixed $user * @return \App\Auth\ApiUser|null */ protected function getApiUser($user) { if ($user !== null) { return new ApiUser($user); } } /** * Get the use details from your API. * * @param string $username * @return array|null */ protected function getUsers() { $ch = curl_init(); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_URL, env('API_HOST') . 'vse/accounts'); $response = curl_exec($ch); $response = json_decode($response, true); curl_close($ch); return $response['data']; } protected function getUserById($id) { $user = []; foreach ($this->getUsers() as $item) { if ($item['account_id'] == $id) { $user = $item; break; } } return $user ?: null; } protected function getUserByUsername($username) { $user = []; foreach ($this->getUsers() as $item) { if ($item['email_address'] == $username) { $user = $item; break; } } return $user ?: null; } // The methods below need to be defined because of the Authenticatable contract // but need no implementation for 'Auth::attempt' to work and can be implemented // if you need their functionality public function retrieveByToken($identifier, $token) { } public function updateRememberToken(UserContract $user, $token) { } } 

2. Также создайте пользовательский класс, который расширяет стандартный GenericUser предлагаемый системой аутентификации в app/Auth/ApiUser.php со следующим содержимым:

 namespace App\Auth; use Illuminate\Auth\GenericUser; use Illuminate\Contracts\Auth\Authenticatable as UserContract; class ApiUser extends GenericUser implements UserContract { public function getAuthIdentifier() { return $this->attributes['account_id']; } } 

3. В app/Providers/AuthServiceProvider.php загрузки вашего app/Providers/AuthServiceProvider.php зарегистрируйте нового поставщика драйверов:

 public function boot(GateContract $gate) { $this->registerPolicies($gate); // The code below sets up the 'api' driver $this->app['auth']->extend('api', function() { return new \App\Auth\ApiUserProvider(); }); } 

4. Наконец, в файле config/auth.php установите драйвер на свой собственный:

  'driver' => 'api', 

Теперь вы можете сделать следующее в действии вашего контроллера:

 public function postSignIn() { $username = strtolower(Input::get('username')); $password = Input::get('password'); if (Auth::attempt(['username' => $username, 'password' => $password])) { return Redirect::to('/dashboard')->with('success', 'Hi '. $username .'! You have been successfully logged in.'); } else { return Redirect::to('/')->with('error', 'Username/Password Wrong')->withInput(Request::except('password'))->with('username', $username); } } 

Вызов Auth::user() для получения сведений о пользователе после успешного входа в систему, вернет экземпляр ApiUser содержащий атрибуты, извлеченные из удаленного API, и будет выглядеть примерно так:

 ApiUser {#143 ▼ #attributes: array:10 [▼ "DBA" => "" "account_id" => 111 "account_type" => "admin" "display_name" => "BobJ" "email_address" => "bob@xyzcorp.com" "first_name" => "Bob" "last_name" => "Jones" "password" => "abc" "message" => "Success" "status" => 200 ] } 

Поскольку вы не разместили образец ответа, который вы получаете, когда в API нет пользовательского getUserDetails для электронной почты пользователя, я настраиваю условие в методе getUserDetails , чтобы определить, что нет совпадения и возвращает значение null если ответ не содержат свойство data или свойство data пусто. Вы можете изменить это условие в соответствии с вашими потребностями.


Вышеописанный код был протестирован с помощью отталкиваемого ответа, который возвращает структуру данных, опубликованную в вашем вопросе, и работает очень хорошо.

В качестве заключительного примечания: вы должны решительно рассмотреть возможность изменения API для более ранней обработки пользовательской аутентификации, а не позже (возможно, с использованием реализации Oauth), поскольку отправка пароля (и даже более тревожная как обычный текст) не является чем-то, что вы хотите отложить делать.