/** * Save AT and timestamp in file * * @param string $access_token * @param int $timestamp * * @return bool */ private function save($access_token, $timestamp) { unset($this->runtime_file_content); $bytes = @file_put_contents($this->getFilePath(), json_encode(array('a' => $access_token, 'e' => $timestamp))); if ($bytes === false) { // fatal log will throw an exception $this->logger->fatal(__CLASS__, 'store', sprintf('Unable to store access token in %s', $this->getFilePath())); } $this->logger->debug(__CLASS__, 'store', sprintf('Access token stored in %s', $this->getFilePath())); return true; }
/** * @return array|string * @throws \MicrosoftTranslator\Exception */ private function generateAndStoreNewAccessToken() { $url = trim($this->auth_base_url, "/ \t\n\r\v"); $access_token = null; $auth = array('grant_type' => 'client_credentials', 'client_id' => $this->api_client_id, 'client_secret' => $this->api_client_secret, 'scope' => $this->api_client_scope); $result = $this->http->post($url, null, $auth, null); if (Http::isRequestOk($result)) { $result['http_body'] = json_decode($result['http_body'], true); if (!isset($result['http_body']['access_token'])) { throw new Exception('Access token not found in response'); } if (!is_string($result['http_body']['access_token'])) { throw new Exception('Access token found in response but it is not a string'); } $access_token = strval(@$result['http_body']['access_token']); $expires_in = @(int) $result['http_body']['expires_in']; $this->logger->debug(__CLASS__, 'oauth', sprintf('New access_token generated %s...', substr($access_token, 0, 10))); $this->guard->storeAccessTokenForSeconds($access_token, $expires_in); } else { $this->logger->fatal(__CLASS__, 'oauth', 'Unable to generate a new access token : ' . json_encode($result)); } return $access_token; }