'slogin.log'), JLog :: ALL, array('slogin')); parent::__construct( $subject, $params ); $this->secret = $this->params->get('secret'); $this->app_id = $this->params->get('app_id'); $this->authorizeUrl=$this->params->get('authorizeUrl'); $this->tokenUrl=$this->params->get('tokenUrl'); $this->scope=$this->params->get('scope'); $this->apiUrl=$this->params->get('apiUrl'); $this->redirectUrl =urlencode(JURI::base().'?option=com_slogin&task=check&plugin=github'); } public function onSloginAuth() { session_start(); $_SESSION['state']=hash('sha256', microtime(TRUE).rand().$_SERVER['REMOTE_ADDR']) ; unset($_SESSION['access_token']); $params = array( 'client_id=' . $this->app_id, 'response_type=code', 'scope='. $this->scope, 'state=' . $_SESSION['state'] , 'redirect_uri='.$this->redirectUrl); $params = implode('&', $params); $url = $this->authorizeUrl.'?'.$params; return $url; } public function onSloginCheck() { require_once JPATH_BASE.'/components/com_slogin/controller.php'; $controller = new SLoginController(); $input = JFactory::getApplication()->input; $code = $input->get('code', null, 'STRING'); $returnRequest = new SloginRequest(); if($this->get('code')) { //Step 2. Exchange the Authorization Code for an Access Token // Exchange the auth code for a token $params= array( 'client_id='.$this->app_id, 'client_secret='.$this->secret, 'code='.$this->get('code'), 'state=' . $_SESSION['state'] , 'redirect_uri='.$this->redirectUrl); $params= implode('&', $params); $request = json_decode($this->apiCall($this->tokenUrl,$params)); if(empty($request)){ echo 'Empty request after sending token for auth code '; JLog :: add(' Empty request after sending token for auth code ' , JLog :: ERROR, 'slogin'); exit; } else if(!empty($request->error)) { JLog :: add('Empty request after sending token for auth code '.$request->error , JLog :: ERROR, 'slogin'); echo '
'; var_dump($request->error); echo ''; exit; } $_SESSION['access_token'] = $request->access_token; //JLog :: add(("REQUEST FOR USER DATA ".$this->apiUrl.'/user'.'?access_token='.$request->access_token), JLog :: INFO, 'slogin'); $headers=array( 'Content-Type: application/json', 'User-Agent: OpenAire2020 Portal', ); $request = json_decode($this->apiCall($this->apiUrl.'/user'.'?access_token='.$_SESSION['access_token'],null,$headers)); $returnRequest->display_name = $request->login; $returnRequest->given_name = $request->login;; $returnRequest->first_name = $request->login; $returnRequest->username= $request->login; $returnRequest->id = $request->id; $returnRequest->all_request = $request; $request = json_decode($this->apiCall($this->apiUrl.'/user/emails'.'?access_token='.$_SESSION['access_token'],null,$headers)); $returnRequest->email = $request[0]->email; JFactory::getApplication()->setUserState('slogin.token', array( 'provider' => 'github', 'token' => $_SESSION['access_token'] , 'slogin_user' => $returnRequest->id, 'app_id' => $this->app_id, 'app_secret' => $this->secret, )); return $returnRequest; } else{ echo 'Error - empty code'; exit; } } function apiCall($url,$params=null, $headers=null) { if (!function_exists('curl_init')) { Log :: add( 'ERROR: CURL library not found' , JLog :: INFO, 'slogin'); die('ERROR: CURL library not found!'); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, TRUE); if ( $params!=null) { curl_setopt($ch, CURLOPT_POSTFIELDS, $params); curl_setopt($ch, CURLOPT_POST, TRUE); } else { curl_setopt($ch, CURLOPT_POST, FALSE); } if($headers!=null) { curl_setopt($ch, CURLOPT_HTTPHEADER,$headers); } else { curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Accept: application/json', 'User-Agent: OpenAire2020 Portal', )); } curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); $result = curl_exec($ch); curl_close($ch); return $result; } function get($key, $default=NULL) { return isset($key, $_GET) ? $_GET[$key] : $default; } function session($key, $default=NULL) { return array_key_exists($key, $_SESSION) ? $_SESSION[$key] : $default; } public function onCreateSloginLink(&$links, $add = '') { $i = count($links); $links[$i]['link'] = 'index.php?option=com_slogin&task=auth&plugin=github' . $add; $links[$i]['class'] = 'githubslogin'; $links[$i]['plugin_name'] = 'github'; $links[$i]['plugin_title'] = JText::_('COM_SLOGIN_PROVIDER_GITHUB'); } }