Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Steam API user data support #420

Merged
merged 2 commits into from Mar 19, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
137 changes: 82 additions & 55 deletions additional-providers/hybridauth-steam/Providers/Steam.php
@@ -1,55 +1,82 @@
<?php
/*!
* HybridAuth
* http://hybridauth.sourceforge.net | http://github.com/hybridauth/hybridauth
* (c) 2009-2012, HybridAuth authors | http://hybridauth.sourceforge.net/licenses.html
*/

/**
* Hybrid_Providers_Steam provider adapter based on OpenID protocol
*
* http://hybridauth.sourceforge.net/userguide/IDProvider_info_Steam.html
*/
class Hybrid_Providers_Steam extends Hybrid_Provider_Model_OpenID
{
var $openidIdentifier = "http://steamcommunity.com/openid";

/**
* finish login step
*/
function loginFinish()
{
parent::loginFinish();

$uid = str_replace( "http://steamcommunity.com/openid/id/", "", $this->user->profile->identifier );

if( $uid ){
$data = @ file_get_contents( "http://steamcommunity.com/profiles/$uid/?xml=1" );

$data = @ new SimpleXMLElement( $data );

if ( ! is_object( $data ) ){
return false;
}

$this->user->profile->displayName = (string) $data->{'steamID'};
$this->user->profile->photoURL = (string) $data->{'avatarMedium'};
$this->user->profile->description = (string) $data->{'summary'};

$realname = (string) $data->{'realname'};

if( $realname ){
$this->user->profile->firstName = $realname;
}

$customURL = (string) $data->{'customURL'};

if( $customURL ){
$this->user->profile->profileURL = "http://steamcommunity.com/id/$customURL/";
}

// restore the user profile
Hybrid_Auth::storage()->set( "hauth_session.{$this->providerId}.user", $this->user );
}
}
}
<?php
/*!
* HybridAuth
* http://hybridauth.sourceforge.net | http://github.com/hybridauth/hybridauth
* (c) 2009-2012, HybridAuth authors | http://hybridauth.sourceforge.net/licenses.html
*/

/**
* Hybrid_Providers_Steam provider adapter based on OpenID protocol
*
* http://hybridauth.sourceforge.net/userguide/IDProvider_info_Steam.html
*
* This class has been entirely reworked for the new Steam API (http://steamcommunity.com/dev)
*/
class Hybrid_Providers_Steam extends Hybrid_Provider_Model_OpenID
{
var $openidIdentifier = "http://steamcommunity.com/openid";

function loginFinish()
{
parent::loginFinish();

$this->user->profile->identifier = str_ireplace("http://steamcommunity.com/openid/id/",
"", $this->user->profile->identifier);

if (!$this->user->profile->identifier) {
throw new Exception("Authentication failed! {$this->providerId} returned an invalid user ID.", 5);
}

// If API key is not provided, use legacy API methods
if (!empty($this->config['keys']['key'])) {
$this->getUserProfileWebAPI($this->config['keys']['key']);
} else {
$this->getUserProfileLegacyAPI();
}

Hybrid_Auth::storage()->set("hauth_session.{$this->providerId}.user", $this->user);
}

function getUserProfileWebAPI($apiKey)
{
$apiUrl = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key='
. $apiKey . '&steamids=' . $this->user->profile->identifier;

$data = @file_get_contents($apiUrl);
$data = json_decode($data);

if (!is_object($data) || !isset($data->response->players[0])) {
return false;
}

// Get the first index in 'players' array
$data = $data->response->players[0];

$this->user->profile->displayName = property_exists($data, 'personaname') ? $data->personaname : '';
$this->user->profile->firstName = property_exists($data, 'realname') ? $data->realname : '';
$this->user->profile->photoURL = property_exists($data, 'avatarfull') ? $data->avatarfull : '';
$this->user->profile->profileURL = property_exists($data, 'profileurl') ? $data->profileurl : '';
$this->user->profile->country = property_exists($data, 'loccountrycode') ? $data->loccountrycode : '';
}

function getUserProfileLegacyAPI()
{
$apiUrl = 'http://steamcommunity.com/profiles/' . $this->user->profile->identifier . '/?xml=1';

$data = @file_get_contents($apiUrl);
$data = @ new SimpleXMLElement($data);

if (!is_object($data)) {
return false;
}

$this->user->profile->displayName = property_exists($data, 'steamID') ? (string)$data->steamID : '';
$this->user->profile->firstName = property_exists($data, 'realname') ? $data->realname : '';
$this->user->profile->photoURL = property_exists($data, 'avatarfull') ? $data->avatarfull : '';
$this->user->profile->description = property_exists($data, 'summary') ? (string)$data->summary : '';
$this->user->profile->region = property_exists($data, 'location') ? (string)$data->location : '';
$this->user->profile->profileURL = property_exists($data, 'customURL')
? "http://steamcommunity.com/id/{$data->customURL}/"
: "http://steamcommunity.com/profiles/{$this->user->profile->identifier}/";
}
}