diff --git a/.gitignore b/.gitignore index 42cd73d..14c5def 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/vendor/ \ No newline at end of file +/vendor/ +/examples/vendor/ diff --git a/composer.json b/composer.json index 8d3471b..316c8a6 100644 --- a/composer.json +++ b/composer.json @@ -1,41 +1,43 @@ { - "name": "adnanhussainturki/microsoft-api-php", - "description": "PHP Wrapper the OneDrive API", - "keywords": [ - "onedrive", - "api" - ], - "homepage": "https://github.com/AdnanHussainTurki/microsoft-api-php", - "license": "MIT", - "authors": [ - { - "name": "Adnan Hussain Turki", - "email": "adnanhussainturki@gmail.com", - "homepage": "https://www.myphpnotes.com", - "role": "Developer" - } - ], - "require": { - "php": ">=5.6", - "microsoft/microsoft-graph": "^1.0" - }, - "require-dev": { - "phpunit/phpunit": "^6.0" - }, - "autoload": { - "psr-4": { - "myPHPnotes\\Microsoft\\": "src" - } - }, - "autoload-dev": { - "psr-4": { - "myPHPnotes\\Microsoft\\Test\\": "tests" - } - }, - "scripts": { - "test": "vendor/bin/phpunit" - }, - "config": { - "sort-packages": true + "name": "adnanhussainturki/microsoft-api-php", + "description": "PHP Wrapper the Microsoft Graph API", + "keywords": [ + "sign with microsoft", + "onedrive", + "api" + ], + "homepage": "https://github.com/AdnanHussainTurki/microsoft-api-php", + "license": "MIT", + "authors": [ + { + "name": "Adnan Hussain Turki", + "email": "adnanhussainturki@gmail.com", + "homepage": "https://www.myphpnotes.com", + "role": "Developer" } + ], + "require": { + "php": ">=5.6", + "microsoft/microsoft-graph": "^1.0", + "league/flysystem": "^1.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.0" + }, + "autoload": { + "psr-4": { + "myPHPnotes\\Microsoft\\": "src" + } + }, + "autoload-dev": { + "psr-4": { + "myPHPnotes\\Microsoft\\Test\\": "tests" + } + }, + "scripts": { + "test": "vendor/bin/phpunit" + }, + "config": { + "sort-packages": true + } } diff --git a/examples/callback.php b/examples/callback.php new file mode 100644 index 0000000..fc20bde --- /dev/null +++ b/examples/callback.php @@ -0,0 +1,26 @@ +getToken($_REQUEST['code'], $_REQUEST['state']); + +$accessToken = $tokens->access_token; + +$auth->setAccessToken($accessToken); + +$user = new User(); +echo 'Name: ' . $user->data->getDisplayName() . '
'; +echo 'Email: ' . $user->data->getUserPrincipalName() . '
'; diff --git a/examples/composer.json b/examples/composer.json new file mode 100644 index 0000000..132680a --- /dev/null +++ b/examples/composer.json @@ -0,0 +1,6 @@ +{ + "require": { + "adnanhussainturki/microsoft-api-php": "^0.03.0", + "league/flysystem": "^2.0" + } +} diff --git a/examples/index.php b/examples/index.php new file mode 100644 index 0000000..0484195 --- /dev/null +++ b/examples/index.php @@ -0,0 +1,11 @@ + + + + + + Sign in with Microsoft + + + Sign in with Microsoft + + \ No newline at end of file diff --git a/examples/signin.php b/examples/signin.php new file mode 100644 index 0000000..30d9e18 --- /dev/null +++ b/examples/signin.php @@ -0,0 +1,17 @@ +getAuthUrl()); diff --git a/src/Auth.php b/src/Auth.php index df2428c..156ea77 100644 --- a/src/Auth.php +++ b/src/Auth.php @@ -1,60 +1,72 @@ tenant_id = $tenant_id; $this->client_id = $client_id; $this->client_secret = $client_secret; $this->redirect_uri = $redirect_uri; $this->scopes = $scopes; - Session::set("host", $this->host); - Session::set("resource", $this->resource); - Session::set("tenant_id", $tenant_id); - Session::set("client_id", $client_id); - Session::set("client_secret", $client_secret); - Session::set("redirect_uri", $redirect_uri); - Session::set("scopes", $scopes); + Session::set('host', $this->host); + Session::set('resource', $this->resource); + Session::set('tenant_id', $tenant_id); + Session::set('client_id', $client_id); + Session::set('client_secret', $client_secret); + Session::set('redirect_uri', $redirect_uri); + Session::set('scopes', $scopes); if (!Session::get('state')) { - Session::set("state", random_int(1, 200000)); + Session::set('state', random_int(1, 200000)); } - $this->guzzle = new \GuzzleHttp\Client(); + $this->guzzle = new \GuzzleHttp\Client([ + 'verify' => $sslVerify, + ]); } - public function setRefreshToken(string $refreshToken) + + public function setRefreshToken(string $refreshToken) { $this->refreshToken = $refreshToken; - Session::set("refreshToken", $this->refreshToken); - return Session::get("refreshToken"); + Session::set('refreshToken', $this->refreshToken); + return Session::get('refreshToken'); } public function getAccessTokenUsingRefreshToken(string $refreshToken = null) { if ($refreshToken) { $this->setRefreshToken($refreshToken); } - $url = $this->host. $this->tenant_id ."/oauth2/v2.0/token"; - $tokens = $this->guzzle->post($url, [ - 'form_params' => [ - 'client_id' => Session::get("client_id"), - 'client_secret' => Session::get("client_secret"), - 'grant_type' => 'refresh_token', - 'refresh_token' => Session::get("refreshToken") - ], - ])->getBody()->getContents(); + $url = $this->host . $this->tenant_id . '/oauth2/v2.0/token'; + $tokens = $this->guzzle + ->post($url, [ + 'form_params' => [ + 'client_id' => Session::get('client_id'), + 'client_secret' => Session::get('client_secret'), + 'grant_type' => 'refresh_token', + 'refresh_token' => Session::get('refreshToken'), + ], + ]) + ->getBody() + ->getContents(); return json_decode($tokens)->access_token; } public function setAccessToken(string $accessToken = null) @@ -64,8 +76,8 @@ public function setAccessToken(string $accessToken = null) } else { $this->accessToken = trim($accessToken); } - Session::set("accessToken", $this->accessToken); - return Session::get("accessToken"); + Session::set('accessToken', $this->accessToken); + return Session::get('accessToken'); } public function getAuthUrl() { @@ -75,29 +87,36 @@ public function getAuthUrl() 'redirect_uri' => $this->redirect_uri, 'response_mode' => 'query', 'scope' => implode(' ', $this->scopes), - 'state' => Session::get("state") + 'state' => Session::get('state'), ]; - return $this->host . $this->tenant_id . "/oauth2/v2.0/authorize?". http_build_query($parameters); + return $this->host . + $this->tenant_id . + '/oauth2/v2.0/authorize?' . + http_build_query($parameters); } public function getToken(string $code, string $state = null) { if (!is_null($state)) { - if (Session::get("state") != $state) { - throw new \Exception("State parameter does not matched.", 1); + if (Session::get('state') != $state) { + throw new \Exception('State parameter does not matched.', 1); return false; } } - $url = $this->host. $this->tenant_id ."/oauth2/v2.0/token"; - $tokens = $this->guzzle->post($url, [ - 'form_params' => [ - 'client_id' => $this->client_id, - 'client_secret' => $this->client_secret, - 'redirect_uri' => $this->redirect_uri, - 'scope' => implode(' ', $this->scopes), - 'grant_type' => 'authorization_code', - 'code' => $code - ], - ])->getBody()->getContents(); + $url = $this->host . $this->tenant_id . '/oauth2/v2.0/token'; + $tokens = $this->guzzle + ->post($url, [ + 'form_params' => [ + 'client_id' => $this->client_id, + 'client_secret' => $this->client_secret, + 'redirect_uri' => $this->redirect_uri, + 'scope' => implode(' ', $this->scopes), + 'grant_type' => 'authorization_code', + 'code' => $code, + ], + ]) + ->getBody() + ->getContents(); + Session::unset('state'); return json_decode($tokens); } } diff --git a/src/Handlers/Session.php b/src/Handlers/Session.php index 673e8ae..1ef5f80 100644 --- a/src/Handlers/Session.php +++ b/src/Handlers/Session.php @@ -1,9 +1,8 @@ get($key)) { + if (Session::get($key)) { unset($_SESSION['adnanhussainturki/microsoft'][$key]); } } public static function get($key) { - return (isset($_SESSION['adnanhussainturki/microsoft'][$key]) ? $_SESSION['adnanhussainturki/microsoft'][$key] : null) ; + return isset($_SESSION['adnanhussainturki/microsoft'][$key]) + ? $_SESSION['adnanhussainturki/microsoft'][$key] + : null; } -} \ No newline at end of file +}