Developed with love by KnpLabs Hire us for your project!
30

GoogleSiteAuthenticatorBundle

by Happyr

Authenticate you website (not your users) with Google API

Happyr Google Site Authenticator Bundle

Latest Version
Software License
Build Status
Code Coverage
Quality Score
Total Downloads

In some situations you want a website to make API request on the behalf of you (not your users). Example when
you want to fetch website data from Google Analytics or upload database dumps to Google Drive. The solution Google
offers for this is a Domain-Wide Delegation of Authority. But
that solution requires you be a paying customer on Google Apps. I wanted a free solution so I created this bundle.

This bundle is using a normal OAuth for a web application, but it authenticates your google account (or accounts) but
not your users'. It saves the access token in a cache until you manually revoke it.

Read all of this README file to understand how to get started and authenticated

Install

Use composer to get the bundle. You do also have to get a PSR-6 cache implementation.

$ php composer.phar require happyr/google-site-authenticator-bundle cache/redis-adapter

Activate this bundle in AppKernel.

new Happyr\GoogleSiteAuthenticatorBundle\HappyrGoogleSiteAuthenticatorBundle(),

Include the routing.yml and make sure it is protected from normal users.

// app/config/routing.yml
happyr_google_site_authenticator:
    resource: "@HappyrGoogleSiteAuthenticatorBundle/Resources/config/routing.yml"
    prefix:   /admin

Get API credentials

You will find all information on the Google Console. Go in to the console and
click on "APIs" in the sidebar to select those API you want to use.

To retrieve the API-key and secret, click on "Credentials" in the sidebar and then "Create new ClientID". And create a
client ID for a web application. Make sure to specify the correct Authorized redirect URIs. If you used the
configuration above you should use the following url:

http://www.domain.com/admin/authenticate-google/return-url

When you are done you will get a client id and a client secret. Save those for the next section.

Configure

This bundle will fetch an access token and save it to cache. The PHPCacheAdapterBundle
is an excellent bundle for this. You may use one of many predefined cache providers like; file_system, apc, mongodb etc.
Read more about caching here: http://www.php-cache.com/

It also allows you to create your own cache provider. Here is an example configuration:

cache_adapter:
  providers:
    my_redis:
      factory: 'cache.factory.redis'

To configure the Happyr Google Site Authenticator bundle you need to add your API credentials and select a service
extending Psr\Cache\CacheItemInterface. If you are using the configuration
above you could use the following values:

happyr_google_site_authenticator:
  cache_service: 'cache.provider.my_redis'
  tokens:
    google_drive:
      client_id: '00000000000-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com'
      client_secret: 'xxxxx-xxxxx_xxxxxxxxxxxx'
      redirect_url: 'http://www.domain.com/admin/authenticate-google/return-url'
      scopes: ['https://www.googleapis.com/auth/drive']

You will find all available scopes here.

The config above will configure a token called google_drive. You may, of course, configure more tokens. To get a
Google_Client instance with those credentials:

$clientProvider = $this->get('happyr.google.client_provider');
$client = $clientProvider->getClient('google_drive');

// or don't use the client provider
$client = $this->get('google.client.google_drive');

// if you only have one token configured
$client = $this->get('google.client');

Authenticating

To make sure you fetch an access token you need to navigate to http://www.domain.com/admin/authenticate-google and
click on Authenticate. You will be asked to sign in with your Google account and grant the permissions. The access token
retrieved will be saved by the cache service. You want to make sure this is stored for a very long time.

When you are authenticated you may use happyr.google.client_provider to get an authenticated client.

The MIT License (MIT)

Copyright (c) 2014 Happyr

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

happyr_google_site_authenticator:
cache_service: ~
tokens: # Required

# Prototype
name:
client_id: ~ # Required
client_secret: ~ # Required
redirect_url: ~ # Required
scopes: ~
  • minor
    By Nyholm, 10 months ago
  • Fixed typo (#16)
    By web-flow, 10 months ago
  • better load templates
    By Nyholm, 1 year ago
  • rename
    By Nyholm, 1 year ago
  • Rename
    By Nyholm, 1 year ago
  • Do not use versions higher than 1.1.7
    By web-flow, 3 years ago
  • Updated link
    By web-flow, 3 years ago
  • Cache key (#14)
    By web-flow, 3 years ago
  • Travis fixes (#13)
    By web-flow, 3 years ago
  • Dropped support for php 5.4
    By web-flow, 3 years ago
  • Removed build.xml (#10)
    By web-flow, 3 years ago
  • Merge pull request #8 from Happyr/meta
    By web-flow, 3 years ago
  • Improved testing, badges and added metadata
    By Nyholm, 3 years ago
  • style fix
    By Nyholm, 3 years ago
  • Added tests
    By Nyholm, 3 years ago
  • Applied fixes from StyleCI (#6)
    By web-flow, 3 years ago
  • Updated travis config (#5)
    By web-flow, 3 years ago
  • Start using PSR-6 cache
    By Nyholm, 3 years ago
  • Merge pull request #4 from segli/segli-patch-1
    By Nyholm, 3 years ago
  • Update services.yml
    By , 3 years ago
  • bugfix
    By Nyholm, 4 years ago
  • Merge pull request #3 from Happyr/remove
    By Nyholm, 4 years ago
  • Merge branch 'master' into remove
    By Nyholm, 4 years ago
  • Code style
    By Nyholm, 4 years ago
  • Made a difference between revoke and remove
    By Nyholm, 4 years ago
  • Adapt to the sf3.0 api
    By Nyholm, 4 years ago
  • You dont need google/apiclient @dev any more
    By Nyholm, 4 years ago
  • Merge pull request #2 from Nyholm/cleanup
    By Nyholm, 4 years ago
  • Remove clover reports
    By Nyholm, 4 years ago
  • Make sure to do a strict comparison
    By Nyholm, 4 years ago