Skip to content

Commit

Permalink
Merge pull request #1 from davicente/integrating_oauth2
Browse files Browse the repository at this point in the history
Adding OAuth2 methods (getting Authorization code, Access token... ) …
  • Loading branch information
davicente authored Apr 18, 2017
2 parents 0ba9725 + 410e756 commit f59d7d3
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 1 deletion.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"homepage": "https://github.com/davicente/flysystem-onedrive",
"require": {
"league/flysystem": "^1.0",
"guzzlehttp/guzzle": "^6.1"
"guzzlehttp/guzzle": "^6.1",
"stevenmaguire/oauth2-microsoft": "^2.0"
},
"require-dev": {
"phpunit/phpunit": "^5.2"
Expand Down
88 changes: 88 additions & 0 deletions src/OAuth2/OAuth2Provider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace JacekBarecki\FlysystemOneDrive\OAuth2;


class OAuth2Provider
{

/**
* @param string $clientId
* @param string $clientSecret
* @param string $redirectUri
*/
private static function getProvider($clientId, $clientSecret, $redirectUri) {
$provider = new \Stevenmaguire\OAuth2\Client\Provider\Microsoft([
'clientId' => $clientId,
'clientSecret' => $clientSecret,
'redirectUri' => $redirectUri
]);
return $provider;
}


/**
* @param string $clientId
* @param string $clientSecret
* @param string $redirectUri
*
* Url returned looks like this:
* https://login.live.com/oauth20_authorize.srf?state=OPTIONAL_CUSTOM_CONFIGURED_STATE&scope=wl.basic%2Cwl.signin%2Cwl.skydrive_update%2Cwl.offline_access&response_type=code&approval_prompt=auto&redirect_uri=https%3A%2F%2Fxxxxxxxx.com%2Fpath&client_id=9828281-18237-xxxxxxx
*/
public static function getAuthorization($clientId, $clientSecret, $redirectUri) {
$provider = OAuth2Provider::getProvider($clientId, $clientSecret, $redirectUri);

//Set the scopes needed for OneDrive API
$options = [
'state' => 'OPTIONAL_CUSTOM_CONFIGURED_STATE',
'scope' => ['wl.basic', 'wl.signin', 'wl.skydrive_update', 'wl.offline_access'] // array or string
];

return $provider->getAuthorizationUrl($options);
}


/**
* @param string $clientId
* @param string $clientSecret
* @param string $redirectUri
* @param string $code
*
* To extract the access token: $token->getToken();
* To extract the refresh token: $token->getRefreshToken();
*/
public static function getTokenFromCode($clientId, $clientSecret, $redirectUri, $code) {
$provider = OAuth2Provider::getProvider($clientId, $clientSecret, $redirectUri);

// Try to get an access token (using the authorization code grant)
$token = $provider->getAccessToken('authorization_code', [
'code' => $code
]);

return $token;
}


/**
* @param string $clientId
* @param string $clientSecret
* @param string $redirectUri
* @param string $refreshToken
*
* To extract the access token: $token->getToken();
* To extract the refresh token: $token->getRefreshToken();
*/
public static function getTokenFromRefreshToken($clientId, $clientSecret, $redirectUri, $refreshToken) {
$provider = OAuth2Provider::getProvider($clientId, $clientSecret, $redirectUri);

//Get new access token
$token = $provider->getAccessToken('refresh_token', [
'refresh_token' => $refreshToken
]);

return $token;
}
}


?>

0 comments on commit f59d7d3

Please sign in to comment.