{{error}}
{{(quickSearchResults.length>10)?'10+':(quickSearchResults.length)}} {{(quickSearchResults.length==1)?'result':'results'}}
{{result.title}} {{result.timeStamp | mysql2ymd }}
I am sorry, no such article was written yet.
Using OpenID SSO mechanism to retrieve your visitor's details
Using OpenID SSO mechanism to retrieve your visitor's details
Recently I got interested in allowing people to add feedback, but from previous experience I noticed that such feedback mechanisms will allow spammers to compromise the site contents.
And to mitigate the situation, I had until recently two solutions:
  • using Captcha mechanisms;
  • using external APIs  to validate if the comment is a spam or not.
Now I decided to implement an OpenID mechanism, allowing people to log in with their already-existing credentials from Google, Yahoo, and other providers.
For this I found on the internet the http://gitorious.org/lightopenid API which will help me authenticate the consumers with their OpenID account. So, inside the LightOpenID class I defined few blocks.
This statement is executed every time the page is loaded, and it simply will set in my session the contact name and email, according to the authentication protocol.
But getting the name and email are not trivial task, for which I defined a getContactDetails() method inside that class.
This function, getContactDetails will return an array containing the email and the name (friendly, full name, or email, whichever is available, preferably the first).
How can be seen in the fist statement, I used a function getAllProviders that will return me all the identity providers recognized by my site. The code for this function is getAllProviders.
Finally, all this functionality is used in the PHP page, as in Usage.php.
The code is functioning for the moment only on http://my.sorescu.eu.
Have fun coding!
LightOpenID.php
try {
	$openid = new LightOpenID($_SESSION['domain_name']);
	$providers=LightOpenID::getAllProviders();
	$provider=@$providers[$_ARGS['OpenIDAuthenticationProvider']];
	if(isset($_ARGS['OpenIDAuthenticationProvider']))
		if(!$_ARGS['OpenIDAuthenticationProvider']){
			unset($_SESSION['contact']);
		}
	if(!$openid->mode) {
		if($provider){
			$openid->identity = $provider['login'];
			header('Location: ' . $openid->authUrl());
			exit;
		}
	} elseif($openid->mode == 'cancel') {
		unset($_SESSION['contact']);
	} else {
		if($openid->validate()){
			//var_dump($openid);
			$contact=$openid->getContactDetails();
			$_SESSION['contact']['provider']=@$_ARGS['OpenIDAuthenticationProvider'];
			$_SESSION['contact']['name']=@$contact['name'];
			$_SESSION['contact']['email']=@$contact['email'];
		}
	}
} catch(ErrorException $e) {
	//echo $e->getMessage();
}
getContactDetails.php
public function getContactDetails() {
	$values=array();
	//var_dump($this->data);
	foreach($this->data as $key=>$type){
		$label=@explode('_type_',$key);
		if(count($label)<2)
			continue;
		$label=@implode('_value_',$label);
		$value=$this->data[$label];
		$values[$type]=$value;
		/*if($value=='http://axschema.org/contact/email'){
			if(@$this->data[$label])
				return $this->data[$label];
		}*/
	}
	//return $this->data['openid_ext1_value_contact_email'];
	$values['email']=$values['http://axschema.org/contact/email'];
	$values['name']=$values['http://axschema.org/namePerson/friendly'];
	if(!$values['name'])
		$values['name']=$values['http://axschema.org/namePerson'];
	if(!$values['name'])
		$values['name']=$values['http://axschema.org/contact/email'];
	return $values;
	return 'unknown-email';
}
getAllProviders.php
public static function getAllProviders(){
	$result=array();
	/*
	Google https://www.google.com/accounts/o8/id
	Yahoo https://me.yahoo.com
	Flickr http://www.flickr.com/username
	AOL http://openid.aol.com/username
	Blogspot https://www.blogspot.com/
	LiveJournal http://username.livejournal.com/
	Wordpress https://username.wordpress.com/
	VerisignLabs https://pip.verisignlabs.com/
	MyOpenID https://www.myopenid.com/
	MyVidoop https://myvidoop.com/
	ClaimID https://claimid.com/username
	Technorati https://technorati.com/people/technorati/username/
	*/
	$result['google']=array('icon'=>'http://www.google.com/favicon.ico', 'label'=>'Google', 
		'login'=>'https://www.google.com/accounts/o8/id', 'logout'=>'https://www.google.com/accounts/Logout');
	$result['yahoo']=array('icon'=>'http://www.yahoo.com/favicon.ico', 'label'=>'Yahoo', 
		'login'=>'https://me.yahoo.com', 'logout'=>'https://login.yahoo.com/config/login?logout=1');
	return $result;
}
Usage.php
<?if(@$_SESSION['contact']){?>
	<a class='dms-button' href='#' onclick='$(this).siblings().toggle()'>
		<%icon:loggedin%><?&@$_SESSION['contact']['email']?>
	</a>
	<a style='display:none' class='dms-button' href='?OpenIDAuthenticationProvider='><%icon:logout%>Logout</a>
<?}else{?>
	<a class='dms-button' href='#' onclick='$(this).siblings().toggle()'><%icon:login%>Login</a>
	<?foreach(LightOpenID::getAllProviders() as $providerName => $provider){?>
		<a style='display:none' class='dms-button' href='?OpenIDAuthenticationProvider=<?=$providerName?>'><img src='<?=$provider['icon']?>'/><?=$provider['label']?></a>
	<?}?>
<?}?>