Skip to content

Commit

Permalink
Поддержка авторизации по токену
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavlov committed May 30, 2024
1 parent 220a0a9 commit e0cfc59
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
13 changes: 11 additions & 2 deletions src/Components/Http/MoySkladHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,22 @@ public function delete($method, $payload = [], $options = null){
*/
public function getRaw($link, $options) {
if (empty($options['headers']['Authorization'])) {
$options['headers']['Authorization'] = "Basic " . base64_encode($this->login . ':' . $this->password);
$options['headers']['Authorization'] = $this->getAuthorizationHeader();
}

$client = new Client();
return $client->get($link, $options);
}

private function getAuthorizationHeader() {
if (is_null($this->password)) {
$authorizationHeader = 'Bearer '.$this->login;
} else {
$authorizationHeader = "Basic " . base64_encode($this->login . ':' . $this->password);
}
return $authorizationHeader;
}

public function getLastRequest(){
return RequestLog::getLast();
}
Expand Down Expand Up @@ -158,7 +167,7 @@ private function makeRequest(

$headers = [
"Accept-Encoding" => "gzip",
"Authorization" => "Basic " . base64_encode($this->login . ':' . $password)
"Authorization" => $this->getAuthorizationHeader()
];
$config = [
"base_uri" => $endpoint,
Expand Down
12 changes: 8 additions & 4 deletions src/MoySklad.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,20 @@ private static function makeHash($login, $password){

/**
* Use it instead of constructor
* @param $login
* @param $loginOrToken
* @param $password
* @param string $subdomain
* @param $posToken
* @return MoySklad
*/
public static function getInstance($login, $password, $subdomain = "api", $posToken = null){
$hash = static::makeHash($login, $password);
public static function getInstance($loginOrToken, $password = null, $subdomain = "api", $posToken = null){
if (is_null($password)) {
$hash = $loginOrToken;
} else {
$hash = static::makeHash($loginOrToken, $password);
}
if ( empty(static::$instances[$hash]) ){
static::$instances[$hash] = new static($login, $password, $posToken, $hash, $subdomain);
static::$instances[$hash] = new static($loginOrToken, $password, $posToken, $hash, $subdomain);
EntityRegistry::instance()->bootEntities();
}
return static::$instances[$hash];
Expand Down

0 comments on commit e0cfc59

Please sign in to comment.