Ceeram

Undercover as programmer

Opauth 1.0-alpha arriving soon

Lately i have been hacking on Opauth as i blogged about earlier already. Most things are now in place for what will be released as Opauth version 1.0. Currently its still a work in progress, but if you like, you can already play with it and provide some feedback before we freeze the api.

Changes

Here a quick overview of things that have changed since 0.x versions:

Opauth no longer makes an additional request to your application

In the past, Opauth used callback_transport config and shipToCallback() method to pass the authentication data to your application. This has all been removed as you will now receive a Response object directly from Opauth->run() This also requires less to none framework specific code around Opauth.

Http requests are now handled by individual transport classes

We still support non-curl users with the included File transport class, which uses the file_get_contents() method. Also there is a built in Curl transport class which is the default transport. Additionally you can add your own transport classes to make the requests with tools like Guzzle.

No more PHP 5.2 support

We have dropped support for this very outdated PHP version. The minimum required version is now PHP 5.3. The code has been refactored into several classes and namespaces have been added.

Map response data

Response mapping was add, so you can add your own mapping in each strategy config array to map raw data to the response object in your preferred structure

What’s left?

In order to push to alpha state, we need to update all documentation and help 3rd party strategy maintainers to upgrade to 1.0 version.

We have moved the github repo away from uzyn’s github account into the opauth organisation. Feel free to checkout the wip/1.0 branch of Opauth and the strategies. Feedback or help out updating the docs for Opauth and all the strategies will be highly appreciated.

Example

Here is a simple example on how to use Opauth 1.0. Create a directory with the following composer.json file and then run composer install

{
    "require": {
        "opauth/opauth": "dev-wip/1.0",
        "opauth/twitter": "dev-wip/1.0"
    }
}

Next create index.php

<?php
require 'vendor/autoload.php';
$config = array(
        'Strategy' => array(
                'Twitter' => array(
                        'key' => 'your_key',
                        'secret' => 'your_secret'
                ),
        ),
        'path' => '/opauth/'
);
$Opauth = new Opauth\Opauth($config);
$response = $Opauth->run();
echo "Authed as " . $response->name . " with uid" . $response->uid;

Fill in the key and secret in index.php then add this .htaccess

<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

Set DocumentRoot of your web server to this directory, or create a vhost as this example does not work when opauth is in a subdirectory

Now point the browser to http://localhost/opauth/twitter to see it in action.

Example in CakePHP application

Here is an example on using Opauth 1.0 in a CakePHP application. Create a fresh application (or use an existing application), add the following composer.json file and then run composer install

{
    "require": {
        "opauth/opauth": "dev-wip/1.0",
        "opauth/twitter": "dev-wip/1.0"
    },
    "config":{
        "vendor-dir":"Vendor"
    }
}

In your app/Config/bootstrap.php add the following lines

App::import('Vendor', array('file' => 'autoload'));
Configure::write('Opauth', array(
    'Strategy' => array(
        'Twitter' => array(
            'key' => 'your_key',
            'secret' => 'your_secret'
            ),
        ),
    'path' => '/opauth/'
));

In your app/Config/routes.php add the following line

Router::connect('/opauth/*', array('controller' => 'users', 'action' => 'opauth'));

In your UsersController.php add the following action

public function opauth() {
    $config = Configure::read('Opauth');
    $Opauth = new Opauth\Opauth($config);
    $response = $Opauth->run();
    //use the Response object as you like
    //debug($response); to see what it looks like
}

Now point the browser to http://pathtoyour/application/opauth/twitter to see it in action.

Alternatively you could use an Authenticate adapter

create app/Controller/Component/Auth/OpauthAuthenticate.php

<?php

class OpauthAuthenticate extends BaseAuthenticate {

    public function authenticate(CakeRequest $request, CakeResponse $response) {
        $config = Configure::read('Opauth');
        try {
            $Opauth = new Opauth\Opauth($config);
            $result = $Opauth->run();
            return $result->info;
        } catch (Exception $e) {}
        return false;
    }

}

and your UsersController opauth action would then look like:

public function opauth() {
    $this->Auth->authenticate = 'Opauth';
    if ($this->Auth->login()) {
        return $this->redirect($this->Auth->loginRedirect);
    }
    $this->Session->setFlash('Social login failed');
    $this->redirect('/');
}

Adding CakePHP as composer package

Here is a quick note on how to add CakePHP 2.x as a dependency for your cake project using composer. The root composer.json file would look something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{
    "name": "vendorname/appname",
    "description": "Addding cake as composer package",
    "repositories": [
        {
            "type": "package",
            "package": {
                "name": "cakephp/cakephp",
                "version": "2.3.6",
                "dist": {
                    "url": "https://github.com/cakephp/cakephp/archive/2.3.6.zip",
                    "type": "zip"
                },
                "source": {
                    "url": "https://github.com/cakephp/cakephp/",
                    "type": "git",
                    "reference": "tree/2.3.6/"
                }
            }
        }
    ],
    "require":{
        "cakephp/cakephp": "~2.3"
    },
    "config":{
        "vendor-dir":"Vendor"
    }
}

As you can see, we don’t specify autoload or include-dirs, we let CakePHP’s own autoloader still handle that part.

Next you need to update your app/webroot/index.php (and test.php)

1
define('CAKE_CORE_INCLUDE_PATH', ROOT . DS . APP_DIR . DS . 'Vendor' . DS . 'cakephp' . DS . 'cakephp' . DS . 'lib');

And also update your app/Console/cake.php

1
2
$root = dirname(dirname(__FILE__)) . $ds . 'Vendor' . $ds . 'cakephp' . $ds . 'cakephp';
ini_set('include_path', $root . $ds . 'lib' . PATH_SEPARATOR . ini_get('include_path'));

Using composer with CakePHP 2.x

Lately i have been hacking on Opauth and finally was forced to learn more about composer. Within the CakePHP community composer has not been adopted really, altough composer-installer supports an easy way for installing cakephp-plugin already. Many plugin maintainers have not added composer.json files to their repositories, as CakePHP itself is not using it as well. There is no need to stop you from using composer with your CakePHP application. You can install both plugins and 3rd party libraries easily. Currently you would clone/download required libraries to the vendors/ or app/Vendor/ directory.

Rake new_post as draft

The first thing after setting up Octopress was looking for ways to create new posts as draft by default. As always Google search was very helpfull and found me this pull request After applying this commit on my octopress install new posts will now be created with published: false

Ceeram finally starts blogging

So i finally have set up a blog to publish things that don’t fit in 140 characters. Mostly i will use the blog for personal reference, but you might find things you like.