Skip to content

Commit

Permalink
Merge pull request #104 from timetorock/development
Browse files Browse the repository at this point in the history
Custom request options for Guzzle.
  • Loading branch information
invisnik authored Feb 6, 2019
2 parents b8fbc7a + 4a615ff commit 431b081
Showing 1 changed file with 75 additions and 13 deletions.
88 changes: 75 additions & 13 deletions src/SteamAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

use RuntimeException;
use Illuminate\Http\Request;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Fluent;
use GuzzleHttp\Client as GuzzleClient;
use Illuminate\Support\Facades\Config;
use GuzzleHttp\Exception\GuzzleException;

class SteamAuth implements SteamAuthInterface
{
Expand Down Expand Up @@ -35,6 +37,11 @@ class SteamAuth implements SteamAuthInterface
*/
protected $guzzleClient;

/**
* @var array
*/
protected $customRequestOptions;

/**
* @var string
*/
Expand All @@ -45,6 +52,26 @@ class SteamAuth implements SteamAuthInterface
*/
const STEAM_INFO_URL = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%s';

/**
* @var string
*/
const OPENID_SIG = 'openid_sig';

/**
* @var string
*/
const OPENID_SIGNED = 'openid_signed';

/**
* @var string
*/
const OPENID_ASSOC_HANDLE = 'openid_assoc_handle';

/**
* @var string
*/
const OPENID_NS = 'http://specs.openid.net/auth/2.0';

/**
* Create a new SteamAuth instance.
*
Expand All @@ -68,27 +95,31 @@ public function __construct(Request $request)
*/
private function requestIsValid()
{
return $this->request->has('openid_assoc_handle')
&& $this->request->has('openid_signed')
&& $this->request->has('openid_sig');
return $this->request->has(self::OPENID_ASSOC_HANDLE)
&& $this->request->has(self::OPENID_SIGNED)
&& $this->request->has(self::OPENID_SIG);
}

/**
* Checks the steam login.
*
* @return bool
* @throws GuzzleException
*/
public function validate()
{
if (! $this->requestIsValid()) {
return false;
}

$params = $this->getParams();
$requestOptions = $this->getDefaultRequestOptions();
$customOptions = $this->getCustomRequestOptions();

if (! empty($customOptions) && is_array($customOptions)) {
$requestOptions = array_merge($requestOptions, $customOptions);
}

$response = $this->guzzleClient->request('POST', self::OPENID_URL, [
'form_params' => $params,
]);
$response = $this->guzzleClient->request('POST', self::OPENID_URL, $requestOptions);

$results = $this->parseResults($response->getBody()->getContents());

Expand All @@ -106,14 +137,14 @@ public function validate()
public function getParams()
{
$params = [
'openid.assoc_handle' => $this->request->get('openid_assoc_handle'),
'openid.signed' => $this->request->get('openid_signed'),
'openid.sig' => $this->request->get('openid_sig'),
'openid.ns' => 'http://specs.openid.net/auth/2.0',
'openid.assoc_handle' => $this->request->get(self::OPENID_ASSOC_HANDLE),
'openid.signed' => $this->request->get(self::OPENID_SIGNED),
'openid.sig' => $this->request->get(self::OPENID_SIG),
'openid.ns' => self::OPENID_NS,
'openid.mode' => 'check_authentication',
];

$signedParams = explode(',', $this->request->get('openid_signed'));
$signedParams = explode(',', $this->request->get(self::OPENID_SIGNED));

foreach ($signedParams as $item) {
$value = $this->request->get('openid_'.str_replace('.', '_', $item));
Expand Down Expand Up @@ -179,7 +210,7 @@ private function buildUrl($return = null)
}

$params = [
'openid.ns' => 'http://specs.openid.net/auth/2.0',
'openid.ns' => self::OPENID_NS,
'openid.mode' => 'checkid_setup',
'openid.return_to' => $return,
'openid.realm' => (Config::get('steam-auth.https') ? 'https' : 'http').'://'.$this->request->server('HTTP_HOST'),
Expand Down Expand Up @@ -227,6 +258,7 @@ public function parseSteamID()
* Get user data from steam api.
*
* @return void
* @throws GuzzleException
*/
public function parseInfo()
{
Expand Down Expand Up @@ -273,4 +305,34 @@ public function getSteamId()
{
return $this->steamId;
}

/**
* @return array
*/
public function getDefaultRequestOptions()
{
return [
RequestOptions::FORM_PARAMS => $this->getParams(),
];
}

/**
* If you need to set additional guzzle options on request,
* set them via this method.
* @param $options
*
* @return void
*/
public function setCustomRequestOptions($options)
{
$this->customRequestOptions = $options;
}

/**
* @return array
*/
public function getCustomRequestOptions()
{
return $this->customRequestOptions;
}
}

0 comments on commit 431b081

Please sign in to comment.