Skip to content

Commit 431b081

Browse files
authored
Merge pull request #104 from timetorock/development
Custom request options for Guzzle.
2 parents b8fbc7a + 4a615ff commit 431b081

File tree

1 file changed

+75
-13
lines changed

1 file changed

+75
-13
lines changed

src/SteamAuth.php

Lines changed: 75 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
use RuntimeException;
66
use Illuminate\Http\Request;
7+
use GuzzleHttp\RequestOptions;
78
use Illuminate\Support\Fluent;
89
use GuzzleHttp\Client as GuzzleClient;
910
use Illuminate\Support\Facades\Config;
11+
use GuzzleHttp\Exception\GuzzleException;
1012

1113
class SteamAuth implements SteamAuthInterface
1214
{
@@ -35,6 +37,11 @@ class SteamAuth implements SteamAuthInterface
3537
*/
3638
protected $guzzleClient;
3739

40+
/**
41+
* @var array
42+
*/
43+
protected $customRequestOptions;
44+
3845
/**
3946
* @var string
4047
*/
@@ -45,6 +52,26 @@ class SteamAuth implements SteamAuthInterface
4552
*/
4653
const STEAM_INFO_URL = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%s';
4754

55+
/**
56+
* @var string
57+
*/
58+
const OPENID_SIG = 'openid_sig';
59+
60+
/**
61+
* @var string
62+
*/
63+
const OPENID_SIGNED = 'openid_signed';
64+
65+
/**
66+
* @var string
67+
*/
68+
const OPENID_ASSOC_HANDLE = 'openid_assoc_handle';
69+
70+
/**
71+
* @var string
72+
*/
73+
const OPENID_NS = 'http://specs.openid.net/auth/2.0';
74+
4875
/**
4976
* Create a new SteamAuth instance.
5077
*
@@ -68,27 +95,31 @@ public function __construct(Request $request)
6895
*/
6996
private function requestIsValid()
7097
{
71-
return $this->request->has('openid_assoc_handle')
72-
&& $this->request->has('openid_signed')
73-
&& $this->request->has('openid_sig');
98+
return $this->request->has(self::OPENID_ASSOC_HANDLE)
99+
&& $this->request->has(self::OPENID_SIGNED)
100+
&& $this->request->has(self::OPENID_SIG);
74101
}
75102

76103
/**
77104
* Checks the steam login.
78105
*
79106
* @return bool
107+
* @throws GuzzleException
80108
*/
81109
public function validate()
82110
{
83111
if (! $this->requestIsValid()) {
84112
return false;
85113
}
86114

87-
$params = $this->getParams();
115+
$requestOptions = $this->getDefaultRequestOptions();
116+
$customOptions = $this->getCustomRequestOptions();
117+
118+
if (! empty($customOptions) && is_array($customOptions)) {
119+
$requestOptions = array_merge($requestOptions, $customOptions);
120+
}
88121

89-
$response = $this->guzzleClient->request('POST', self::OPENID_URL, [
90-
'form_params' => $params,
91-
]);
122+
$response = $this->guzzleClient->request('POST', self::OPENID_URL, $requestOptions);
92123

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

@@ -106,14 +137,14 @@ public function validate()
106137
public function getParams()
107138
{
108139
$params = [
109-
'openid.assoc_handle' => $this->request->get('openid_assoc_handle'),
110-
'openid.signed' => $this->request->get('openid_signed'),
111-
'openid.sig' => $this->request->get('openid_sig'),
112-
'openid.ns' => 'http://specs.openid.net/auth/2.0',
140+
'openid.assoc_handle' => $this->request->get(self::OPENID_ASSOC_HANDLE),
141+
'openid.signed' => $this->request->get(self::OPENID_SIGNED),
142+
'openid.sig' => $this->request->get(self::OPENID_SIG),
143+
'openid.ns' => self::OPENID_NS,
113144
'openid.mode' => 'check_authentication',
114145
];
115146

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

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

181212
$params = [
182-
'openid.ns' => 'http://specs.openid.net/auth/2.0',
213+
'openid.ns' => self::OPENID_NS,
183214
'openid.mode' => 'checkid_setup',
184215
'openid.return_to' => $return,
185216
'openid.realm' => (Config::get('steam-auth.https') ? 'https' : 'http').'://'.$this->request->server('HTTP_HOST'),
@@ -227,6 +258,7 @@ public function parseSteamID()
227258
* Get user data from steam api.
228259
*
229260
* @return void
261+
* @throws GuzzleException
230262
*/
231263
public function parseInfo()
232264
{
@@ -273,4 +305,34 @@ public function getSteamId()
273305
{
274306
return $this->steamId;
275307
}
308+
309+
/**
310+
* @return array
311+
*/
312+
public function getDefaultRequestOptions()
313+
{
314+
return [
315+
RequestOptions::FORM_PARAMS => $this->getParams(),
316+
];
317+
}
318+
319+
/**
320+
* If you need to set additional guzzle options on request,
321+
* set them via this method.
322+
* @param $options
323+
*
324+
* @return void
325+
*/
326+
public function setCustomRequestOptions($options)
327+
{
328+
$this->customRequestOptions = $options;
329+
}
330+
331+
/**
332+
* @return array
333+
*/
334+
public function getCustomRequestOptions()
335+
{
336+
return $this->customRequestOptions;
337+
}
276338
}

0 commit comments

Comments
 (0)