2017 © Pedro PelĂĄez
 

library connect

SensioLabs Connect SDK

image

sensiolabs/connect

SensioLabs Connect SDK

  • Thursday, May 31, 2018
  • by fabpot
  • Repository
  • 19 Watchers
  • 79 Stars
  • 16,511 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 27 Forks
  • 6 Open issues
  • 24 Versions
  • 3 % Grown

The README.md

SymfonyConnect SDK

About

This is the official SDK for the SymfonyConnect API. It works for the public API or with a registered OAuth application. To register an application, please go to your SymfonyConnect Account., (*1)

Installation

To install the SDK, run the command below and you will get the latest version:, (*2)

composer require symfonycorp/connect

Usage

OAuth

To use the SDK in a Symfony application, we recommend using the built-in bundle., (*3)

Otherwise, you can take inspiration from the following part, which will show you how to include OAuth authentication within a Silex App., (*4)

Warning: We take for granted that you already have registered your app on SymfonyConnect and that you're in possession of your application_id, application_secret and scope., (*5)

  1. Configure your silex app with the data we gave us at app registration., (*6)

    // index.php
    use SymfonyCorp\Connect\Api\Api;
    use SymfonyCorp\Connect\OAuthConsumer;
    
    $app = new Silex\Application();
    $app['connect_id'] = 'application_id';
    $app->register(new Silex\Provider\UrlGeneratorServiceProvider());
    $app->register(new Silex\Provider\SessionServiceProvider());
    
    $app['connect_secret'] = 'application_secret';
    // List of scope copy-pasted from your application page on SymfonyConnect
    $app['connect_scope'] = array(
        'SCOPE_ADDITIONAL_EMAILS',
        'SCOPE_BIRTHDAY',
        'SCOPE_EMAIL',
        'SCOPE_LOCATION',
        'SCOPE_PUBLIC',
        'SCOPE_SSH_KEYS',
    );
    
    $app['connect_consumer'] = new OAuthConsumer(
        $app['connect_id'],
        $app['connect_secret'],
        implode(' ', $app['connect_scope']) // scope MUST be space separated
    );
    $app['connect_api'] = new Api();
    

    This done. We can now move on to the second step., (*7)

  2. We need to create two controllers to handle the OAuth2 Three-Legged workflow., (*8)

    The first controller goal is to redirect the user to SymfonyConnect in order to ask him for the authorization that your app will use his data. This controller will be bound to the connect_auth route. In your template, you'll need to create a link to this route., (*9)

    // index.php
    $app->get('/connect/new', function () use ($app) {
        $callback = $app['url_generator']->generate('connect_callback', array(), true);
        $url = $app['connect_consumer']->getAuthorizationUri($callback);
    
        return $app->redirect($url);
    })->bind('connect_auth');
    

    The second controller is the one that will welcome the user after SymfonyConnect redirected him to your application. When registering your client, you'll have to provide the exact absolute URL that points to this controller., (*10)

    $app->get('/connect/callback', function (Request $request) use ($app) {
        // There was an error during the workflow.
        if ($request->get('error')) {
            throw new \RuntimeException($request->get('error_description'));
        }
    
        // Everything went fine, you can now request an access token.
        try {
            $data = $app['connect_consumer']->requestAccessToken($app['url_generator']->generate('connect_callback', array(), true), $request->get('code'));
        } catch (OAuthException $e) {
            throw $e;
        }
    
        // At this point, we have an access token and we can use the SDK to request the API
        $app['connect_api']->setAccessToken($data['access_token']); // All further request will be done with this access token
        $root = $app['connect_api']->getRoot();
        $user = $root->getCurrentUser();
        $user->getBadges()->refresh();
    
        $app['session']->start();
        $app['session']->set('connect_access_token', $data['access_token']);
        $app['session']->set('connect_user', $user);
    
        return $app->redirect('/');
    })->bind('connect_callback');
    
  3. Create a link from your template, (*11)

    In a template, you can use the following snippet of code to render a SymfonyConnect button:, (*12)

    <a href="#" class="connect-with-symfony">
        <span>Log in with SymfonyConnect</span>
    </a>
    

    And include the following CSS file: https://connect.symfony.com/css/sln.css, (*13)

Et voilĂ ! Your application can now use SymfonyConnect as an authentication method!, (*14)

The API

The SymfonyConnect Connect API is RESTFul and (tries to) conforms to the HATEOAS principle., (*15)

Here are some useful recipes., (*16)

  1. Search, (*17)

    $root = $api->getRoot();
    
    // Will search for users
    $users = $root->getUsers('fab');
    
  2. Edit authenticated user, (*18)

    $app['connect_api']->setAccessToken($app['session']->get('connect_access_token'));
    $root = $app['connect_api']->getRoot();
    $user = $root->getCurrentUser();
    $user->setBiography("I'm sexy and I know it.");
    $user->submitForm();
    

As you can see by these examples, you always have to to go through the API Root to make an action. This is because the API is discoverable and that the SDK should not know anything beside the API's entry point., (*19)

License

This library is licensed under the MIT license., (*20)

The Versions

31/05 2018
04/04 2018
30/12 2015

v4.2.1

4.2.1.0 https://github.com/sensiolabs/connect

SensioLabs Connect SDK

  Sources   Download

MIT

The Requires

 

The Development Requires

sdk sensiolabs connect

12/12 2014

v4.2.0

4.2.0.0 https://github.com/sensiolabs/connect

SensioLabs Connect SDK

  Sources   Download

MIT

The Requires

 

The Development Requires

sdk sensiolabs connect

21/10 2014

v4.1.0

4.1.0.0 https://github.com/sensiolabs/connect

SensioLabs Connect SDK

  Sources   Download

MIT

The Requires

 

The Development Requires

sdk sensiolabs connect

12/09 2014

v4.0.0

4.0.0.0 https://github.com/sensiolabs/connect

SensioLabs Connect SDK

  Sources   Download

MIT

The Requires

 

The Development Requires

sdk sensiolabs connect

18/07 2014

v3.0.0

3.0.0.0 https://github.com/sensiolabs/connect

SensioLabs Connect SDK

  Sources   Download

MIT

The Requires

 

The Development Requires

sdk sensiolabs connect

02/07 2014

v2.1.0

2.1.0.0 https://github.com/sensiolabs/connect

SensioLabs Connect SDK

  Sources   Download

MIT

The Requires

 

The Development Requires

sdk sensiolabs connect

15/05 2014

v2.0.2

2.0.2.0 https://github.com/sensiolabs/connect

SensioLabs Connect SDK

  Sources   Download

MIT

The Requires

 

The Development Requires

sdk sensiolabs connect

12/05 2014

v2.0.1

2.0.1.0 https://github.com/sensiolabs/connect

SensioLabs Connect SDK

  Sources   Download

MIT

The Requires

 

The Development Requires

sdk sensiolabs connect

23/12 2013

v2.0.0

2.0.0.0 https://github.com/sensiolabs/connect

SensioLabs Connect SDK

  Sources   Download

MIT

The Requires

 

The Development Requires

sdk sensiolabs connect

10/12 2013

v1.9

1.9.0.0 https://github.com/sensiolabs/connect

SensioLabs Connect SDK

  Sources   Download

MIT

The Requires

 

The Development Requires

sdk sensiolabs connect

10/12 2013

v1.8

1.8.0.0 https://github.com/sensiolabs/connect

SensioLabs Connect SDK

  Sources   Download

MIT

The Requires

 

The Development Requires

sdk sensiolabs connect

10/12 2013

v1.7

1.7.0.0 https://github.com/sensiolabs/connect

SensioLabs Connect SDK

  Sources   Download

MIT

The Requires

 

The Development Requires

sdk sensiolabs connect

23/10 2013

v1.6

1.6.0.0 https://github.com/sensiolabs/connect

SensioLabs Connect SDK

  Sources   Download

MIT

The Requires

 

The Development Requires

sdk sensiolabs connect

02/09 2013

v1.5

1.5.0.0 https://github.com/sensiolabs/connect

SensioLabs Connect SDK

  Sources   Download

MIT

The Requires

 

The Development Requires

sdk sensiolabs connect

03/08 2013

v1.4

1.4.0.0 https://github.com/sensiolabs/connect

SensioLabs Connect SDK

  Sources   Download

MIT

The Requires

 

The Development Requires

sdk sensiolabs connect

05/07 2013

v1.3

1.3.0.0 https://github.com/sensiolabs/connect

SensioLabs Connect SDK

  Sources   Download

MIT

The Requires

 

The Development Requires

sdk sensiolabs connect

04/07 2013

v1.2

1.2.0.0 https://github.com/sensiolabs/connect

SensioLabs Connect SDK

  Sources   Download

MIT

The Requires

 

The Development Requires

sdk sensiolabs connect

25/06 2013

v1.1

1.1.0.0 https://github.com/sensiolabs/connect

SensioLabs Connect SDK

  Sources   Download

MIT

The Requires

 

The Development Requires

sdk sensiolabs connect

15/02 2013

v1.0

1.0.0.0 https://github.com/sensiolabs/connect

SensioLabs Connect SDK

  Sources   Download

MIT

The Requires

 

The Development Requires

sdk sensiolabs connect