Skip to content
forked from SocialConnect/auth

:atom: Social (OAuth1\OAuth2\OpenID\OpenIDConnect) sign with PHP :shipit:

License

Notifications You must be signed in to change notification settings

fossabot/auth-1

 
 

Repository files navigation

SocialConnect Auth

Packagist License Scrutinizer Code Quality Build Status Scrutinizer Code Coverage FOSSA Status

Open source social sign on PHP. Connect your application(s) with social network(s).

Code examples you can find in example directory

Supported type of providers

Supported providers

SocialConnect/Auth support 30+ providers such as Facebook, Google, Twitter, GitHub, Vk and another.

See all 30+ provider

Versions

Version Status PHP Version
3.x Development >= 7.1
2.x Current >= 7.0
1.x Maintenance >= 5.5

Installation

The recommended way to install socialconnect/auth is via Composer.

  1. If you do not have composer installed, download the composer.phar executable or use the installer.
$ curl -sS https://getcomposer.org/installer | php
  1. Run php composer.phar require socialconnect/auth or add a new requirement in your composer.json.
{
  "require": {
    "socialconnect/auth": "^2.2.0"
  }
}
  1. Run php composer.phar update

Referenced projects

How to use

Composer:

composer install 

First you need to setup SocialConnect\Auth\Service:

$httpClient = new \SocialConnect\Common\Http\Client\Curl();

/**
 * By default we are using Curl class from SocialConnect/Common
 * but you can use Guzzle wrapper ^5.3|^6.0
 */
//$httpClient = new \SocialConnect\Common\Http\Client\Guzzle(
//    new \GuzzleHttp\Client()
//);

/**
 * Why do we need cache decorator for HTTP Client?
 * Providers like OpenID & OpenIDConnect require US
 * to request OpenID specification (and JWK(s) for OpenIDConnect)
 *
 * It's not a best practice to request it every time, because it's unneeded round trip to the server
 * if you are using OpenID or OpenIDConnect we suggest you to use cache
 *
 * If you don`t use providers like (Steam) from OpenID or OpenIDConnect
 * you may skip this because it's not needed
 */
$httpClient = new \SocialConnect\Common\Http\Client\Cache(
    $httpClient,
    /**
     * Please dont use FilesystemCache for production/stage env, only for local testing!
     * It doesnot support cache expire (remove)
     */
    new \Doctrine\Common\Cache\FilesystemCache(
        __DIR__ . '/cache'
    )
);

$configureProviders = [
    'redirectUri' => 'http://sconnect.local/auth/cb/${provider}/',
    'provider' => [
        'facebook' => [
            'applicationId' => '',
            'applicationSecret' => '',
            'scope' => ['email'],
            'options' => [
                'identity.fields' => [
                    'email',
                    'picture.width(99999)'
                ],
            ],
        ],
    ],
];

$service = new \SocialConnect\Auth\Service(
    $httpClient,
    new \SocialConnect\Provider\Session\Session(),
    $configureProviders
);

/**
 * By default collection factory is null, in this case Auth\Service will create 
 * a new instance of \SocialConnect\Auth\CollectionFactory
 * you can use custom or register another providers by CollectionFactory instance
 */
$collectionFactory = null;

$service = new \SocialConnect\Auth\Service(
    $httpClient,
    new \SocialConnect\Provider\Session\Session(),
    $configureProviders,
    $collectionFactory
);

Next create you loginAction:

$providerName = 'facebook';

$provider = $service->getProvider($providerName);
header('Location: ' . $provider->makeAuthUrl());

And implement callback handler:

$providerName = 'facebook';

$provider = $service->getProvider($providerName);
$accessToken = $provider->getAccessTokenByRequestParameters($_GET);
var_dump($accessToken);

$user = $provider->getIdentity($accessToken);
var_dump($user);

Contributors

This project exists thanks to all the people who contribute. Contributions are welcome!

License

This project is open-sourced software licensed under the MIT License.

See the LICENSE file for more information.

FOSSA Status

About

:atom: Social (OAuth1\OAuth2\OpenID\OpenIDConnect) sign with PHP :shipit:

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%