Skip to content

Commit

Permalink
Support for PHP 8.2 & Examples
Browse files Browse the repository at this point in the history
  • Loading branch information
AdnanHussainTurki committed Oct 21, 2023
1 parent c7c7fb1 commit 4a5b5b3
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 89 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/vendor/
/vendor/
/examples/vendor/
78 changes: 40 additions & 38 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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": "[email protected]",
"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": "[email protected]",
"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
}
}
26 changes: 26 additions & 0 deletions examples/callback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use myPHPnotes\Microsoft\Auth;
use myPHPnotes\Microsoft\Handlers\Session;
use myPHPnotes\Microsoft\Models\User;

session_start();

require 'vendor/autoload.php';

$auth = new Auth(
Session::get('tenant_id'),
Session::get('client_id'),
Session::get('client_secret'),
Session::get('redirect_uri'),
Session::get('scopes')
);
$tokens = $auth->getToken($_REQUEST['code'], $_REQUEST['state']);

$accessToken = $tokens->access_token;

$auth->setAccessToken($accessToken);

$user = new User();
echo 'Name: ' . $user->data->getDisplayName() . '<br>';
echo 'Email: ' . $user->data->getUserPrincipalName() . '<br>';
6 changes: 6 additions & 0 deletions examples/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"require": {
"adnanhussainturki/microsoft-api-php": "^0.03.0",
"league/flysystem": "^2.0"
}
}
11 changes: 11 additions & 0 deletions examples/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sign in with Microsoft</title>
</head>
<body>
<a href="/signin.php">Sign in with Microsoft</a>
</body>
</html>
17 changes: 17 additions & 0 deletions examples/signin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

session_start();

require 'vendor/autoload.php';

use myPHPnotes\Microsoft\Auth;

$tenant = 'common';
$client_id = 'YOUR_CLIENT_ID_HERE';
$client_secret = 'YOUR_CLIENT_SECRET_HERE';
$callback = 'http://localhost:8080/callback.php'; // Your callback URL
$scopes = ['User.Read', 'Files.ReadWrite.All', 'offline_access'];

$microsoft = new Auth($tenant, $client_id, $client_secret, $callback, $scopes);

header('location: ' . $microsoft->getAuthUrl());
109 changes: 64 additions & 45 deletions src/Auth.php
Original file line number Diff line number Diff line change
@@ -1,60 +1,72 @@
<?php


namespace myPHPnotes\Microsoft;

use myPHPnotes\Microsoft\Handlers\Session;


class Auth {
protected $host = "https://login.microsoftonline.com/";
protected $resource = "https://graph.microsoft.com/";
class Auth
{
protected $host = 'https://login.microsoftonline.com/';
protected $resource = 'https://graph.microsoft.com/';
protected $tenant_id;
protected $client_id;
protected $client_secret;
protected $redirect_uri;
protected $scopes;
protected $guzzle;
protected $accessToken;
protected $refreshToken;
public function __construct(string $tenant_id, string $client_id, string $client_secret, string $redirect_uri, array $scopes = [])
{
public function __construct(
string $tenant_id,
string $client_id,
string $client_secret,
string $redirect_uri,
array $scopes = [],
bool $sslVerify = true
) {
$this->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)
Expand All @@ -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()
{
Expand All @@ -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);
}
}
11 changes: 6 additions & 5 deletions src/Handlers/Session.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
<?php


namespace myPHPnotes\Microsoft\Handlers;
/**
*
*
*/
class Session
{
Expand All @@ -13,12 +12,14 @@ public static function set($key, $value)
}
public static function unset($key)
{
if ($this->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;
}
}
}

0 comments on commit 4a5b5b3

Please sign in to comment.