PHP code example of gigablah / silex-oauth
1. Go to this page and download the library: Download gigablah/silex-oauth library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gigablah / silex-oauth example snippets
ini_set('display_errors', 1);
error_reporting(-1);
K_API_SECRET', '');
define('TWITTER_API_KEY', '');
define('TWITTER_API_SECRET', '');
define('GOOGLE_API_KEY', '');
define('GOOGLE_API_SECRET', '');
define('GITHUB_API_KEY', '');
define('GITHUB_API_SECRET', '');
$app = new Silex\Application();
$app['debug'] = true;
$app->register(new Gigablah\Silex\OAuth\OAuthServiceProvider(), array(
'oauth.services' => array(
'Facebook' => array(
'key' => FACEBOOK_API_KEY,
'secret' => FACEBOOK_API_SECRET,
'scope' => array('email'),
'user_endpoint' => 'https://graph.facebook.com/me'
),
'Twitter' => array(
'key' => TWITTER_API_KEY,
'secret' => TWITTER_API_SECRET,
'scope' => array(),
// Note: permission needs to be obtained from Twitter to use the ail'),
'user_endpoint' => 'https://api.github.com/user'
)
)
));
// Provides CSRF token generation
// You will have to ());
// Provides session storage
$app->register(new Silex\Provider\SessionServiceProvider(), array(
'session.storage.save_path' => '/tmp'
));
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
'security.firewalls' => array(
'default' => array(
'pattern' => '^/',
'anonymous' => true,
'oauth' => array(
//'login_path' => '/auth/{service}',
//'callback_path' => '/auth/{service}/callback',
//'check_path' => '/auth/{service}/check',
'failure_path' => '/login',
'with_csrf' => true
),
'logout' => array(
'logout_path' => '/logout',
'with_csrf' => true
),
// OAuthInMemoryUserProvider returns a StubUser and is intended only for testing.
// Replace this with your own UserProvider and User class.
'users' => new Gigablah\Silex\OAuth\Security\User\Provider\OAuthInMemoryUserProvider()
)
),
'security.access_rules' => array(
array('^/auth', 'ROLE_USER')
)
));
// Provides Twig template engine
$app->register(new Silex\Provider\TwigServiceProvider(), array(
'twig.path' => __DIR__.'/views'
));
$app->before(function (Symfony\Component\HttpFoundation\Request $request) use ($app) {
if (isset($app['security.token_storage'])) {
$token = $app['security.token_storage']->getToken();
} else {
$token = $app['security']->getToken();
}
$app['user'] = null;
if ($token && !$app['security.trust_resolver']->isAnonymous($token)) {
$app['user'] = $token->getUser();
}
});
$app->get('/login', function (Symfony\Component\HttpFoundation\Request $request) use ($app) {
$services = array_keys($app['oauth.services']);
return $app['twig']->render('index.twig', array(
'login_paths' => $app['oauth.login_paths'],
'logout_path' => $app['url_generator']->generate('logout', array(
'_csrf_token' => $app['oauth.csrf_token']('logout')
)),
'error' => $app['security.last_error']($request)
));
});
$app->match('/logout', function () {})->bind('logout');
$app->register(new Gigablah\Silex\OAuth\OAuthServiceProvider(), array(
'oauth.services' => array(
'my_service' => array(
'class' => 'My\\Custom\\Namespace\\MyOAuthService',
'key' => MY_API_KEY,
'secret' => MY_API_SECRET,
'scope' => array(),
'user_endpoint' => 'https://my.domain/userinfo',
'user_callback' => function ($token, $userInfo, $service) {
...
}
),
// ...
)
));