OAuthLogout.php 5.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

namespace Drupal\oauthost\Controller;

use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\user\Entity\User;
use Drupal\Core\Database\Connection;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
14 15
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

class OAuthLogout extends ControllerBase
{
    protected $entity_query;
    protected $entityTypeManager;
    protected $logger;
    protected $connection;

    protected $consumer_key = '';
    protected $consumer_secret = '';
    protected $request_token_url;
    protected $user_authorization_url;
    protected $access_token_url;
    protected $signature_method;
    protected $api_url;
    protected $callback_url;
    protected $logout_url;
33
    protected $redirect_url;
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58

    public function __construct(
    EntityTypeManagerInterface $entityTypeManager,
    QueryFactory $entity_query,
    Connection $connection,
    LoggerChannelFactoryInterface $loggerChannel)
    {
        $this->entityTypeManager = $entityTypeManager;
        $this->entity_query = $entity_query;
        $this->connection = $connection;
        $this->logger = $loggerChannel->get('oauthost');
    }

    public static function create(ContainerInterface $container)
    {
        return new static(
          $container->get('entity.manager'),
          $container->get('entity.query'),
          $container->get('database'),
          $container->get('logger.factory')
      );
    }

    public function logoutGo(Request $request)
    {
59

60 61
        $trx = $this->connection->startTransaction();
        try {
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
            $user = null;
            $username = $request->headers->get('PHP_AUTH_USER');
            $oauthostSessions = $this->entityTypeManager->getStorage('oauthost_session')->loadByProperties(array('authtoken' => $username));
            $this->oauthostSession = reset($oauthostSessions);

            if ($this->oauthostSession) {
                $configRowName = $this->oauthostSession->configrowname->value;
            } else {
                $response = new Response();
                $response->setContent('forbidden');
                $response->setStatusCode(Response::HTTP_FORBIDDEN);
                $response->headers->set('Content-Type', 'application/json');
                return $response;
            }
            $ostauthConfigs = $this->entityTypeManager->getStorage('oauthost_config')->loadByProperties(array('name' => $configRowName));
77 78 79 80 81 82 83 84 85 86 87
        $ostauthConfig = reset($ostauthConfigs);
        if ($ostauthConfig) {
            $this->consumer_key = $ostauthConfig->consumer_key->value;
            $this->consumer_secret = $ostauthConfig->consumer_secret->value;
            $this->request_token_url = $ostauthConfig->request_token_url->value;
            $this->user_authorization_url = $ostauthConfig->user_authorization_url->value;
            $this->access_token_url = $ostauthConfig->access_token_url->value;
            $this->signature_method = $ostauthConfig->signature_method->value;
            $this->api_url = $ostauthConfig->api_url->value;
            $this->callback_url = $ostauthConfig->callback_url->value;
            $this->logout_url = $ostauthConfig->logout_url->value;
88
            $this->redirect_url = $ostauthConfig->redirect_url->value;
89 90 91 92 93 94 95 96
        } else {
            $response = new Response();
            $response->setContent('forbidden');
            $response->setStatusCode(Response::HTTP_FORBIDDEN);
            $response->headers->set('Content-Type', 'application/json');
            return $response;
        }

97

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        $epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $username));
        $epalUser = reset($epalUsers);
        $foundUser = true;

        if ($epalUser) {
            $user = $this->entityTypeManager->getStorage('user')->load($epalUser->user_id->target_id);
            if ($user) {

                $res = \Drupal::httpClient()->get($this->logout_url . $username, array('headers' => array('Accept' => 'text/plain')));
/*                $resData = (string) $res->getBody();
                if (empty($resData)) {
                    return FALSE;
                } */

//                if ($res->getStatusCode() === "200")

                $user->setPassword(uniqid('pw'));
                $user->save();
                $epalUser->set('accesstoken', '-');
                $epalUser->set('accesstoken_secret', '-');
                $epalUser->set('authtoken','-');
                $epalUser->set('requesttoken','-');
                $epalUser->set('requesttoken_secret', '-');
                $epalUser->save();

            } else {
                $foundUser = false;
            }
        } else {
            $foundUser = false;
        }
        if (!$foundUser) {
            $this->logger->warning("user not found");
            $response = new Response();
            $response->setContent('forbidden');
            $response->setStatusCode(Response::HTTP_FORBIDDEN);
            $response->headers->set('Content-Type', 'application/json');
            return $response;
        }
        $response = new Response();
        $response->setContent('logout successful');
        $response->setStatusCode(Response::HTTP_OK);
        $response->headers->set('Content-Type', 'application/json');
141
        $this->oauthostSession->delete();
142
        return $response;
143
//        return new RedirectResponse($this->redirect_url . '&auth_role=', 302, []);
144 145 146 147 148 149 150 151 152 153 154 155 156 157


        } catch (Exception $e) {
            $this->logger->warning($e->getMessage());
            $response = new Response();
            $response->setContent('forbidden');
            $response->setStatusCode(Response::HTTP_FORBIDDEN);
            $response->headers->set('Content-Type', 'application/json');
            $trx->rollback();
            return $response;
        }
    }

}