Skip to content
Snippets Groups Projects
Commit f18c3b13 authored by Janega Zoltán's avatar Janega Zoltán
Browse files

curl átalakítása

parent c3411afa
Branches
No related tags found
No related merge requests found
...@@ -7,7 +7,6 @@ class AuthSCHClient { ...@@ -7,7 +7,6 @@ class AuthSCHClient {
private static $password = ""; // your application's password private static $password = ""; // your application's password
private static $scope = ""; // wanted data, separated with plus sign. For more information see your website profile on auth.sch.bme.hu. private static $scope = ""; // wanted data, separated with plus sign. For more information see your website profile on auth.sch.bme.hu.
private $curl = null; // curl session
private $tokens; private $tokens;
public function __construct($checkLogin = true) { public function __construct($checkLogin = true) {
...@@ -33,19 +32,22 @@ class AuthSCHClient { ...@@ -33,19 +32,22 @@ class AuthSCHClient {
} }
private function curlSetOpt($urlPart, $data) { private function curlExec($urlPart, $data) {
curl_setopt($this->curl, CURLOPT_URL, self::$host . $urlPart); $ch = curl_init();
curl_setopt($this->curl, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_URL, self::$host . $urlPart);
curl_setopt($this->curl, CURLOPT_USERPWD, self::$username . ":" . self::$password); curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($this->curl, CURLOPT_TIMEOUT, 30); if($urlPart != "oauth2/resource")
curl_setopt($this->curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_USERPWD, self::$username . ":" . self::$password);
curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($data)); curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$ret = curl_exec($ch);
curl_close($ch);
return $ret;
} }
private function authenticate() { private function authenticate() {
$this->curl = curl_init();
// before authentication & authorization // before authentication & authorization
if (!isset($_GET['code'])) { if (!isset($_GET['code'])) {
// get token // get token
...@@ -53,33 +55,33 @@ class AuthSCHClient { ...@@ -53,33 +55,33 @@ class AuthSCHClient {
'grant_type' => 'client_credentials', 'grant_type' => 'client_credentials',
); );
$this->curlSetOpt("oauth2/token", $data); $ch = $this->curlExec("oauth2/token", $data);
$data = array( $data = array(
'access_token' => json_decode(curl_exec($this->curl))->access_token, 'access_token' => json_decode($ch)->access_token,
); );
$this->curlSetOpt("oauth2/resource", $data); $ch = $this->curlExec("oauth2/resource", $data);
// check api access & redirect to auth.sch.bme.hu for authorization // check api access & redirect to auth.sch.bme.hu for authorization
if (json_decode(curl_exec($this->curl))->success == true) if ($res != null && isset($res->success) && $res->success == true)
header("Location: " . $host . "site/login?response_type=code&client_id&". self::$username ."&state=" . sha1($_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']) . "&scope=" . self::$scope); header("Location: " . self::$host . "site/login?response_type=code&client_id&". self::$username ."&state=" . sha1($_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']) . "&scope=" . self::$scope);
else
throw new Exception("error during api check");
} else { } else {
$data = array( $data = array(
'grant_type'=>'authorization_code', 'grant_type'=>'authorization_code',
'code'=>$_GET['code'], 'code'=>$_GET['code'],
); );
$this->curlSetOpt("oauth2/token", $data); $ch = $this->curlExec("oauth2/token", $data);
$tokens = json_decode(curl_exec($this->curl)); $tokens = json_decode($ch);
if($tokens === null || !isset($tokens->access_token) || empty($tokens->access_token)) if($tokens === null || !isset($tokens->access_token) || empty($tokens->access_token))
throw new Exception ("invalid token data"); throw new Exception ("invalid token data");
$this->tokens = $tokens; $this->tokens = $tokens;
} }
curl_close($this->curl);
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment