/**
  * Performs an OAuth authentication
  */
 public function execute()
 {
     /** @var \Grav\Common\Language\Language */
     $t = $this->grav['language'];
     $provider = strtolower($this->action);
     $config = $this->grav['config']->get('plugins.login-oauth.providers.' . $this->action, []);
     if (isset($config['credentials'])) {
         // Setup the credentials for the requests
         $credentials = new Credentials($config['credentials']['key'], $config['credentials']['secret'], $this->grav['uri']->url(true));
         // Instantiate service using the credentials, http client
         // and storage mechanism for the token
         $scope = isset($this->scopes[$provider]) ? $this->scopes[$provider] : [];
         $this->service = $this->factory->createService($this->action, $credentials, $this->storage, $scope);
     }
     if (!$this->service || empty($config)) {
         $this->setMessage($t->translate(['PLUGIN_LOGIN_OAUTH.OAUTH_PROVIDER_NOT_SUPPORTED', $this->action]));
         return true;
     }
     // Check OAuth authentication status
     $authenticated = parent::execute();
     if (is_bool($authenticated)) {
         $this->reset();
         if ($authenticated) {
             $this->setMessage($t->translate('PLUGIN_LOGIN.LOGIN_SUCCESSFUL'));
         } else {
             $this->setMessage($t->translate('PLUGIN_LOGIN.ACCESS_DENIED'));
         }
         // Redirect to current URI
         $redirect = $this->grav['config']->get('plugins.login.redirect_after_login');
         if (!$redirect) {
             $redirect = $this->grav['session']->redirect_after_login;
         }
         $this->setRedirect($redirect);
     } elseif (!$this->grav['session']->oauth) {
         $this->setMessage($t->translate(['PLUGIN_LOGIN_OAUTH.OAUTH_PROVIDER_NOT_SUPPORTED', $this->action]));
     }
     return true;
 }
Пример #2
0
 /**
  * Initialize login controller
  */
 public function loginController()
 {
     /** @var Uri $uri */
     $uri = $this->grav['uri'];
     $task = !empty($_POST['task']) ? $_POST['task'] : $uri->param('task');
     $task = substr($task, strlen('login.'));
     $post = !empty($_POST) ? $_POST : [];
     if (method_exists('Grav\\Common\\Utils', 'getNonce')) {
         switch ($task) {
             case 'login':
                 if (!isset($post['login-form-nonce']) || !Utils::verifyNonce($post['login-form-nonce'], 'login-form')) {
                     $this->grav['messages']->add($this->grav['language']->translate('PLUGIN_LOGIN.ACCESS_DENIED'), 'info');
                     $this->authenticated = false;
                     $twig = $this->grav['twig'];
                     $twig->twig_vars['notAuthorized'] = true;
                     return;
                 }
                 break;
             case 'logout':
                 $nonce = $this->grav['uri']->param('logout-nonce');
                 if (!isset($nonce) || !Utils::verifyNonce($nonce, 'logout-form')) {
                     return;
                 }
                 break;
             case 'forgot':
                 if (!isset($post['forgot-form-nonce']) || !Utils::verifyNonce($post['forgot-form-nonce'], 'forgot-form')) {
                     $this->grav['messages']->add($this->grav['language']->translate('PLUGIN_LOGIN.ACCESS_DENIED'), 'info');
                     return;
                 }
                 break;
             case 'reset':
                 if (!isset($post['reset-form-nonce']) || !Utils::verifyNonce($post['reset-form-nonce'], 'reset-form')) {
                     //$this->grav['messages']->add($this->grav['language']->translate('PLUGIN_LOGIN.ACCESS_DENIED'), 'info');
                     //return;
                 }
                 break;
         }
     }
     $controller = new Controller($this->grav, $task, $post);
     $controller->execute();
     $controller->redirect();
 }