-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
1 parent
07bf5e8
commit 796e66b
Showing
30 changed files
with
806 additions
and
44 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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
# Yii Framework authentication client extension Change Log | ||
# Yii Framework External Authentication Extension Change Log | ||
|
||
1.1.0 under development | ||
----------------------- | ||
## 1.1.0 under development | ||
|
||
- Initial release. |
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
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 |
---|---|---|
@@ -1 +1,6 @@ | ||
# Upgrading Instructions for Yii Framework external authentication via OAuth and OpenID Extension | ||
# Yii Framework External Authentication Extension Upgrading Instructions | ||
|
||
This file contains the upgrade notes. These notes highlight changes that could break your | ||
application when you upgrade the package from one version to another. | ||
|
||
## Changes summary |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,22 @@ | ||
AuthClient Extension for Yii | ||
============================ | ||
|
||
This extension adds [OAuth](https://oauth.net/) and [OAuth2](https://oauth.net/2/) consumers | ||
for the Yii framework 3.0. | ||
|
||
|
||
Getting Started | ||
--------------- | ||
|
||
* [Installation](installation.md) | ||
* [Quick Start](quick-start.md) | ||
|
||
Additional topics | ||
----------------- | ||
|
||
* [Third Party Auth Clients](third-party-auth-clients.md) | ||
* [Getting additional data via extra API calls](usage-api.md) | ||
* [Creating your own auth clients](creating-your-own-auth-clients.md) | ||
* [OAuth 2.0 direct authentication](oauth-direct-authentication.md) | ||
* [OpenID Connect](open-id-connect.md) | ||
* [Setup HTTP Client](setup-http-client.md) |
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,115 @@ | ||
Creating your own auth clients | ||
============================== | ||
|
||
You may create your own auth client for any external auth provider, which supports | ||
OpenId or OAuth protocol. To do so, first of all, you need to find out which protocol is | ||
supported by the external auth provider, this will give you the name of the base class | ||
for your extension: | ||
|
||
- For OAuth 2 use [[Yiisoft\Yii\AuthClient\OAuth2]]. | ||
- For OAuth 1/1.0a use [[Yiisoft\Yii\AuthClient\OAuth1]]. | ||
- For OpenID use [[Yiisoft\Yii\AuthClient\OpenId]]. | ||
|
||
At this stage you can determine auth client default name, title and view options, declaring | ||
corresponding methods: | ||
|
||
```php | ||
use Yiisoft\Yii\AuthClient\OAuth2; | ||
|
||
class MyAuthClient extends OAuth2 | ||
{ | ||
protected function defaultName() | ||
{ | ||
return 'my_auth_client'; | ||
} | ||
|
||
protected function defaultTitle() | ||
{ | ||
return 'My Auth Client'; | ||
} | ||
|
||
protected function defaultViewOptions() | ||
{ | ||
return [ | ||
'popupWidth' => 800, | ||
'popupHeight' => 500, | ||
]; | ||
} | ||
} | ||
``` | ||
|
||
Depending on actual base class, you will need to redeclare different fields and methods. | ||
|
||
## [[Yiisoft\Yii\AuthClient\OAuth2]] | ||
|
||
You will need to specify: | ||
|
||
- Auth URL by redeclaring [[Yiisoft\Yii\AuthClient\OAuth2::authUrl|authUrl]] field. | ||
- Token request URL by redeclaring [[Yiisoft\Yii\AuthClient\OAuth2::tokenUrl|tokenUrl]] field. | ||
- API base URL by redeclaring [[Yiisoft\Yii\AuthClient\OAuth2::apiBaseUrl|apiBaseUrl]] field. | ||
- User attribute fetching strategy by redeclaring [[Yiisoft\Yii\AuthClient\OAuth2::initUserAttributes()|initUserAttributes()]] | ||
method. | ||
|
||
For example: | ||
|
||
```php | ||
use Yiisoft\Yii\AuthClient\OAuth2; | ||
|
||
class MyAuthClient extends OAuth2 | ||
{ | ||
public $authUrl = 'https://www.my.com/oauth2/auth'; | ||
|
||
public $tokenUrl = 'https://www.my.com/oauth2/token'; | ||
|
||
public $apiBaseUrl = 'https://www.my.com/apis/oauth2/v1'; | ||
|
||
protected function initUserAttributes() | ||
{ | ||
return $this->api('userinfo', 'GET'); | ||
} | ||
} | ||
``` | ||
|
||
You may also specify default auth scopes. | ||
|
||
> Note: Some OAuth providers may not follow OAuth standards clearly, introducing | ||
differences, and may require additional efforts to implement clients for. | ||
|
||
## [[Yiisoft\Yii\AuthClient\OAuth1]] | ||
|
||
You will need to specify: | ||
|
||
- Auth URL by redeclaring [[Yiisoft\Yii\AuthClient\OAuth1::authUrl|authUrl]] field. | ||
- Request token URL by redeclaring [[Yiisoft\Yii\AuthClient\OAuth1::requestTokenUrl|requestTokenUrl]] field. | ||
- Access token URL by redeclaring [[Yiisoft\Yii\AuthClient\OAuth1::accessTokenUrl|accessTokenUrl]] field. | ||
- API base URL by redeclaring [[Yiisoft\Yii\AuthClient\OAuth1::apiBaseUrl|apiBaseUrl]] field. | ||
- User attribute fetching strategy by redeclaring [[Yiisoft\Yii\AuthClient\OAuth1::initUserAttributes()|initUserAttributes()]] | ||
method. | ||
|
||
For example: | ||
|
||
```php | ||
use Yiisoft\Yii\AuthClient\OAuth1; | ||
|
||
class MyAuthClient extends OAuth1 | ||
{ | ||
public $authUrl = 'https://www.my.com/oauth/auth'; | ||
|
||
public $requestTokenUrl = 'https://www.my.com/oauth/request_token'; | ||
|
||
public $accessTokenUrl = 'https://www.my.com/oauth/access_token'; | ||
|
||
public $apiBaseUrl = 'https://www.my.com/apis/oauth/v1'; | ||
|
||
protected function initUserAttributes() | ||
{ | ||
return $this->api('userinfo', 'GET'); | ||
} | ||
} | ||
``` | ||
|
||
You may also specify default auth scopes. | ||
|
||
> Note: Some OAuth providers may not follow OAuth standards clearly, introducing | ||
differences, and may require additional efforts to implement clients for. | ||
|
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,107 @@ | ||
Installation | ||
============ | ||
|
||
## Installing an extension | ||
|
||
In order to install extension use Composer. Either run: | ||
|
||
``` | ||
composer require --prefer-dist yiisoft/yii-auth-client "~3.0.0" | ||
``` | ||
|
||
or add | ||
|
||
```json | ||
"yiisoft/yii-auth-client": "~3.0.0" | ||
``` | ||
|
||
to the `require` section of your composer.json. | ||
|
||
## Configuring application | ||
|
||
After extension is installed you need to setup auth client collection application component: | ||
|
||
```php | ||
return [ | ||
'authClients' => [ | ||
'google' => [ | ||
'class' => Yiisoft\Yii\AuthClient\Client\Google::class, | ||
'setClientId' => ['google_client_id'], | ||
'setClientSecret' => ['google_client_secret'], | ||
], | ||
'facebook' => [ | ||
'class' => Yiisoft\Yii\AuthClient\Client\Facebook::class, | ||
'setClientId' => ['facebook_client_id'], | ||
'setClientSecret' => ['facebook_client_secret'], | ||
], | ||
// etc. | ||
], | ||
// ... | ||
]; | ||
``` | ||
|
||
Out of the box the following clients are provided: | ||
|
||
- [[\Yiisoft\Yii\AuthClient\Client\Facebook|Facebook]]. | ||
- [[Yiisoft\Yii\AuthClient\Client\GitHub|GitHub]]. | ||
- Google (via [[Yiisoft\Yii\AuthClient\Client\Google|OAuth]] and [[Yiisoft\Yii\AuthClient\Client\GoogleHybrid|OAuth Hybrid]]). | ||
- [[Yiisoft\Yii\AuthClient\Client\LinkedIn|LinkedIn]]. | ||
- [[Yiisoft\Yii\AuthClient\Client\Live|Microsoft Live]]. | ||
- [[Yiisoft\Yii\AuthClient\Client\Twitter|Twitter]]. | ||
- [[Yiisoft\Yii\AuthClient\Client\VKontakte|VKontakte]]. | ||
- [[Yiisoft\Yii\AuthClient\Client\Yandex|Yandex]]. | ||
|
||
Configuration for each client is a bit different. For OAuth it's required to get client ID and secret key from | ||
the service you're going to use. For OpenID it works out of the box in most cases. | ||
|
||
## Storing authorization data | ||
|
||
In order to recognize the user authenticated via external service we need to store ID provided on first authentication | ||
and then check against it on subsequent authentications. It's not a good idea to limit login options to external | ||
services only since these may fail and there won't be a way for the user to log in. Instead it's better to provide | ||
both external authentication and good old login and password. | ||
|
||
If we're storing user information in a database the corresponding migration code could be the following: | ||
|
||
```php | ||
class m??????_??????_auth extends \Yiisoft\Db\Migration | ||
{ | ||
public function up() | ||
{ | ||
$this->createTable('user', [ | ||
'id' => $this->primaryKey(), | ||
'username' => $this->string()->notNull(), | ||
'auth_key' => $this->string()->notNull(), | ||
'password_hash' => $this->string()->notNull(), | ||
'password_reset_token' => $this->string()->notNull(), | ||
'email' => $this->string()->notNull(), | ||
'status' => $this->smallInteger()->notNull()->defaultValue(10), | ||
'created_at' => $this->integer()->notNull(), | ||
'updated_at' => $this->integer()->notNull(), | ||
]); | ||
|
||
$this->createTable('auth', [ | ||
'id' => $this->primaryKey(), | ||
'user_id' => $this->integer()->notNull(), | ||
'source' => $this->string()->notNull(), | ||
'source_id' => $this->string()->notNull(), | ||
]); | ||
|
||
$this->addForeignKey('fk-auth-user_id-user-id', 'auth', 'user_id', 'user', 'id', 'CASCADE', 'CASCADE'); | ||
} | ||
|
||
public function down() | ||
{ | ||
$this->dropTable('auth'); | ||
$this->dropTable('user'); | ||
} | ||
} | ||
``` | ||
|
||
In the above example `user` is a standard table that is used in advanced project template to store user info. | ||
Each user can authenticate using multiple external services therefore each `user` record can relate to | ||
multiple `auth` records. In the `auth` table `source` is the name of the auth provider used and `source_id` is | ||
unique user identifier that is provided by external service after successful login. | ||
|
||
Using tables created above we can generate `Auth` model. No further adjustments needed. | ||
|
Oops, something went wrong.