Opauth の使用方法
As said in the introduction of the Opauth extension to Auth, the wrapper class configures itself
automatically for either Simpleauth or Ormauth, based on the driver configured in the auth.php configuration
file.
If you don't use these drivers, but you have enrolled your own driver set in the Auth framework, you can still use the Opauth
integration class, but you'll have to create the 'providers' table defined in the migration 008 manually.
You can name it anything you like. Also, your custom Login driver should provide the methods Auth::check(),
Auth::instance()->get_user_id(), Auth::instance()->force_login() and
Auth::create_user(), with prototypes compatible with Simpleauth. Without these, it will not work.
Runtime configuration
Most of the Opauth configuration is done through the opauth.php configuration file, and all of the options in that
file can be altered at runtime by passing a new value to the forge() using the $config array. In addition to this, there are some
configuration values you can only pass at runtime, they are not supported in the configuration file:
table |
string |
auto detected |
Allows you to define the name of the table that contains the provider information manually. When using Simpleauth or
Ormauth, the name of this table is auto-detected. If you use a custom Auth driver set, and have created the table
manually, you will have to pass this table name on every forge call.
|
path |
string |
auto detected |
URI path to the controller method that does your OAuth authentication. By default, it is set to whatever the current
path is when you forge an Opauth object.
|
callback_url |
string |
auto detected |
URI path to the controller method that handles the OAuth provider callback. By default, it is set to call the 'action_callback'
or 'get_callback' method in the same controller that path points to.
|
provider |
string |
auto detected |
If you have manually set the path, auto detection of the OAuth provider called doesn't work, and you will
have to define the provider name manually. This is case sensitive, and MUST match the case used by the strategy provider
and the key used to define the strategy in the opauth.php configuration file.
|
When using Simpleauth or Ormauth, and you have followed the example code for integrating Opauth in your application,
none of these runtime configuration values are needed, the auto-detected values will do the trick just fine.
Class methods
forge($config = array(), $autorun = true)
The forge method creates an instance of the Auth_Opauth class, which provides OAuth integration to your application.
静的 |
はい |
パラメータ |
パラメータ |
デフォルト |
説明 |
$config |
array()
|
Any runtime configuration values you want to pass to the instance |
$autorun |
true
|
If true, it will automatically initiate a connection to the OAuth provider. If false, it will just instantiate the object. |
|
Returns |
Auth_Opauth |
例 |
/**
* load the wrapper, and have it process the provider defined in the URI
*
* this redirects to the providers authentication page, so it should be the
* last line in your method
*/
\Auth_Opauth::forge();
// forge an object with a runtime config
\Auth_Opauth::forge(array(
'default_group' => $custom_group_id,
));
// forge an object with a runtime config, to process a callback
$opauth = \Auth_Opauth::forge(array(
'default_group' => $custom_group_id,
), false);
|
If you want to process a callback without passing a custom config, you don't have to pass an empty array, you
can use \Auth_Opauth::forge(false); as a shortcut.
login_or_register()
The login_or_register method processes the provider callback. If the returned provider UID is known, the user will
be logged in using the local account it is associated to. If not, the user is asked to either register for a new account, or login with
and existing account and associate the UID with that account.
静的 |
いいえ |
パラメータ |
なし
|
Returns |
string, status code. The following status codes are supported:
- 'linked': the UID is succesfully linked to the current logged-in user
- 'logged_in': the user is succesfully logged-in using the OAuth account
- 'registered': the UID is linked to a newly registered account, and the user is logged-in
- 'register': the UID is not known, and no logged-in user exists, have the user register first
|
例 |
// get the Opauth object
$opauth = \Auth_Opauth::forge(false);
// attempt to auto-login using the provider callback data
$status = $opauth->login_or_register();
|
link_provider(array $data)
The link_provider method allows you to manually link a local user account to an OAuth login. You need this if you want
to provide the option for new users to register for a local account first, and then create the link between that account and the provider
UID manually.
Static |
No |
パラメータ |
パラメータ |
デフォルト |
説明 |
$data |
array()
|
Array with all data required to create the link (see example) |
|
Returns |
mixed. The insert id of the created provider link record, or false if the creation failed. |
例 |
// get the opauth object
$opauth = \Auth_Opauth::forge(false);
// do we have OAuth strategy data?
if ($authentication = \Session::get('auth-strategy.authentication', array()))
{
// get the current Auth userid
list(, $userid) = \Auth::instance()->get_user_id();
// and link it to the provider
$insert_id = $opauth->link_provider(array(
'parent_id' => $userid,
'provider' => $authentication['provider'],
'uid' => $authentication['uid'],
'access_token' => $authentication['access_token'],
'secret' => $authentication['secret'],
'refresh_token' => $authentication['refresh_token'],
'expires' => $authentication['expires'],
'created_at' => time(),
));
}
else
{
// no data available, looks like we didn't receive an OAuth callback earlier?
}
|
get($key, $default = null)
The get method is a getter that allows direct access to the raw provider callback response structure as
created by the Opauth strategy.
Static |
No |
パラメータ |
パラメータ |
デフォルト |
パラメータ |
$key |
required |
Name of the key to the desired value in the response array |
$default |
null
|
Default value to return if the requested key does not exist |
|
Returns |
mixed. The value referenced by the given key, or whatever was defined as $default. |
例 |
// get the opauth object
$opauth = \Auth_Opauth::forge(false);
// get some auth data
$provider = $opauth->get('auth.provider');
$uid = $opauth->get('auth.uid');
$token = $opauth->get('credentials.token', null);
$secret = $opauth->get('credentials.secret', null);
$expires = $opauth->get('credentials.expires', null);
$refresh_token = $opauth->get('credentials.refresh_token', null);
|
You can only use this in the method in which you are processing the callback. In all other cases, there
is no OAuth response available. If there was a valid response processed earlier, its basic data is stored in the session
using the session key "auth-strategy".