Skip to content

Commit

Permalink
Cookie File update
Browse files Browse the repository at this point in the history
  • Loading branch information
Allyans3 committed Dec 4, 2022
1 parent 5ad87e9 commit 8ed3ef3
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,48 @@ $auth->getCookiesByHost();

```

## Cookie storage

By default, cookie saves in `$cookieStorage` attribute and can be retrieved by `getCookies()` or `getCookiesByHost()` methods.

Optional, you can specify the path to your cookie file.

Also, you can send your cookies as array to `cookie_storage``key.

```php
use SteamAuth\SteamAuth;

require "vendor/autoload.php";

$cookieOptions = [
'cookie_file' => 'path_to_cookie_file',

'cookie_storage' => [
"steamcommunity.com" => [
"sessionid" => "*******"
"steamCountry" => "*******"
"steamLoginSecure" => "*******"
],
"store.steampowered.com" => [
"sessionid" => "*******"
"steamLoginSecure" => "*******"
],
"help.steampowered.com" => [
"sessionid" => "*******"
"steamLoginSecure" => "*******"
],
"steam.tv" => [
"sessionid" => "*******"
"steamCountry" => "*******"
"steamLoginSecure" => "*******"
]
]
];

$auth = new SteamAuth('login', 'password', 'shared_secret', $cookieOptions);
```


## Handle Exceptions

```php
Expand Down
25 changes: 21 additions & 4 deletions src/SteamAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class SteamAuth
private $password;
private $sharedSecret;

private $cookieStorage;
private $cookieStorage = [];
private $cookieFile = '';

const HEADERS = [
'Origin' => "https://steamcommunity.com",
Expand All @@ -46,13 +47,19 @@ class SteamAuth
];


public function __construct($login, $password, $sharedSecret = null, $cookieStorage = [])
public function __construct($login, $password, $sharedSecret = null, array $cookieOptions = [])
{
$this->login = $login;
$this->password = $password;
$this->sharedSecret = $sharedSecret;

$this->cookieStorage = $cookieStorage;
self::setCookieOptions($cookieOptions);
}

private function setCookieOptions(array $options)
{
$this->cookieStorage = isset($options['cookie_storage']) ? $options['cookie_storage'] : $this->cookieStorage;
$this->cookieFile = isset($options['cookie_file']) ? $options['cookie_file'] : $this->cookieFile;
}

/**
Expand All @@ -64,7 +71,7 @@ public function login(): bool
{
if (self::isAuthorized())
return true;
else if (!array_key_exists('sessionid', $this->cookieStorage))
else if (!array_key_exists('sessionid', self::getCookiesByHost()))
self::getStartupCookies();

$keys = self::getRSAKey();
Expand Down Expand Up @@ -274,6 +281,8 @@ public function getStartupCookies()
$curl->setConnectTimeout(30);
$curl->setTimeout(60);

$curl->setCookieJar($this->cookieFile);

$curl->get('https://steamcommunity.com/');

if ($curl->error)
Expand Down Expand Up @@ -325,7 +334,10 @@ private function setToken(string $url, string $nonce, $auth, $steamId)
$curl->setDefaultJsonDecoder($assoc = true);
$curl->setConnectTimeout(30);
$curl->setTimeout(60);

$curl->setCookies(self::getCookiesByHost(self::getHostFromUrl($url)));
$curl->setCookieFile($this->cookieFile);
$curl->setCookieJar($this->cookieFile);

$curl->post($url,
[
Expand All @@ -351,7 +363,9 @@ public function isAuthorized()
$curl->setDefaultJsonDecoder($assoc = true);
$curl->setConnectTimeout(30);
$curl->setTimeout(60);

$curl->setCookies(self::getCookiesByHost());
$curl->setCookieFile($this->cookieFile);

$curl->get('https://steamcommunity.com/chat/clientjstoken');

Expand All @@ -371,7 +385,10 @@ private function getAdditionalCookies($url)
$curl->setDefaultJsonDecoder($assoc = true);
$curl->setConnectTimeout(30);
$curl->setTimeout(60);

$curl->setCookies(self::getCookiesByHost(self::getHostFromUrl($url)));
$curl->setCookieFile($this->cookieFile);
$curl->setCookieJar($this->cookieFile);

$curl->get($url);

Expand Down

0 comments on commit 8ed3ef3

Please sign in to comment.