<commit_before><?php
/**
 * ownCloud - oauthclient
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright <NAME> 2017
 */

namespace OCA\OauthClient\Controller;

use \OCP\AppFramework\Controller;
use \OCP\AppFramework\Http\RedirectResponse;
use \OCP\IUserManager;
use \OCP\IUserSession;

require_once __DIR__ . '/../3rdparty/vendor/autoload.php';

class AuthController extends Controller {

	private $userManager;
	private $userSession;

  public function __construct($appName, $request,
    								IUserManager $userManager,
		                IUserSession $userSession
  ){
      parent::__construct($appName, $request);
      $this->userSession = $userSession;
		  $this->userManager = $userManager;
  }

  /**
   * @NoAdminRequired
   * @NoCSRFRequired
   * @PublicPage
   */
  public function login($code=null) {
    /**
     * TODO: Agafar aquests paràmetres de la configuració
     */
    $clientid     = 'e63b7303a7a6443e7ce50414773b7d1f0a1b9033ae1020534c7440888e4e8633';
    $clientsecret = 'de5d9a3ed59694dacd5a5f06e6998c8d69a8ecbe044acb690dc99a72e8aed315';
    $redirecturi  = 'https://betafitxers.barcelonaencomu.cat/index.php/apps/oauthclient';
    $autorizationendpoint = 'https://betaparticipa.barcelonaencomu.cat/oauth/authorize';
    $tokenendpoint         = 'https://betaparticipa.barcelonaencomu.cat/oauth/token';
    $apiendpoint = 'https://betaparticipa.barcelonaencomu.cat/api/v2/users/me';

    $oauthclient = new \OAuth2\Client($clientid, $clientsecret);
    /*
    $user = $this->userManager->get('usu1');
    $pass = rand();
    $user->setPassword($pass);
    $this->userSession->login('usu1', $pass);
    $this->userSession->createSessionToken($this->request, 'usu1', 'usu1', $pass);
    return new RedirectResponse('/index.php/apps/files');
    */
    if (!$code) {
      $authurl = $oauthclient->getAuthenticationUrl($autorizationendpoint, $redirecturi);
      return new RedirectResponse($authurl);
    } else {
      $params = array('code' => $code, 'redirect_uri' => $redirecturi);
      $response = $oauthclient->getAccessToken($tokenendpoint, 'authorization_code', $params);
      $oauthclient->setAccessToken($response['result']['access_token']);
      $response = $oauthclient->fetch($apiendpoint);
      $result = $response['result'];

      //Check if user exists
      if ($this->userManager->userExists($result['username'])) {
        $user = $this->userManager->get($result['username']);
        $pass = rand();
        $user->setPassword($pass);
        $this->userSession->login($result['username'], $pass);
        $this->userSession->createSessionToken($this->request, $result['username'], $result['username'], $pass);
        return new RedirectResponse('/index.php/apps/files');
      } else {
        //Create the user
        die('create user');
      }
    }
  }
}<commit_msg>user creation<commit_after><?php
/**
 * ownCloud - oauthclient
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright <NAME> 2017
 */

namespace OCA\OauthClient\Controller;

use \OCP\AppFramework\Controller;
use \OCP\AppFramework\Http\RedirectResponse;
use \OCP\IUserManager;
use \OCP\IUserSession;

require_once __DIR__ . '/../3rdparty/vendor/autoload.php';

class AuthController extends Controller {

	private $userManager;
	private $userSession;

  public function __construct($appName, $request,
    								IUserManager $userManager,
		                IUserSession $userSession
  ){
      parent::__construct($appName, $request);
      $this->userSession = $userSession;
		  $this->userManager = $userManager;
  }

  /**
   * @NoAdminRequired
   * @NoCSRFRequired
   * @PublicPage
   */
  public function login($code=null) {
    /**
     * TODO: Agafar aquests paràmetres de la configuració
     */
    $clientid     = 'e63b7303a7a6443e7ce50414773b7d1f0a1b9033ae1020534c7440888e4e8633';
    $clientsecret = 'de5d9a3ed59694dacd5a5f06e6998c8d69a8ecbe044acb690dc99a72e8aed315';
    $redirecturi  = 'https://betafitxers.barcelonaencomu.cat/index.php/apps/oauthclient';
    $autorizationendpoint = 'https://betaparticipa.barcelonaencomu.cat/oauth/authorize';
    $tokenendpoint         = 'https://betaparticipa.barcelonaencomu.cat/oauth/token';
    $apiendpoint = 'https://betaparticipa.barcelonaencomu.cat/api/v2/users/me';

    $oauthclient = new \OAuth2\Client($clientid, $clientsecret);
    /*
    $user = $this->userManager->get('usu1');
    $pass = rand();
    $user->setPassword($pass);
    $this->userSession->login('usu1', $pass);
    $this->userSession->createSessionToken($this->request, 'usu1', 'usu1', $pass);
    return new RedirectResponse('/index.php/apps/files');
    */
    if (!$code) {
      $authurl = $oauthclient->getAuthenticationUrl($autorizationendpoint, $redirecturi);
      return new RedirectResponse($authurl);
    } else {
      $params = array('code' => $code, 'redirect_uri' => $redirecturi);
      $response = $oauthclient->getAccessToken($tokenendpoint, 'authorization_code', $params);
      $oauthclient->setAccessToken($response['result']['access_token']);
      $response = $oauthclient->fetch($apiendpoint);
      $result = $response['result'];

			$pass = <PASSWORD>();
			$uid = $result['username'];

      //Check if user exists
      if ($this->userManager->userExists($uid)) {
        $user = $this->userManager->get($uid);
        $user->setPassword($pass);
      } else {
				$user = $this->userManager->createUser($uid, $pass);
      }
			$this->userSession->login($uid, $pass);
			$this->userSession->createSessionToken($this->request, $uid, $uid, $pass);
			return new RedirectResponse('/index.php/apps/files');
    }
  }
}
{'fraction_non_alphanumeric': 0.1536871123363198, 'fraction_numerical': 0.032046864231564436, 'mean_word_length': 3.7426470588235294, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 66, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1979890', 'n_tokens_mistral': 2038, 'n_tokens_neox': 1920, 'n_words': 309}