diff --git a/README.md b/README.md index 1561b9b..5de3905 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ 'max' => 10, // Максимальное количество знаков 'stable_length' => true, // Стабильная генерация 'secret' => 'erb26vwu2', // Секретная фраза для метода md5 + 'viewReg' => true, // Верхний регистр для метода md5 'default_gen' => true, // Использовать default_pass как дефолтный пароль 'default_pass' => "123456", // Строка для дефолтного пароля diff --git a/src/Http/Controllers/LoginController.php b/src/Http/Controllers/LoginController.php index 8f4914e..d933759 100644 --- a/src/Http/Controllers/LoginController.php +++ b/src/Http/Controllers/LoginController.php @@ -6,11 +6,15 @@ use Illuminate\Http\Request; use App\Http\Controllers\Controller; use Illuminate\Support\Facades\Auth; +use Sashagm\Social\Traits\GenPassTrait; use Laravel\Socialite\Facades\Socialite; - +use Sashagm\Social\Traits\GuardTrait; class LoginController extends Controller { + use GenPassTrait, GuardTrait; + + public function redirectToProvider($provider) { return Socialite::driver($provider)->redirect(); @@ -52,26 +56,22 @@ public function handleProviderCallback($provider) $this->isAccess($socialUser->getEmail()); - $user->updated_at = \Carbon\Carbon::now(); - $user->save(); - Auth::login($user); - if($new) { + if ($new) { return redirect() - ->route(config('socials.redirect.auth')) - ->with('success', trans('social-auth::socials.register')); - + ->route(config('socials.redirect.auth')) + ->with('success', trans('social-auth::socials.register')); } else { - return redirect() - ->route(config('socials.redirect.auth')) - ->with('success', trans('social-auth::socials.login')); + $user->updated_at = \Carbon\Carbon::now(); + $user->save(); + return redirect() + ->route(config('socials.redirect.auth')) + ->with('success', trans('social-auth::socials.login')); } - - } @@ -80,156 +80,12 @@ public function logout() { Auth::logout(); - return redirect()->route(config('socials.redirect.logout'))->with('success', trans('social-auth::socials.logout')); - - } - - - private function checkSocialsIsActive($user = null) - { - $access = config('socials.access_admin'); - - if ($user && in_array($user->id, $access)) { - return true; - } else { - if (!config('socials.isActive')) { - abort(403, trans('social-auth::socials.offline') ); - } - } - } - - - private function isAccess($email) - { - $user = User::where('email', $email)->first(); - - if ($user && $user->{config('socials.user.access_colum')} == config('socials.user.access_value')) { - abort(403, trans('social-auth::socials.ban') ); - } - } - - - - private function generatePass() - { - $method = config('socials.genPass.method'); - $filter = config('socials.genPass.filter'); - $secret = config('socials.genPass.secret'); - - switch ($method) { - - case 'bcrypt': - $pass = bcrypt($this->generateString($filter)); - break; - - case 'md5': - $pass = md5($this->generateString($filter) . $secret); - break; - - case 'password_hash': - $pass = password_hash($this->generateString($filter), PASSWORD_DEFAULT); - break; - - case 'sha1': - $pass = sha1($this->generateString($filter)); - break; - - case 'sha256': - $pass = hash('sha256', $this->generateString($filter)); - break; - - case 'base64': - $pass = base64_encode($this->generateString($filter)); - break; - - - - default: - $pass = bcrypt($this->generateString($filter)); - break; - } - - return $pass; + return redirect()->route(config('socials.redirect.logout'))->with('success', trans('social-auth::socials.logout')); } - private function generateString($filter) - { - - if (config('socials.genPass.default_gen')) { - - return config('socials.genPass.default_pass'); - } else { - - switch ($filter) { - - case 'string': - $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; - break; - case 'number': - $characters = '0123456789'; - break; + - case 'hard': - $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; - break; - case 'hard-unique': - $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-+=[]{}|:;<>,.?/~'; - break; - case 'rus-string': - $characters = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'; - break; - - case 'rus-hard': - $characters = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ0123456789'; - break; - - case 'rus-unique': - $characters = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ0123456789!@#$%^&*()_-+=[]{}|:;<>,.?/~'; - break; - - default: - $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; - break; - } - - $minLength = config('socials.genPass.min'); - $maxLength = config('socials.genPass.max'); - $stableLength = config('socials.genPass.stable_length'); - - - if ($stableLength) { - $length = config('socials.genPass.length'); - } else { - $length = rand($minLength, $maxLength); - } - - - $string = ''; - for ($i = 0; $i < $length; $i++) { - $string .= $characters[rand(0, strlen($characters) - 1)]; - } - return $string; - } - } - - private function checkProvider($user, $provider) - { - $guard = config('socials.isProvider'); - - switch ($guard) { - case true: - if ($user->provider == $provider) { - return true; - } else { - abort(403, trans('social-auth::socials.provider')); - } - break; - case false: - return true; - break; - } - } } diff --git a/src/Traits/GenPassTrait.php b/src/Traits/GenPassTrait.php new file mode 100644 index 0000000..6cfc25d --- /dev/null +++ b/src/Traits/GenPassTrait.php @@ -0,0 +1,125 @@ +generateString($filter)); + break; + + case 'md5': + if (config('socials.genPass.viewReg')){ + $pass = strtoupper(md5($this->generateString($filter) . $secret)); + } else { + $pass = md5($this->generateString($filter) . $secret); + } + break; + + case 'password_hash': + $pass = password_hash($this->generateString($filter), PASSWORD_DEFAULT); + break; + + case 'sha1': + $pass = sha1($this->generateString($filter)); + break; + + case 'sha256': + $pass = hash('sha256', $this->generateString($filter)); + break; + + case 'base64': + $pass = base64_encode($this->generateString($filter)); + break; + + + + default: + $pass = bcrypt($this->generateString($filter)); + break; + } + + return $pass; + } + + private function generateString($filter) + { + + if (config('socials.genPass.default_gen')) { + + return config('socials.genPass.default_pass'); + } else { + + switch ($filter) { + + case 'string': + $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + break; + + case 'number': + $characters = '0123456789'; + break; + + case 'hard': + $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; + break; + + case 'hard-unique': + $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_-+=[]{}|:;<>,.?/~'; + break; + + case 'rus-string': + $characters = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ'; + break; + + case 'rus-hard': + $characters = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ0123456789'; + break; + + case 'rus-unique': + $characters = 'абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯ0123456789!@#$%^&*()_-+=[]{}|:;<>,.?/~'; + break; + + default: + $characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; + break; + } + + $minLength = config('socials.genPass.min'); + $maxLength = config('socials.genPass.max'); + $stableLength = config('socials.genPass.stable_length'); + + + if ($stableLength) { + $length = config('socials.genPass.length'); + } else { + $length = rand($minLength, $maxLength); + } + + + $string = ''; + for ($i = 0; $i < $length; $i++) { + $string .= $characters[rand(0, strlen($characters) - 1)]; + } + return $string; + } + } + + + +} \ No newline at end of file diff --git a/src/Traits/GuardTrait.php b/src/Traits/GuardTrait.php new file mode 100644 index 0000000..1405a8a --- /dev/null +++ b/src/Traits/GuardTrait.php @@ -0,0 +1,56 @@ +id, $access)) { + return true; + } else { + if (!config('socials.isActive')) { + abort(403, trans('social-auth::socials.offline')); + } + } + } + + + private function isAccess($email) + { + $user = User::where('email', $email)->first(); + + if ($user && $user->{config('socials.user.access_colum')} == config('socials.user.access_value')) { + abort(403, trans('social-auth::socials.ban')); + } + } + + + private function checkProvider($user, $provider) + { + $guard = config('socials.isProvider'); + + switch ($guard) { + case true: + if ($user->provider == $provider) { + return true; + } else { + abort(403, trans('social-auth::socials.provider')); + } + break; + case false: + return true; + break; + } + } + + +} \ No newline at end of file diff --git a/src/config/socials.php b/src/config/socials.php index bcee841..353649a 100644 --- a/src/config/socials.php +++ b/src/config/socials.php @@ -26,6 +26,7 @@ 'max' => 10, // Максимальное количество знаков 'stable_length' => true, // Стабильная генерация 'secret' => 'erb26vwu2', // Секретная фраза для метода md5 + 'viewReg' => true, // Верхний регистр для метода md5 'default_gen' => true, // Использовать default_pass как дефолтный пароль 'default_pass' => "123456", // Строка для дефолтного пароля