-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit a2d15d9
Showing
27 changed files
with
1,160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
## Setup | ||
|
||
Install package | ||
|
||
```bash | ||
composer require contributte/oauth2-client | ||
``` | ||
|
||
## Supported flows | ||
|
||
Take a look at [integration](#integration) for usage | ||
|
||
|
||
- Implemented package [league/oauth2-google](https://github.com/thephpleague/oauth2-google) | ||
- [Credentials source](https://developers.google.com/identity/protocols/OpenIDConnect#registeringyourapp) | ||
- Flow registration | ||
```yaml | ||
services: | ||
- Contributte\OAuth2Client\Flow\Google\GoogleProvider([ | ||
clientId: | ||
clientSecret: | ||
]) | ||
- Contributte\OAuth2Client\Flow\Google\GoogleAuthCodeFlow | ||
``` | ||
- Implemented package [league/oauth2-facebook](https://github.com/thephpleague/oauth2-facebook) | ||
- [Credentials source](https://developers.facebook.com/docs/facebook-login/overview) | ||
- Flow registration | ||
```yaml | ||
services: | ||
- Contributte\OAuth2Client\Flow\Facebook\FacebookProvider([ | ||
clientId: | ||
clientSecret: | ||
graphApiVersion: v3.2 | ||
]) | ||
- Contributte\OAuth2Client\Flow\Facebook\Facebook\AuthCodeFlow | ||
``` | ||
### Others | ||
You could implement other providers which support auth code authentication by extending `Contributte\OAuth2Client\Flow\AuthCodeFlow`. Other authentication methods are currently not supported (PR is welcome). | ||
|
||
List of all providers is [here](https://github.com/thephpleague/oauth2-client/blob/master/docs/providers/thirdparty.md) | ||
|
||
## Integration | ||
|
||
This example uses Google as provider with integration through [league/oauth2-google](https://github.com/thephpleague/oauth2-google) | ||
|
||
Install package | ||
|
||
```bash | ||
composer require league/oauth2-google | ||
``` | ||
|
||
Get your oauth2 credentials (`clientId` and `clientSecret`) from [Google website](https://developers.google.com/identity/protocols/OpenIDConnect#registeringyourapp) | ||
|
||
Register flow | ||
|
||
```yaml | ||
services: | ||
- Contributte\OAuth2Client\Flow\Google\GoogleProvider([ | ||
clientId: | ||
clientSecret: | ||
]) | ||
- Contributte\OAuth2Client\Flow\Google\GoogleAuthCodeFlow | ||
``` | ||
|
||
Create a control which can handle authentication and authorization | ||
|
||
```php | ||
use Contributte\OAuth2Client\Flow\Google\GoogleAuthCodeFlow; | ||
use League\OAuth2\Client\Provider\Exception\IdentityProviderException; | ||
use League\OAuth2\Client\Provider\GoogleUser; | ||
use Nette\Application\UI\Control; | ||
class GoogleButton extends Control | ||
{ | ||
/** @var GoogleAuthCodeFlow */ | ||
private $flow; | ||
public function __construct(GoogleAuthCodeFlow $flow) | ||
{ | ||
parent::__construct(); | ||
$this->flow = $flow; | ||
} | ||
public function authenticate(): void | ||
{ | ||
$this->flow->getProvider()->setRedirectUri( | ||
$this->presenter->link('//:Sign:googleAuthorize') | ||
); | ||
$this->presenter->redirectUrl($this->flow->getAuthorizationUrl()); | ||
} | ||
public function authorize(array $parameters): void | ||
{ | ||
// Setup propel redirect URL | ||
$this->flow->getProvider()->setRedirectUri( | ||
$this->presenter->link('//:Sign:googleAuthorize') | ||
); | ||
try { | ||
$accessToken = $this->flow->getAccessToken($parameters); | ||
} catch (IdentityProviderException $e) { | ||
// TODO - Identity provider failure, cannot get information about user | ||
} | ||
/** @var GoogleUser $owner */ | ||
$owner = $this->flow->getProvider()->getResourceOwner($accessToken); | ||
// TODO - try sign in user with it's email ($owner->getEmail()) | ||
} | ||
} | ||
``` | ||
|
||
Add control to sign presenter | ||
|
||
```php | ||
use Nette\Application\UI\Presenter; | ||
class SignPresenter extends Presenter | ||
{ | ||
public function actionGoogleAuthenticate(): void | ||
{ | ||
$this['googleButton']->authenticate(); | ||
} | ||
public function actionGoogleAuthorize(): void | ||
{ | ||
$this['googleButton']->authorize($this->getHttpRequest()->getQuery()); | ||
} | ||
protected function createComponentGoogleButton(): GoogleButton | ||
{ | ||
// TODO - create and return GoogleButton control | ||
} | ||
} | ||
``` | ||
|
||
Create link to authentication action | ||
|
||
```latte | ||
<a href="{plink :Front:Sign:googleAuthenticate}">Sign in with Google</a> | ||
``` | ||
|
||
That's all! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# EditorConfig is awesome: http://EditorConfig.org | ||
|
||
root = true | ||
|
||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
trim_trailing_whitespace = true | ||
indent_style = tab | ||
indent_size = tab | ||
tab_width = 4 | ||
|
||
[{*.json,*.yml,*.md}] | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# Not archived | ||
.docs export-ignore | ||
tests export-ignore | ||
.editorconfig export-ignore | ||
.gitattributes export-ignore | ||
.gitignore export-ignore | ||
.travis.yml export-ignore | ||
Makefile export-ignore | ||
phpstan.neon export-ignore | ||
README.md export-ignore | ||
ruleset.xml export-ignore |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# IDE | ||
/.idea | ||
|
||
# Composer | ||
/vendor | ||
/composer.lock | ||
|
||
# Tests | ||
/temp | ||
/coverage.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
language: php | ||
php: | ||
- 7.2 | ||
- 7.3 | ||
- 7.4snapshot | ||
- nightly | ||
|
||
before_install: | ||
- phpenv config-rm xdebug.ini || return 0 # Turn off XDebug | ||
|
||
install: | ||
- travis_retry composer install --no-progress --prefer-dist # Install dependencies | ||
|
||
script: | ||
- make tests # Tests | ||
|
||
jobs: | ||
include: | ||
- env: title="Lowest Dependencies 7.2" | ||
php: 7.2 | ||
install: | ||
- travis_retry composer update --no-progress --prefer-dist --prefer-lowest --prefer-stable | ||
script: | ||
- make tests | ||
|
||
- stage: Quality Assurance | ||
php: 7.3 | ||
script: | ||
- make qa | ||
|
||
- stage: Test Coverage | ||
if: branch = master AND type = push | ||
php: 7.3 | ||
script: | ||
- make coverage | ||
after_script: | ||
- composer global require php-coveralls/php-coveralls ^2.1.0 | ||
- ~/.composer/vendor/bin/php-coveralls --verbose --config tests/.coveralls.yml | ||
|
||
- stage: Outdated Dependencies | ||
if: branch = master AND type = cron | ||
php: 7.3 | ||
script: | ||
- composer outdated --direct | ||
|
||
allow_failures: | ||
- stage: Test Coverage | ||
- php: 7.4snapshot | ||
- php: nightly | ||
|
||
sudo: false | ||
|
||
cache: | ||
directories: | ||
- $HOME/.composer/cache |
Oops, something went wrong.