Skip to content

Commit c541be5

Browse files
first
0 parents  commit c541be5

File tree

6 files changed

+347
-0
lines changed

6 files changed

+347
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/build
2+
/vendor
3+
composer.phar
4+
composer.lock
5+
.DS_Store

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Jason Varga
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Dropbox Provider for OAuth 2.0 Client
2+
3+
[![Latest Version](https://img.shields.io/github/release/stevenmaguire/oauth2-dropbox.svg?style=flat-square)](https://github.com/stevenmaguire/oauth2-dropbox/releases)
4+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
5+
[![Build Status](https://img.shields.io/travis/stevenmaguire/oauth2-dropbox/master.svg?style=flat-square)](https://travis-ci.org/stevenmaguire/oauth2-dropbox)
6+
[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/stevenmaguire/oauth2-dropbox.svg?style=flat-square)](https://scrutinizer-ci.com/g/stevenmaguire/oauth2-dropbox/code-structure)
7+
[![Quality Score](https://img.shields.io/scrutinizer/g/stevenmaguire/oauth2-dropbox.svg?style=flat-square)](https://scrutinizer-ci.com/g/stevenmaguire/oauth2-dropbox)
8+
[![Total Downloads](https://img.shields.io/packagist/dt/stevenmaguire/oauth2-dropbox.svg?style=flat-square)](https://packagist.org/packages/stevenmaguire/oauth2-dropbox)
9+
10+
This package provides Dropbox OAuth 2.0 support for the PHP League's [OAuth 2.0 Client](https://github.com/thephpleague/oauth2-client).
11+
12+
## Installation
13+
14+
To install, use composer:
15+
16+
```
17+
composer require stevenmaguire/oauth2-dropbox
18+
```
19+
20+
**Note:** Due API deprecation, we dropped support to Dropbox API v1. If you need use v1, please use `^2.0.0` version constraint:
21+
22+
```
23+
composer require "stevenmaguire/oauth2-dropbox:^2.0.0"
24+
```
25+
26+
## Usage
27+
28+
Usage is the same as The League's OAuth client, using `\Stevenmaguire\OAuth2\Client\Provider\Dropbox` as the provider.
29+
30+
### Authorization Code Flow
31+
32+
```php
33+
$provider = new Stevenmaguire\OAuth2\Client\Provider\Dropbox([
34+
'clientId' => '{dropbox-client-id}',
35+
'clientSecret' => '{dropbox-client-secret}',
36+
'redirectUri' => 'https://example.com/callback-url'
37+
]);
38+
39+
if (!isset($_GET['code'])) {
40+
41+
// If we don't have an authorization code then get one
42+
$authUrl = $provider->getAuthorizationUrl();
43+
$_SESSION['oauth2state'] = $provider->getState();
44+
header('Location: '.$authUrl);
45+
exit;
46+
47+
// Check given state against previously stored one to mitigate CSRF attack
48+
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
49+
50+
unset($_SESSION['oauth2state']);
51+
exit('Invalid state');
52+
53+
} else {
54+
55+
// Try to get an access token (using the authorization code grant)
56+
$token = $provider->getAccessToken('authorization_code', [
57+
'code' => $_GET['code']
58+
]);
59+
60+
// Optional: Now you have a token you can look up a users profile data
61+
try {
62+
63+
// We got an access token, let's now get the user's details
64+
$user = $provider->getResourceOwner($token);
65+
66+
// Use these details to create a new profile
67+
printf('Hello %s!', $user->getId());
68+
69+
} catch (Exception $e) {
70+
71+
// Failed to get user details
72+
exit('Oh dear...');
73+
}
74+
75+
// Use this to interact with an API on the users behalf
76+
echo $token->getToken();
77+
}
78+
```
79+
80+
## Refreshing a Token
81+
Dropbox's OAuth implementation does not use refresh tokens. Access tokens are valid until a user revokes access manually, or until an app deauthorizes itself.
82+
83+
## Testing
84+
85+
``` bash
86+
$ ./vendor/bin/phpunit
87+
```
88+
89+
## Contributing
90+
91+
Please see [CONTRIBUTING](https://github.com/stevenmaguire/oauth2-dropbox/blob/master/CONTRIBUTING.md) for details.
92+
93+
94+
## Credits
95+
96+
- [Steven Maguire](https://github.com/stevenmaguire)
97+
- [All Contributors](https://github.com/stevenmaguire/oauth2-dropbox/contributors)
98+
99+
100+
## License
101+
102+
The MIT License (MIT). Please see [License File](https://github.com/stevenmaguire/oauth2-dropbox/blob/master/LICENSE) for more information.

composer.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "flotfeali/oauth2-payping",
3+
"description": "Payping OAuth 2.0 Client Provider for The PHP League OAuth2-Client",
4+
"license": "MIT",
5+
"authors": [
6+
{
7+
"name": "Farhad Lotfeali",
8+
"email": "flotfeali@gmail.com",
9+
"homepage": "https://github.com/farhad-lotfeali"
10+
}
11+
],
12+
"keywords": [
13+
"oauth",
14+
"oauth2",
15+
"client",
16+
"authorization",
17+
"authorisation",
18+
"payping"
19+
],
20+
"require": {
21+
"league/oauth2-client": "^2.0"
22+
},
23+
"require-dev": {
24+
"phpunit/phpunit": "~4.0",
25+
"mockery/mockery": "~0.9",
26+
"squizlabs/php_codesniffer": "~2.0"
27+
},
28+
"autoload": {
29+
"psr-4": {
30+
"PayPing\\OAuth2\\Client\\": "src/"
31+
}
32+
},
33+
"autoload-dev": {
34+
"psr-4": {
35+
"PayPing\\OAuth2\\Client\\Test\\": "tests/src/"
36+
}
37+
}
38+
}

src/Provider/PayPing.php

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
namespace PayPing\OAuth2\Client\Provider;
4+
5+
use League\OAuth2\Client\Provider\AbstractProvider;
6+
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
7+
use League\OAuth2\Client\Token\AccessToken;
8+
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
9+
use Psr\Http\Message\ResponseInterface;
10+
11+
class PayPing extends AbstractProvider
12+
{
13+
use BearerAuthorizationTrait;
14+
15+
/**
16+
* @var string Key used in the access token response to identify the resource owner.
17+
*/
18+
const ACCESS_TOKEN_RESOURCE_OWNER_ID = 'account_id';
19+
20+
/**
21+
* Get authorization url to begin OAuth flow
22+
*
23+
* @return string
24+
*/
25+
public function getBaseAuthorizationUrl()
26+
{
27+
return 'https://oauth.payping.ir/connect/authorize';
28+
}
29+
30+
/**
31+
* Get access token url to retrieve token
32+
*
33+
* @return string
34+
*/
35+
public function getBaseAccessTokenUrl(array $params)
36+
{
37+
return 'https://oauth.payping.ir/connect/token';
38+
}
39+
40+
/**
41+
* Get provider url to fetch user details
42+
*
43+
* @param AccessToken $token
44+
*
45+
* @return string
46+
*/
47+
public function getResourceOwnerDetailsUrl(AccessToken $token)
48+
{
49+
return 'https://oauth.payping.ir/connect/userinfo';
50+
}
51+
52+
/**
53+
* Get the default scopes used by this provider.
54+
*
55+
* This should not be a complete list of all scopes, but the minimum
56+
* required for the provider user interface!
57+
*
58+
* @return array
59+
*/
60+
protected function getDefaultScopes()
61+
{
62+
return ['openid'];
63+
}
64+
65+
/**
66+
* Check a provider response for errors.
67+
*
68+
* @link https://www.dropbox.com/developers/core/docs
69+
* @throws IdentityProviderException
70+
* @param ResponseInterface $response
71+
* @param string $data Parsed response data
72+
* @return void
73+
*/
74+
protected function checkResponse(ResponseInterface $response, $data)
75+
{
76+
if (isset($data['error'])) {
77+
throw new IdentityProviderException(
78+
$data['error'] ?: $response->getReasonPhrase(),
79+
$response->getStatusCode(),
80+
$response
81+
);
82+
}
83+
}
84+
85+
/**
86+
* Generate a user object from a successful user details request.
87+
*
88+
* @param object $response
89+
* @param AccessToken $token
90+
* @return DropboxResourceOwner
91+
*/
92+
protected function createResourceOwner(array $response, AccessToken $token)
93+
{
94+
return new DropboxResourceOwner($response);
95+
}
96+
97+
/**
98+
* Requests resource owner details.
99+
*
100+
* @param AccessToken $token
101+
* @return mixed
102+
*/
103+
protected function fetchResourceOwnerDetails(AccessToken $token)
104+
{
105+
$url = $this->getResourceOwnerDetailsUrl($token);
106+
107+
$request = $this->getAuthenticatedRequest(self::METHOD_POST, $url, $token);
108+
109+
return $this->getParsedResponse($request);
110+
}
111+
112+
/**
113+
* Builds the authorization URL.
114+
*
115+
* @param array $options
116+
* @return string Authorization URL
117+
*/
118+
public function getAuthorizationUrl(array $options = [])
119+
{
120+
return parent::getAuthorizationUrl(array_merge([
121+
'approval_prompt' => []
122+
], $options));
123+
}
124+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace PayPing\OAuth2\Client\Provider;
4+
5+
use League\OAuth2\Client\Provider\ResourceOwnerInterface;
6+
use League\OAuth2\Client\Tool\ArrayAccessorTrait;
7+
8+
class PayPingResourceOwner implements ResourceOwnerInterface
9+
{
10+
use ArrayAccessorTrait;
11+
/**
12+
* Raw response
13+
*
14+
* @var array
15+
*/
16+
protected $response;
17+
18+
/**
19+
* Creates new resource owner.
20+
*
21+
* @param array $response
22+
*/
23+
public function __construct(array $response = array())
24+
{
25+
$this->response = $response;
26+
}
27+
28+
/**
29+
* Get resource owner id
30+
*
31+
* @return string
32+
*/
33+
public function getId()
34+
{
35+
return $this->getValueByKey($this->response, 'account_id');
36+
}
37+
38+
/**
39+
* Get resource owner name
40+
*
41+
* @return string
42+
*/
43+
public function getName()
44+
{
45+
return $this->getValueByKey($this->response, 'name.display_name');
46+
}
47+
48+
/**
49+
* Return all of the owner details available as an array.
50+
*
51+
* @return array
52+
*/
53+
public function toArray()
54+
{
55+
return $this->response;
56+
}
57+
}

0 commit comments

Comments
 (0)