/
SteamOpenId.php
124 lines (100 loc) · 3.21 KB
/
SteamOpenId.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* Created by PhpStorm.
* User: Dziadzia
* Date: 2016-05-29
* Time: 20:41
*/
namespace SteamAuth;
use GuzzleHttp\ClientInterface;
class SteamOpenId
{
protected $config;
/**
* @var ClientInterface
*/
protected $httpClient;
protected $redirectParams = [
'openid.ns' => 'http://specs.openid.net/auth/2.0',
'openid.mode' => 'checkid_setup',
'openid.identity' => 'http://specs.openid.net/auth/2.0/identifier_select',
'openid.claimed_id' => 'http://specs.openid.net/auth/2.0/identifier_select'
];
protected $verifyWhitelist = [
'openid.ns' => 'openid_ns',
'openid.op_endpoint' => 'openid_op_endpoint',
'openid.claimed_id' => 'openid_claimed_id',
'openid.identity' => 'openid_identity',
'openid.return_to' => 'openid_return_to',
'openid.response_nonce' => 'openid_response_nonce',
'openid.assoc_handle' => 'openid_assoc_handle',
'openid.signed' => 'openid_signed',
'openid.sig' => 'openid_sig',
];
/**
* SteamOpenId constructor.
* @param array $options
* @param ClientInterface $httpClient
*/
public function __construct(array $options = [], ClientInterface $httpClient = null)
{
if (!$httpClient)
$httpClient = new \GuzzleHttp\Client([
'timeout' => 30,
'connect_timeout' => 5,
]);
$this->httpClient = $httpClient;
$this->configureDefaults();
$this->setConfigOptions($options);
}
public function getRedirectUrl()
{
$params = $this->redirectParams;
$params['openid.return_to'] = $this->config['return_to'];
$params['openid.realm'] = $this->config['realm'];
return $this->config['op_url'] . '?' . http_build_query($params);
}
public function verifyAssertion(array $getParams)
{
$params = [];
foreach ($this->verifyWhitelist as $k => $v) {
if(isset($getParams[$v]))
$params[$k] = $getParams[$v];
}
$params['openid.mode'] = 'check_authentication';
if (!isset($params['openid.return_to'])
|| $params['openid.return_to'] != $this->config['return_to']
)
return false;
$response = $this->httpClient->post($this->config['op_url'], [
'form_params' => $params
]);
if (strpos($response->getBody(), 'is_valid:true') === false)
return false;
$id = $params['openid.identity'];
return substr($id, strrpos($id, '/') + 1);
}
private function configureDefaults()
{
$defaults = [
'op_url' => 'https://steamcommunity.com/openid/login',
'return_to' => null,
'realm' => null,
];
$this->config = $defaults;
}
public function getConfig($option = null)
{
return $option === null
? $this->config
: (isset($this->config[$option]) ? $this->config[$option] : null);
}
public function setConfigOptions(array $options)
{
$this->config = array_merge($this->config, $options);
}
public function setConfig(array $config)
{
$this->config = $config;
}
}