Skip to content

Commit

Permalink
Merge branch 'release/4.2.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
kristofser committed Feb 15, 2023
2 parents aa99c56 + a607ff7 commit 6fda88a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 45 deletions.
12 changes: 8 additions & 4 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

All notable changes to `GoogleAuthenticator` will be documented in this file.
## Version 4.2.0
- Added support for Laravel 10
- Added return types
- Removed support for Laravel 8
## Version 4.1.3
- Added banner image in readme
## Version 4.1.2
Expand All @@ -10,7 +14,7 @@ All notable changes to `GoogleAuthenticator` will be documented in this file.
## Version 4.1.0
- Updated version illumninate/support to use Laravel 8 or 9
- Updated auth templates with new format from Laravel + google login button
- Removed Laravel support for version 6 and 7
- Removed Laravel support for version 6 and 7
- Removed google/apiclient
## Version 4.0.0
### Updated
Expand Down Expand Up @@ -48,7 +52,7 @@ All notable changes to `GoogleAuthenticator` will be documented in this file.
## Version 3.0.1
### Changed
- Fixed markdown typos
- Removed statik.be from roles array in config file
- Removed statik.be from roles array in config file

## Version 3.0.0
### Changed
Expand Down Expand Up @@ -86,7 +90,7 @@ All notable changes to `GoogleAuthenticator` will be documented in this file.
- Added names for routes & use them in overwritten auth views
### Changed
- Updated packages to work with laravel 6
- Cleaned up some code
- Cleaned up some code

## Version 1.0.9
### Added
Expand Down Expand Up @@ -129,4 +133,4 @@ All notable changes to `GoogleAuthenticator` will be documented in this file.

## Version 1.0
### Added
- Everything
- Everything
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"homepage": "https://github.com/statikbe/laravel-google-authenticate",
"keywords": ["Laravel", "google", "authenticate", "socialite"],
"require": {
"illuminate/support": "^8.0|^9.0",
"illuminate/routing": "^8.0|^9.0",
"laravel/socialite": "^5.5"
"illuminate/support": "^9.0|^10.0",
"illuminate/routing": "^9.0|^10.0",
"laravel/socialite": "^5.6"
},
"suggest": {
"doctrine/dbal": "If you are using the provided migration you will need doctrine/dbal."
Expand Down
33 changes: 16 additions & 17 deletions src/GoogleAuthenticateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

namespace Statikbe\GoogleAuthenticate;

use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Str;
use Laravel\Socialite\AbstractUser;
use Laravel\Socialite\Facades\Socialite;
use Laravel\Socialite\Two\InvalidStateException;
Expand All @@ -29,7 +30,7 @@ class GoogleAuthenticateController extends Controller
*
* @var string
*/
protected $redirectTo = '/';
protected mixed $redirectTo = '/';

const GOOGLE_VALUES = [
'name',
Expand Down Expand Up @@ -61,16 +62,16 @@ public function __construct()
/**
* Redirect the user to the Google authentication page.
*
* @return \Illuminate\Http\Response
* @return RedirectResponse
*/
public function redirectToProvider()
public function redirectToProvider(): RedirectResponse
{
request()->session()->flash('googleLoginUrl', url()->previous());

return Socialite::driver('google')->scopes(['openid', 'profile', 'email'])->redirect();
}

public function handleProviderCallback()
public function handleProviderCallback(): RedirectResponse
{
$loginUrl = session('googleLoginUrl') ?? '/';

Expand All @@ -97,7 +98,7 @@ public function handleProviderCallback()
* @return User
* @throws GoogleAuthenticationException
*/
private function findOrCreate(AbstractUser $googleUser, string $provider)
private function findOrCreate(AbstractUser $googleUser, string $provider): User
{
if (isset($googleUser->email)) {
//make userFillableArray
Expand All @@ -115,15 +116,15 @@ private function findOrCreate(AbstractUser $googleUser, string $provider)
//If the disabled array is filled we check the domain against it
$domainsToIgnore = $domains['disabled'] ?? null;
if ($domainsToIgnore) {
if (in_array($emailDomain, $domainsToIgnore)) {
if (in_array($emailDomain, $domainsToIgnore, true)) {
throw new GoogleAuthenticationException();
}
}

//If the allowed array is filled we check the domain against it
$domainsToValidate = $domains['allowed'] ?? null;
if (!empty($domainsToValidate)) {
if (in_array($emailDomain, $domainsToValidate)) {
if (in_array($emailDomain, $domainsToValidate, true)) {
return $this->createUser($userData);
}
throw new GoogleAuthenticationException();
Expand All @@ -141,7 +142,7 @@ private function findOrCreate(AbstractUser $googleUser, string $provider)
* @param AbstractUser $user
* @return array $data
*/
private function fillUserData(AbstractUser $user)
private function fillUserData(AbstractUser $user): array
{
//get user table columns
$columns = config('google-authenticate.user_columns', []);
Expand All @@ -161,12 +162,11 @@ private function fillUserData(AbstractUser $user)

/**
* @param array $userData
* @param Role $roleModel
* @return User $user
*/
private function createUser($userData)
private function createUser(array $userData): User
{
//search for possible user with this email but without google provider
//search for possible user with this email but without Google provider
$user = $this->userModel::where('email', $userData['email'])->whereNull('provider_id')->first();
if ($user) {
//filling found user
Expand All @@ -187,9 +187,9 @@ private function createUser($userData)

/**
* @param array $values
* @param $user Socialite user object
* @param $user user object
*/
private function checkForGoogleData(&$values, $user)
private function checkForGoogleData(array &$values, $user): void
{
//loop values provided from configInvalidStateException
foreach ($values as $key => $value) {
Expand All @@ -201,10 +201,9 @@ private function checkForGoogleData(&$values, $user)
}

//if value found in google_values array, return it's google value
if (in_array($value, self::GOOGLE_VALUES)) {
if (in_array($value, self::GOOGLE_VALUES, true)) {
$values[$key] = $user[$value];
continue;
}
}
}
}
}
26 changes: 13 additions & 13 deletions src/GoogleAuthenticateServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ class GoogleAuthenticateServiceProvider extends ServiceProvider
*
* @return void
*/
public function boot()
public function boot(): void
{
//$this->loadViewsFrom(__DIR__.'/../resources/views', 'google-authenticate');

$this->publishes([
__DIR__.'/../database/migrations/add_google_provider_to_user_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_add_google_provider_to_user_table.php'),
__DIR__ . '/../database/migrations/add_google_provider_to_user_table.php.stub' => database_path('migrations/' . date('Y_m_d_His', time()) . '_add_google_provider_to_user_table.php'),
], 'migrations');

//loaders
$this->loadRoutesFrom(__DIR__.'/routes.php');
$this->loadRoutesFrom(__DIR__ . '/routes.php');

$this->loadTranslationsFrom(__DIR__.'/../resources/lang', 'google-authenticate');
$this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'google-authenticate');

// Publishing is only necessary when using the CLI:
if ($this->app->runningInConsole()) {
Expand All @@ -35,10 +35,10 @@ public function boot()
*
* @return void
*/
public function register()
public function register(): void
{
$this->mergeConfigFrom(
__DIR__.'/config/google-authenticate.php', 'google-authenticate'
__DIR__ . '/config/google-authenticate.php', 'google-authenticate'
);

// Register the service the package provides.
Expand All @@ -52,7 +52,7 @@ public function register()
*
* @return array
*/
public function provides()
public function provides(): array
{
return ['GoogleAuthenticate'];
}
Expand All @@ -62,25 +62,25 @@ public function provides()
*
* @return void
*/
protected function bootForConsole()
protected function bootForConsole(): void
{
// Publishing the views
$this->publishes([
__DIR__.'/../resources/views' => resource_path('views/vendor/google-authenticate'),
__DIR__ . '/../resources/views' => resource_path('views/vendor/google-authenticate'),
], 'views');

//publish the translations
$langPath = 'vendor/google-authenticate';
$langPath = (function_exists('lang_path'))
? lang_path($langPath)
: resource_path('lang/'.$langPath);
: resource_path('lang/' . $langPath);
$this->publishes([
__DIR__.'/../resources/lang' => $langPath,
__DIR__ . '/../resources/lang' => $langPath,
], 'lang');

//publishes config file
$this->publishes([
__DIR__.'/config/google-authenticate.php' => config_path('google-authenticate.php'),
__DIR__ . '/config/google-authenticate.php' => config_path('google-authenticate.php'),
], 'config');
}
}
}
16 changes: 8 additions & 8 deletions src/Traits/HasGoogleAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@

trait HasGoogleAuth
{

/**
* The attributes that are needed for Google Auth
* @var array
*/
protected $auth_fillable = [
protected array $auth_fillable = [
'provider',
'provider_id'
];
public function getFillable()


public function getFillable(): array
{

return array_merge($this->fillable, $this->auth_fillable);
}
}

}

0 comments on commit 6fda88a

Please sign in to comment.