Welcome Guest, Not a member yet? Register   Sign In
EasyOpenID Library
#1

[eluser]kbjr[/eluser]
I have recently created an OpenID library for use in CodeIgniter, built on top of Janrain's php-openid library. Its pretty straight-forward and easy to use. Currently, the database storage option has not been tested, but file storage does work (they're only temp files). Instructions for install and usage are on my website (http://code.kbjrweb.com/project/easyopenid). If you encounter a bug, have a patch, or in some other way want to help, the source is on GitHub.

Edit: View the Demo

An Example of Use:
Code:
<?php

class Yours extends Controller {

    function Yours()
    {
        parent::Controller();
        
        // load the OpenID library
        $this->load->library('openid');
    }
    
    function index()
    {
        // get the link data
        $data['openid_links'] = $this->openid->build_auth(
            'yours/try_auth', array('openid', 'google', 'yahoo'), 'yours/load_icon');
            
        // load the sign in page
        $this->load->view('signin', $data);
    }
    
    function try_auth()
    {
        // figure out which provider we're loading
        $which = $this->uri->segment(3);
        switch ($which)
        {
            // handle standard OpenID
            case 'openid':
                if (array_key_exists('openid', $_POST))
                {
                    $result = $this->openid->try_auth($_POST['openid'], 'yours/finish_auth');
                }
                else
                {
                    $data['message'] = 'No Provider Given';
                }
            break;
            
            // handle Google Accounts
            case 'google':
                $result = $this->openid->try_auth_google('yours/finish_auth');
            break;
            
            // handle Yahoo!
            case 'yahoo':
                $result = $this->openid->try_auth_yahoo('yours/finish_auth');
            break;
            
            // handle errors
            default:
                $data['message'] = 'Invalid Provider';
            break;
        }
        
        // handle errors
        if (isset($result) && is_int($result))
        {
            $data['message'] = $this->openid->last_error();
        }
        $this->load->view('signin', $data);
    }
    
    function finish_auth()
    {
        // finish authentication
        $result = $this->openid->finish_auth();
        
        // check for errors
        if (is_int($result))
        {
            $data['error'] = $this->openid->last_error();
        }
        else
        {
            $data['user_data'] = $result;
        }
        
        // load the next page
        $this->load->view('next/page', $data);
    }
    
    // create the icon loader
    function load_icon()
    {
        $this->openid->icon_loader();
    }

}

/* End of file */
#2

[eluser]Rolly1971[/eluser]
interesting library, have not really gave it a good eye yet, but i will. I am planing a CMS (something that will be a cross between : Wordpress, Joomla, and ExpressionEngine) and OpenID is something i want to intigrate.

Once i get to the coding stage of my CMS i will let you know how it goes. I will be keeping an eye on this thread for sure Tongue

Cheers Mate, and thanks for sharing.
#3

[eluser]augustowloch[/eluser]
Hii! could you attach or post the complete example? i'm wondering how should look the signin.php view file!

CheerSSSS!!!
#4

[eluser]kbjr[/eluser]
This is the view file I used when testing the system:

edit: i just fixed an inconsistency between the two examples as they were written at different times and some variables were off.

Code:
<html>
    <head>
        <title>EasyOpenID Authentication Example</title>
        <base href="<?php echo $this->config->item('base_url'); ?>" />
    </head>
    <style type="text/css">
            * {
                font-family: helvetica, verdana, sans-serif;
                font-weight: 100;
                color: #333;
            }
            a {
                font-size: 0.8em;
                color: #668;
                text-decoration: none;
            }
            a:hover {
                color: #448;
                text-decoration: underline;
            }
            body {
                width: 50em;
                margin: 1em;
            }
            div {
                padding: .8em;
            }
            .data {
                border: 1px solid #666666;
                background: #888888;
            }
            .data pre {
                color: #fff;
                font-family: monospace;
            }
            #verify-form {
                border: 1px solid #777777;
                background: #dddddd;
                margin-top: 1em;
            }
            #verify-form a[rel="openid"] img {
                position: relative;
                top: 3px;
                left: -3px;
            }
            form {
                margin: 0.5em;
                padding: 0;
            }
    </style>
    <body>
        <h1>EasyOpenID Authentication Example</h1>
        <p>
            This example consumer uses the <a href="http://github.com/kbjr/EasyOpenID">
            EasyOpenID class</a> built on top of the <a href="http://github.com/openid/php-openid">
            PHP OpenID</a> library. It just verifies that the URL that you enter is your identity URL.
        </p>

        &lt;?php if (isset($data) && ! empty($data)) :
            print '<div class="data"><pre>'.print_r($data, true).'</pre></div>';
        endif; ?&gt;

        <div id="verify-form">
            &lt;?php
                foreach ($openid_links as $link) :
                    echo $link->anchor."<br />\n";
                    if ($link->provider == 'openid' && isset($openid) && $openid) : ?&gt;
                        &lt;form action="yours/try_auth/openid" method="post"&gt;
                            &lt;input type="text" name="openid" value="" /&gt;
                            &lt;input type="submit" value="Sign In" /&gt;
                        &lt;/form&gt;
                    &lt;?php endif;
                endforeach;
             ?&gt;
        </div>
    &lt;/body&gt;
&lt;/html&gt;
#5

[eluser]kbjr[/eluser]
I have just finished up with an extended version of this which includes native support for UI options (eg. authentication inside a popup window) as well as having an extension class very similar in nature to the database active record class.

I will be updating the source code repo (http://github.com/kbjr/EasyOpenID) as soon as I run a few more tests, and I will post here again when that is done.
#6

[eluser]kbjr[/eluser]
Alright, the new source code is up on github (http://github.com/kbjr/EasyOpenID) an there is a basic example up on the documentation site (http://code.kbjrweb.com/project/easyopenid); The complete API is coming. For now, here is an example of the new class:
Code:
//...

function try_auth()
{
    // load the library
    $this->load->library('EasyOpenID');

    // start the request
    $this->easyopenid
        ->provider('google')
        ->return_to('Yours/finish_auth')
        ->required(array( 'email', 'name' ))
        ->make_request();
}

function finish_auth()
{
    // load the library
    $this->load->library('EasyOpenID');

    // get the returned user data
    $user_data = $this->easyopenid->fetch_result();

    // continue processing ...
}

//...
#7

[eluser]kbjr[/eluser]
A few bugs have been fixed, the error handling system is finished, and the new package files are available for download. This library is now stable enough that I'm calling this a beta release.
#8

[eluser]jkevinburton[/eluser]
Hi kjbr - I love your library - and plan on using it. I Have one problem:

When using myspace as a provider - I get the following when authorizing is complete:

Code:
A PHP Error was encountered

Severity: Notice

Message: Trying to get property of non-object

Filename: libraries/OpenID.php

Line Number: 814

google,and yahoo both work perfectly.

-----

Another problem I noticed is that I put in the required 'email','name' - BUT doesn't matter what provider I put in ALL I get is email returned.
#9

[eluser]kbjr[/eluser]
interesting... i will look into that bug and get back to you. as far as only getting certain info back, some providers only provide certain info. if they don't want to give the info (or don't have it) there isn't really anything that can be done. however, it could also be a problem in the code (google and yahoo both use AX authorization where as i think that myspace uses SREG, so there are a few differences and there may be a bug in the SREG function). a copy of the malfunctioning code might be helpful.

edit:
also, which version of the library are you using. i have recently released a few updates and it will be hard to debug if i don't know which code i need to look at.
#10

[eluser]jkevinburton[/eluser]
Hi - Thanks for the reply - here is what I noticed:

I have them click on the link
Code:
/auth/social/login/myspace

(google and yahoo both work.)

Once i've authenticated on myspace's website, it redirects the user to:

Code:
http://socialcore.loc/auth/social/finish_auth/myspace?janrain_nonce=2010-08-20T14:19:34Z6KYAEa&openid;.ns=http://specs.openid.net/auth/2.0&openid;.mode=id_res&openid;.identity=http://www.myspace.com/546592617&openid;.return_to=http://socialcore.loc/auth/social/finish_auth/myspace?janrain_nonce=2010-08-20T14:19:34Z6KYAEa&openid;.claimed_id=http://www.myspace.com/546592617&openid;.op_endpoint=http://api.myspace.com/openid&openid;.response_nonce=2010-08-20T14:19:37ZrM~}9_@#&openid;.invalidate_handle={{HMAC-SHA1}{1282313820.83859}{tZH+7A==}&openid;.assoc_handle={{HMAC-SHA512}{1282313977.5398}{5n+lGQ==}&openid;.signed=return_to,identity,claimed_id,op_endpoint,response_nonce,assoc_handle&openid;.sig=MKUhUAe5YbgrIOfv4LZwYHRa0aWy+SO5gYalRKw47hmPCEb5h6KB+VI5DK5qOZz8znxtx/8DWELxCPdPk4rPNw==

then these errors come up:
Code:
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: libraries/OpenID.php
Line Number: 814

and
Code:
A PHP Error was encountered
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: libraries/OpenID.php
Line Number: 816

the
Code:
$user_data
array is empty.

my code for the page is as follows:

Code:
public function login() {
        $provider = $this->uri->segment(4);
        
        if ($provider == "facebook") {
            
            $params = array(
                'appId'     => $this->config->item('app_id')
              , 'secret'    => $this->config->item('api_secret')
              , 'cookie'    => TRUE
            );
            
            //d($params,true);
            
            $this->load->library('facebook',$params);
            
            $session = $this->facebook->getSession();
            
            if ($session) {
                
                try {
                    $uid        = $this->facebook->getUser();
                    $this->auth->social['facebook'] = $this->facebook->api('/me');
                } catch (FacebookApiException $e) {
                    show_error($e);
                }
                
            }
            
            
            // session is valid!
            if ($this->auth->social['facebook']) {
                    
                // does account exist in the database?
                // if yes, grab CI session,
                if ($this->auth->login_by_username($this->auth->social['facebook']['id'],"facebook")) {
                    redirect('/auth/users/profile');
                } else {
                    // if no, register the fb user.
                    $this->auth->register($this->auth->social['facebook']['email'],"facebook",$this->auth->social['facebook']['name'],$oauth_token=$session['access_token'],$oauth_secret="",$password="",$this->auth->social['facebook']['id']);
                    redirect('/auth/users/profile');
                }
                
                
                            
            } else if ($this->uri->segment(5,'')=="cancel") {
                redirect('auth/users/login');
            } else {
                // do url redirect.
                $loginUrl = $this->facebook->getLoginUrl(
                    array('req_perms' => 'email','cancel_url'=>site_url('auth/social/login/facebook/cancel'))
                );
                
                header("location:$loginUrl");
            }
            
    
        
        } else {
        
            $result = $this->easyopenid
                ->provider($provider)                     // what provider you're using
                ->return_to('auth/social/finish_auth/'.$provider)    // where to redirect after authentication
                ->required('email')                       // what data do we want
                ->make_request();                         // start the authentication process
            
            // if an error occured
            if (is_int($result))
            {
                $error = $this->easyopenid->last_error();
                show_error('There was an error connecting to: '.$provider);
            }
        }
        
    }

    public function finish_auth()
    {
        $result = $this->easyopenid
            ->fetch_response()    // collect the provider response
            ->result();           // return the response for processing
        
        // if an error occured
        if (is_int($result))
        {
            $error = $this->easyopenid->last_error();
            show_error($error);
        }
        else
        {
            $user_data = $result;
            
            d($user_data,true);
            
            
            // handle success...
            $provider = $this->uri->segment(4);
            
            if (is_array($user_data) && isset($user_data['email'])) {
                if (!$this->auth->login_by_email($user_data['email'],$provider)) {
                        
                    $name = explode('@',$user_data['email']);
                    $name = $name[0];
                    
                    //register($email="",$type="eig",$name="",$oauth_token="",$oauth_secret="",$password="",$username="")
                    $this->auth->register($user_data['email'],$provider,$name,'','','',$user_data['email']);
                }
            }
            
            redirect('/auth/users/profile');
            
        }
    }




Theme © iAndrew 2016 - Forum software by © MyBB