4
4
5
5
use RuntimeException ;
6
6
use Illuminate \Http \Request ;
7
+ use GuzzleHttp \RequestOptions ;
7
8
use Illuminate \Support \Fluent ;
8
9
use GuzzleHttp \Client as GuzzleClient ;
9
10
use Illuminate \Support \Facades \Config ;
11
+ use GuzzleHttp \Exception \GuzzleException ;
10
12
11
13
class SteamAuth implements SteamAuthInterface
12
14
{
@@ -35,6 +37,11 @@ class SteamAuth implements SteamAuthInterface
35
37
*/
36
38
protected $ guzzleClient ;
37
39
40
+ /**
41
+ * @var array
42
+ */
43
+ protected $ customRequestOptions ;
44
+
38
45
/**
39
46
* @var string
40
47
*/
@@ -45,6 +52,26 @@ class SteamAuth implements SteamAuthInterface
45
52
*/
46
53
const STEAM_INFO_URL = 'http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%s ' ;
47
54
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
+
48
75
/**
49
76
* Create a new SteamAuth instance.
50
77
*
@@ -68,27 +95,31 @@ public function __construct(Request $request)
68
95
*/
69
96
private function requestIsValid ()
70
97
{
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 );
74
101
}
75
102
76
103
/**
77
104
* Checks the steam login.
78
105
*
79
106
* @return bool
107
+ * @throws GuzzleException
80
108
*/
81
109
public function validate ()
82
110
{
83
111
if (! $ this ->requestIsValid ()) {
84
112
return false ;
85
113
}
86
114
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
+ }
88
121
89
- $ response = $ this ->guzzleClient ->request ('POST ' , self ::OPENID_URL , [
90
- 'form_params ' => $ params ,
91
- ]);
122
+ $ response = $ this ->guzzleClient ->request ('POST ' , self ::OPENID_URL , $ requestOptions );
92
123
93
124
$ results = $ this ->parseResults ($ response ->getBody ()->getContents ());
94
125
@@ -106,14 +137,14 @@ public function validate()
106
137
public function getParams ()
107
138
{
108
139
$ 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 ,
113
144
'openid.mode ' => 'check_authentication ' ,
114
145
];
115
146
116
- $ signedParams = explode (', ' , $ this ->request ->get (' openid_signed ' ));
147
+ $ signedParams = explode (', ' , $ this ->request ->get (self :: OPENID_SIGNED ));
117
148
118
149
foreach ($ signedParams as $ item ) {
119
150
$ value = $ this ->request ->get ('openid_ ' .str_replace ('. ' , '_ ' , $ item ));
@@ -179,7 +210,7 @@ private function buildUrl($return = null)
179
210
}
180
211
181
212
$ params = [
182
- 'openid.ns ' => ' http://specs.openid.net/auth/2.0 ' ,
213
+ 'openid.ns ' => self :: OPENID_NS ,
183
214
'openid.mode ' => 'checkid_setup ' ,
184
215
'openid.return_to ' => $ return ,
185
216
'openid.realm ' => (Config::get ('steam-auth.https ' ) ? 'https ' : 'http ' ).':// ' .$ this ->request ->server ('HTTP_HOST ' ),
@@ -227,6 +258,7 @@ public function parseSteamID()
227
258
* Get user data from steam api.
228
259
*
229
260
* @return void
261
+ * @throws GuzzleException
230
262
*/
231
263
public function parseInfo ()
232
264
{
@@ -273,4 +305,34 @@ public function getSteamId()
273
305
{
274
306
return $ this ->steamId ;
275
307
}
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
+ }
276
338
}
0 commit comments