Skip to content

Commit 990bdfb

Browse files
committed
Initial commit
0 parents  commit 990bdfb

File tree

13 files changed

+942
-0
lines changed

13 files changed

+942
-0
lines changed

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
*.swp
2+
*~
3+
.coveralls.yml
4+
build/
5+
composer.lock
6+
composer.phar
7+
coveralls.phar
8+
test/log
9+
vendor/

.scrutinizer.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
filter:
2+
excluded_paths: [test/*]
3+
checks:
4+
php:
5+
code_rating: true
6+
remove_extra_empty_lines: true
7+
remove_php_closing_tag: true
8+
remove_trailing_whitespace: true
9+
fix_use_statements:
10+
remove_unused: true
11+
preserve_multiple: false
12+
preserve_blanklines: true
13+
order_alphabetically: true
14+
fix_php_opening_tag: true
15+
fix_linefeed: true
16+
fix_line_ending: true
17+
fix_identation_4spaces: true
18+
fix_doc_comments: true
19+
tools:
20+
external_code_coverage:
21+
timeout: 600
22+
runs: 3
23+
php_analyzer: true
24+
php_code_coverage: false
25+
php_code_sniffer:
26+
config:
27+
standard: PSR2
28+
filter:
29+
paths: ['src']
30+
php_loc:
31+
enabled: true
32+
excluded_dirs: [vendor, test]
33+
php_cpd:
34+
enabled: true
35+
excluded_dirs: [vendor, test]
36+

.travis.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
language: php
2+
3+
sudo: false
4+
5+
matrix:
6+
include:
7+
- php: 5.6
8+
env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"'
9+
- php: 7.0
10+
- php: 7.1
11+
- php: nightly
12+
- php: hhvm
13+
sudo: required
14+
dist: trusty
15+
group: edge
16+
- php: hhvm-nightly
17+
sudo: required
18+
dist: trusty
19+
group: edge
20+
fast_finish: true
21+
allow_failures:
22+
- php: nightly
23+
- php: hhvm-nightly
24+
25+
26+
before_script:
27+
- travis_retry composer self-update
28+
- travis_retry composer install --no-interaction --prefer-source --dev
29+
- travis_retry phpenv rehash
30+
31+
script:
32+
- ./vendor/bin/parallel-lint src test
33+
- ./vendor/bin/phpcs src --standard=psr2 -sp
34+
- ./vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
35+
36+
after_success:
37+
- wget https://scrutinizer-ci.com/ocular.phar
38+
- php ocular.phar code-coverage:upload --format=php-clover coverage.clover
39+
- mkdir -p build/logs
40+
- wget https://github.com/satooshi/php-coveralls/releases/download/v1.0.2/coveralls.phar
41+
- travis_retry php coveralls.phar -x coverage.clover

CONTRIBUTING.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Contributing
2+
3+
Contributions are **welcome** and will be fully **credited**.
4+
5+
We accept contributions via Pull Requests on [Github](https://github.com/wohali/oauth2-discord-new).
6+
7+
8+
## Pull Requests
9+
10+
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer).
11+
12+
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
13+
14+
- **Document any change in behaviour** - Make sure the README and any other relevant documentation are kept up-to-date.
15+
16+
- **Consider our release cycle** - We try to follow SemVer. Randomly breaking public APIs is not an option.
17+
18+
- **Create topic branches** - Don't ask us to pull from your master branch.
19+
20+
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
21+
22+
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting.
23+
24+
- **Ensure tests pass!** - Please run the tests (see below) before submitting your pull request, and make sure they pass. We won't accept a patch until all tests pass.
25+
26+
- **Ensure no coding standards violations** - Please run PHP Code Sniffer using the PSR-2 standard (see below) before submitting your pull request. A violation will cause the build to fail, so please make sure there are no violations. We can't accept a patch if the build fails.
27+
28+
29+
## Testing
30+
31+
The following tests must pass for a build to be considered successful. If contributing, please ensure these pass before submitting a pull request. Travis CI will be used to enforce a pass before a merge can occur!
32+
33+
``` bash
34+
$ ./vendor/bin/parallel-lint src test
35+
$ ./vendor/bin/phpunit --coverage-text
36+
$ ./vendor/bin/phpcs src --standard=psr2 -sp
37+
```
38+
39+
**Happy coding**!
40+

LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Joan Touzet <wohali@apache.org>
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.
22+

README.md

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
# Discord Provider for OAuth 2.0 Client
2+
[![Source Code](http://img.shields.io/badge/source-wohali/oauth2--discord--new-blue.svg?style=flat-square)](https://github.com/wohali/oauth2-discord-new)
3+
[![Latest Version](https://img.shields.io/github/release/wohali/oauth2-discord-new.svg?style=flat-square)](https://github.com/wohali/oauth2-discord-new/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/wohali/oauth2-discord-new/master.svg?style=flat-square)](https://travis-ci.org/wohali/oauth2-discord-new)
6+
[![Scrutinizer](https://img.shields.io/scrutinizer/g/wohali/oauth2-discord-new/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/wohali/oauth2-discord-new)
7+
[![Coverage Status](https://img.shields.io/coveralls/wohali/oauth2-discord-new/master.svg?style=flat-square)](https://coveralls.io/r/wohali/oauth2-discord-new?branch=master)
8+
[![Total Downloads](https://img.shields.io/packagist/dt/wohali/oauth2-discord-new.svg?style=flat-square)](https://packagist.org/packages/wohali/oauth2-discord-new)
9+
10+
This package provides Discord OAuth 2.0 support for the PHP League's [OAuth 2.0 Client](https://github.com/thephpleague/oauth2-client), v2.0 and up.
11+
12+
## Requirements
13+
14+
The following versions of PHP are supported.
15+
16+
* PHP 5.6
17+
* PHP 7.0
18+
* PHP 7.1
19+
* HHVM
20+
21+
## Installation
22+
23+
To install, use composer:
24+
25+
```bash
26+
$ composer require wohali/oauth2-discord-new
27+
```
28+
29+
## Usage
30+
31+
Usage is the same as The League's OAuth client, using `\Wohali\OAuth2\Client\Provider\Discord` as the provider.
32+
33+
### Sample Authorization Code Flow
34+
35+
This self-contained example:
36+
37+
1. Gets an authorization code
38+
1. Gets an access token using the provided authorization code
39+
1. Looks up the user's provile with the provided access token
40+
41+
You can try this script by [registering a Discord App](https://discordapp.com/developers/applications/me/create) with a redirect URI to your server's copy of this sample script. Then, place the Discord app's client id and secret, along with that same URI, into the settings at the top of the script.
42+
43+
```php
44+
<?php
45+
46+
require __DIR__ . '/vendor/autoload.php';
47+
48+
session_start();
49+
50+
echo ('Main screen turn on!<br/><br/>');
51+
52+
$provider = new \Wohali\OAuth2\Client\Provider\Discord([
53+
'clientId' => '{discord-client-id}',
54+
'clientSecret' => '{discord-client-secret}',
55+
'redirecturi' => '{your-server-uri-to-this-script-here}'
56+
]);
57+
58+
if (!isset($_GET['code'])) {
59+
60+
// Step 1. Get authorization code
61+
$authUrl = $provider->getAuthorizationUrl();
62+
$_SESSION['oauth2state'] = $provider->getState();
63+
header('Location: ' . $authUrl);
64+
65+
// Check given state against previously stored one to mitigate CSRF attack
66+
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
67+
68+
unset($_SESSION['oauth2state']);
69+
exit('Invalid state');
70+
71+
} else {
72+
73+
// Step 2. Get an access token using the provided authorization code
74+
$token = $provider->getAccessToken('authorization_code', [
75+
'code' => $_GET['code']
76+
]);
77+
78+
// Show some token details
79+
echo '<h2>Token details:</h2>';
80+
echo 'Token: ' . $token->getToken() . "<br/>";
81+
echo 'Refresh token: ' . $token->getRefreshToken() . "<br/>";
82+
echo 'Expires: ' . $token->getExpires() . " - ";
83+
echo ($token->hasExpired() ? 'expired' : 'not expired') . "<br/>";
84+
85+
// Step 3. (Optional) Look up the user's profile with the provided token
86+
try {
87+
88+
$user = $provider->getResourceOwner($token);
89+
90+
echo '<h2>Resource owner details:</h2>';
91+
printf('Hello %s#%s!<br/><br/>', $user->getUsername(), $user->getDiscriminator());
92+
var_export($user->toArray());
93+
94+
} catch (Exception $e) {
95+
96+
// Failed to get user details
97+
exit('Oh dear...');
98+
99+
}
100+
}
101+
```
102+
103+
### Managing Scopes
104+
105+
When creating your Discord authorization URL in Step 1, you can specify the state and scopes your application may authorize.
106+
107+
```php
108+
$options = [
109+
'state' => 'OPTIONAL_CUSTOM_CONFIGURED_STATE',
110+
'scope' => ['identify', 'email', '...'] // array or string
111+
];
112+
113+
$authorizationUrl = $provider->getAuthorizationUrl($options);
114+
```
115+
If neither are defined, the provider will utilize internal defaults.
116+
117+
At the time of authoring this documentation, the [following scopes are available](https://discordapp.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes):
118+
119+
- bot
120+
- connections
121+
- email
122+
- identify
123+
- guilds
124+
- guilds.join
125+
- gdm.join
126+
- messages.read
127+
- rpc
128+
- rpc.api
129+
- rpc.notifications.read
130+
- webhook.incoming
131+
132+
### Refreshing a Token
133+
134+
You can refresh an expired token using a refresh token rather than going through the entire process of obtaining a brand new token. To do so, simply reuse the fresh token from your data store to request a refresh:
135+
136+
```php
137+
// create $provider as in the initial example
138+
$existingAccessToken = getAccessTokenFromYourDataStore();
139+
140+
if ($existingAccessToken->hasExpired()) {
141+
$newAccessToken = $provider->getAccessToken('refresh_token', [
142+
'refresh_token' => $existingAccessToken->getRefreshToken()
143+
]);
144+
145+
// Purge old access token and store new access token to your data store.
146+
}
147+
```
148+
149+
### Client Credentials Grant
150+
151+
Discord provides a client credentials flow for bot developers to get their own bearer tokens for testing purposes. This returns an access token for the *bot owner*:
152+
153+
```php
154+
// create $provider as in the initial example
155+
try {
156+
157+
// Try to get an access token using the client credentials grant.
158+
$accessToken = $provider->getAccessToken('client_credentials');
159+
160+
} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
161+
162+
// Failed to get the access token
163+
exit($e->getMessage());
164+
165+
}
166+
```
167+
168+
### Bot Authorization
169+
170+
To authorize a bot, specify a scope of `bot` and set [permissions](https://discordapp.com/developers/docs/topics/permissions#permissions-bitwise-permission-flags) appropriately:
171+
172+
```php
173+
// create $provider as in the initial example
174+
175+
$options = [
176+
'scope' => ['bot'],
177+
'permissions' => 1
178+
];
179+
180+
$authorizationUrl = $provider->getAuthorizationUrl($options);
181+
182+
// Redirect user to authorization page
183+
header('Location: ' . $authUrl);
184+
```
185+
186+
## Testing
187+
188+
``` bash
189+
$ ./vendor/bin/parallel-lint src test
190+
$ ./vendor/bin/phpcs src --standard=psr2 -sp
191+
$ ./vendor/bin/phpunit --coverage-text
192+
```
193+
194+
## Contributing
195+
196+
Please see [CONTRIBUTING](https://github.com/wohali/oauth2-discord-new/blob/master/CONTRIBUTING.md) for details.
197+
198+
## Credits
199+
200+
- [Joan Touzet](https://github.com/wohali)
201+
- [All Contributors](https://github.com/wohali/oauth2-discord-new/contributors)
202+
203+
## License
204+
205+
The MIT License (MIT). Please see [License File](https://github.com/wohali/oauth2-discord-new/blob/master/LICENSE) for more information.

0 commit comments

Comments
 (0)