#5: Facebook login is now working using the app ID/secret stored in the database, instead of in the services file

This commit is contained in:
Andy Heathershaw 2018-08-14 12:57:41 +01:00
parent 8af88c56aa
commit 40fc25eba9
3 changed files with 41 additions and 12 deletions

View File

@ -29,10 +29,17 @@ use Illuminate\Support\Facades\View;
class DefaultController extends Controller class DefaultController extends Controller
{ {
private $passwordSettingKeys;
public function __construct() public function __construct()
{ {
$this->middleware('auth'); $this->middleware('auth');
View::share('is_admin', true); View::share('is_admin', true);
$this->passwordSettingKeys = [
'smtp_password',
'facebook_app_secret'
];
} }
public function about() public function about()
@ -211,9 +218,6 @@ class DefaultController extends Controller
{ {
$this->authorizeAccessToAdminPanel('admin:configure'); $this->authorizeAccessToAdminPanel('admin:configure');
$passwordKeys = [
'smtp_password'
];
$checkboxKeys = [ $checkboxKeys = [
'albums_menu_parents_only', 'albums_menu_parents_only',
'allow_self_registration', 'allow_self_registration',
@ -264,7 +268,7 @@ class DefaultController extends Controller
} }
$config->value = $request->request->get($key); $config->value = $request->request->get($key);
if (in_array($key, $passwordKeys) && strlen($config->value) > 0) if (in_array($key, $this->passwordSettingKeys) && strlen($config->value) > 0)
{ {
$config->value = encrypt($config->value); $config->value = encrypt($config->value);
} }
@ -313,6 +317,14 @@ class DefaultController extends Controller
$dateFormatsLookup[$dateFormat] = date($dateFormat); $dateFormatsLookup[$dateFormat] = date($dateFormat);
} }
foreach ($this->passwordSettingKeys as $passwordSettingKey)
{
if (isset($config[$passwordSettingKey]) && !empty($config[$passwordSettingKey]))
{
$config[$passwordSettingKey] = decrypt($config[$passwordSettingKey]);
}
}
$themeNamesLookup = UserConfig::allowedThemeNames(); $themeNamesLookup = UserConfig::allowedThemeNames();
return Theme::render('admin.settings', [ return Theme::render('admin.settings', [

View File

@ -3,10 +3,12 @@
namespace App\Http\Controllers\Auth; namespace App\Http\Controllers\Auth;
use App\Facade\Theme; use App\Facade\Theme;
use App\Facade\UserConfig;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\User; use App\User;
use Illuminate\Foundation\Auth\AuthenticatesUsers; use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Laravel\Socialite\Two\FacebookProvider;
use Socialite; use Socialite;
class LoginController extends Controller class LoginController extends Controller
@ -71,7 +73,8 @@ class LoginController extends Controller
*/ */
public function redirectToFacebook() public function redirectToFacebook()
{ {
return Socialite::driver('facebook')->redirect(); $socialite = $this->setSocialiteConfigs();
return $socialite->driver('facebook')->redirect();
} }
/** /**
@ -81,7 +84,8 @@ class LoginController extends Controller
*/ */
public function handleFacebookCallback(Request $request) public function handleFacebookCallback(Request $request)
{ {
$facebookUser = Socialite::driver('facebook')->user(); $socialite = $this->setSocialiteConfigs();
$facebookUser = $socialite->driver('facebook')->user();
$user = User::where('facebook_id', $facebookUser->id)->first(); $user = User::where('facebook_id', $facebookUser->id)->first();
if (is_null($user)) if (is_null($user))
@ -100,4 +104,23 @@ class LoginController extends Controller
return redirect(route('home')); return redirect(route('home'));
} }
private function setSocialiteConfigs()
{
// Force Socialite to use our config from the database instead of hard-coded in config/services.php
$socialite = app()->make('Laravel\Socialite\Contracts\Factory');
$socialite->extend(
'facebook',
function ($app) use ($socialite) {
$config = [
'client_id' => UserConfig::get('facebook_app_id'),
'client_secret' => decrypt(UserConfig::get('facebook_app_secret')),
'redirect' => route('login_callback.facebook')
];
return $socialite->buildProvider(FacebookProvider::class, $config);
}
);
return $socialite;
}
} }

View File

@ -14,12 +14,6 @@ return [
| |
*/ */
'facebook' => [
'client_id' => env('FACEBOOK_CLIENT_ID'),
'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
'redirect' => php_sapi_name() != 'cli' ? url('login/facebook/callback') : ''
],
'gitea' => [ 'gitea' => [
'api_url' => 'https://apps.andysh.uk/api/v1', 'api_url' => 'https://apps.andysh.uk/api/v1',
'cache_time_seconds' => 3600, 'cache_time_seconds' => 3600,