/** * @param $provider * * @throws HybridAuthError */ public function index($provider) { if (isset($_REQUEST['hauth_start']) || isset($_REQUEST['hauth_done'])) { Hybrid_Endpoint::process(); } else { try { $map = ['Facebook' => 'id', 'Google' => 'id', 'GitHub' => 'id']; $settings = ['Facebook' => ['scope' => 'email', 'display' => 'popup'], 'Google' => ['scope' => 'https://www.googleapis.com/auth/userinfo.email'], 'Twitter' => ['includeEmail' => true]]; $config = $this->providers->getProvider($provider); if (!empty($config['key']) && !empty($config['secret'])) { $pConfig = array_merge(['enabled' => true, "keys" => [$map[$provider] ?? 'key' => $config['key'], 'secret' => $config['secret']]], $settings[$provider] ?? []); $config = ["providers" => [$provider => $pConfig]]; $hauth = new Hybrid_Auth($config); $auth = $hauth->authenticate($provider); if ($profile = $auth->getUserProfile()) { $event = 'session_user_login'; $data = ['ident' => $profile->identifier, 'email' => $profile->email, 'first_name' => $profile->firstName, 'last_name' => $profile->lastName, 'photo_url' => $profile->photoURL, 'verified' => 'true']; /** @var User $user */ if ($user = User::where('ident', '=', $profile->identifier)->first()) { if (empty($user->email) && !empty($profile->email)) { $user->email = $profile->email; $user->save(); } if (!empty($profile->email)) { $user->contact_email = $profile->email; $user->save(); } } elseif (!empty($profile->email) && ($user = User::where('email', '=', $profile->email)->first())) { $user->ident = $profile->identifier; $user->save(); } else { $user = $this->signupHandler->registerUser($data); $event = 'session_user_signup'; } if ($user_id = $user->user_id ?? null) { $this->session->startSession($user_id); $socialEvent = new UserSocialEvent($user->user_id, array_merge((array) $profile, ['provider' => $provider])); $this->dispatcher->fire($event === 'session_user_signup' ? UserSocialEvent::USER_SOCIAL_SIGNUP : UserSocialEvent::USER_SOCIAL_LOGIN, $socialEvent); $userData = json_encode(['user' => $user->toArray()]); printf("<scrip" . "t>try { self.opener.Minute.setSessionData(%s, '%s'); } catch(err) { console.log(err); } finally { self.window.close(); }</script>", $userData, $event); } } } else { throw new Exception("Configuration for {$provider} is incomplete"); } } catch (\Throwable $e) { Hybrid_Auth::logoutAllProviders(); throw new HybridAuthError($e->getMessage()); } } }
public function getCachedSessionData($reload) { $key = sprintf("session-user-%d", $this->session->getLoggedInUserId()); $userData = function () { $user_id = $this->session->getLoggedInUserId(); /** @var User $user_info */ if ($user_info = User::find($user_id)) { $user_data = array_diff_key($user_info->getAttributes(), ['password' => 1, 'verified' => 1, 'ident' => 1]); $user_data['groups'] = $this->userInfo->getUserGroups($user_id, true) ?: []; } else { $user_data = null; } if (!empty($user_data) && empty($user_data['full_name'])) { $user_data['full_name'] = trim(sprintf('%s %s', $user_data['first_name'], $user_data['last_name'])) ?: 'Anonymous'; } foreach ($this->providers->getEnabled() as $provider) { unset($provider['key'], $provider['secret']); $providers[] = $provider; } return ['site' => $this->config->getPublicVars(), 'user' => $user_data, 'providers' => $providers ?? []]; }; $data = $reload ? $userData() : $this->cache->get($key, $userData, 300); return $data; }