diff --git a/CHANGELOG.md b/CHANGELOG.md index aa376c5..c3dcfb5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,8 @@ Yii2 EAuth Change Log ===================== ### dev (13.01.2016) -* Move response parsing from oauth to base service (fix #71) +* Move response parsing from oauth to base service (#71) +* Get user profile from Steam API (#70) ### 2.4.0 (03.01.2016) * Fixed error param names for Facebook (#63) diff --git a/README.md b/README.md index 704bb5c..e1255d3 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,7 @@ Add the following in your config: 'steam' => [ 'class' => 'nodge\eauth\services\SteamOpenIDService', //'realm' => '*.example.org', // your domain, can be with wildcard to authenticate on subdomains. + 'apiKey' => '...', // Optional. You can get it here: https://steamcommunity.com/dev/apikey ], 'instagram' => [ // register your app here: https://instagram.com/developer/register/ diff --git a/src/services/SteamOpenIDService.php b/src/services/SteamOpenIDService.php index 1dc76e3..71c7f4e 100644 --- a/src/services/SteamOpenIDService.php +++ b/src/services/SteamOpenIDService.php @@ -25,16 +25,34 @@ class SteamOpenIDService extends Service protected $jsArguments = ['popup' => ['width' => 990, 'height' => 615]]; protected $url = 'http://steamcommunity.com/openid/'; + protected $apiUrl = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/'; + + public $apiKey; protected function fetchAttributes() { - if (isset($this->attributes['id'])) { - $urlChunks = explode('/', $this->attributes['id']); - if ($count = count($urlChunks)) { - $name = $urlChunks[$count - 1]; - $this->attributes['name'] = $name; - } - } + $chunks = explode('/', $this->attributes['id']); + $id = array_pop($chunks); + + $this->attributes['name'] = $id; + + if ($this->apiKey) { + $response = $this->makeRequest($this->apiUrl, [ + 'query' => [ + 'steamids' => $id, + 'key' => $this->apiKey, + 'format' => 'json' + ] + ]); + + if (isset($response['response']['players'][0])) { + $profile = $response['response']['players'][0]; + + $this->attributes = array_merge($this->attributes, $profile); + $this->attributes['name'] = $profile['personaname']; + $this->attributes['url'] = $profile['profileurl']; + } + } } } \ No newline at end of file