Skip to content

Commit

Permalink
Добавлены новые Трэйты GenPassTrait, GuardTrait. update md5 регистр
Browse files Browse the repository at this point in the history
  • Loading branch information
Sashagm committed Jun 17, 2023
1 parent 08a2f9c commit 2ca2a84
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 159 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
'max' => 10, // Максимальное количество знаков
'stable_length' => true, // Стабильная генерация
'secret' => 'erb26vwu2', // Секретная фраза для метода md5
'viewReg' => true, // Верхний регистр для метода md5
'default_gen' => true, // Использовать default_pass как дефолтный пароль
'default_pass' => "123456", // Строка для дефолтного пароля

Expand Down
174 changes: 15 additions & 159 deletions src/Http/Controllers/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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'));
}


}


Expand All @@ -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;
}
}
}
125 changes: 125 additions & 0 deletions src/Traits/GenPassTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace Sashagm\Social\Traits;


use Illuminate\Database\Eloquent\Relations\MorphToMany;
use App\Models\User;

trait GenPassTrait
{



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':
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;
}
}



}
Loading

0 comments on commit 2ca2a84

Please sign in to comment.