public function __construct(JWT $jwt, array $config = []) { $this->jwt = $jwt; $shopId = $jwt->get('id_shop'); $apiUrl = isset($config['base_uri']) ? $config['base_uri'] : self::API_URL; $baseUri = $apiUrl . 'v2/' . ($shopId ? sprintf('shops/%s/', $shopId) : ''); $defaultConfig = ['base_uri' => $baseUri, 'headers' => ['User-Agent' => sprintf('%s wizishop-php-sdk/%s', \GuzzleHttp\default_user_agent(), self::VERSION), 'Authorization' => 'Bearer ' . $this->jwt->getToken()]]; parent::__construct($defaultConfig + $config); }
public function testIsExpired() { $expiredJwt = JWT::fromString($this->expiredToken); $this->assertTrue($expiredJwt->isExpired()); $validJwt = JWT::fromString($this->neverExpiredToken); $this->assertFalse($validJwt->isExpired()); }
/** * @param string $username Username * @param string $password Password * @param array $config Guzzle client configuration settings * * @return AuthenticatedApiClient */ public static function authenticate($username, $password, array $config = []) { $client = new Client(['base_uri' => AuthenticatedApiClient::API_URL]); try { $authResponse = $client->post('/api/login_check', ['json' => ['username' => $username, 'password' => $password]]); $jsonResponse = json_decode($authResponse->getBody(), true); $jwt = JWT::fromString($jsonResponse['token']); $client = new AuthenticatedApiClient($jwt, $config); return $client; } catch (RequestException $e) { throw new AuthenticationException('Authentication problem', $e->getRequest(), $e->getResponse()); } }