OAUTH_CONFIG = Config::get('container_path') . $this->OAUTH_CONFIG; } /** * @param SigningFetcher $fetcher */ public function initFromConfigFile($fetcher) { // Read our consumer keys and secrets from config/oauth.js // This actually involves fetching gadget specs try { $oauthConfigStr = file_get_contents($this->OAUTH_CONFIG); // remove all comments because this confuses the json parser // note: the json parser also crashes on trailing ,'s in records so please don't use them $contents = preg_replace('@/\\*.*?\\*/@s', '', $oauthConfigStr); $oauthConfig = json_decode($contents, true); if ($oauthConfig == $contents) { throw new GadgetException("OAuth configuration json failed to parse."); } foreach ($oauthConfig as $gadgetUri => $value) { $this->storeConsumerInfos($gadgetUri, $value); } } catch (\Exception $e) { throw new GadgetException($e); } } /** * * @param string $gadgetUri * @param array $oauthConfig */ protected function storeConsumerInfos($gadgetUri, $oauthConfig) { foreach ($oauthConfig as $key => $value) { $serviceName = $key; $consumerInfo = $value; $this->storeConsumerInfo($gadgetUri, $serviceName, $consumerInfo); } } /** * * @param string $gadgetUri * @param string $serviceName * @param array $consumerInfo */ protected function storeConsumerInfo($gadgetUri, $serviceName, $consumerInfo) { if (! isset($consumerInfo[$this->CONSUMER_SECRET_KEY]) || ! isset($consumerInfo[$this->CONSUMER_KEY_KEY]) || ! isset($consumerInfo[$this->KEY_TYPE_KEY])) { throw new \Exception("Invalid configuration in oauth.json"); } $consumerSecret = $consumerInfo[$this->CONSUMER_SECRET_KEY]; $consumerKey = $consumerInfo[$this->CONSUMER_KEY_KEY]; $keyTypeStr = $consumerInfo[$this->KEY_TYPE_KEY]; $keyType = 'HMAC_SYMMETRIC'; if ($keyTypeStr == "RSA_PRIVATE") { $keyType = 'RSA_PRIVATE'; $cache = Cache::createCache(Config::get('data_cache'), 'OAuthToken'); if (($cachedRequest = $cache->get(md5("RSA_KEY_" . $serviceName))) !== false) { $consumerSecret = $cachedRequest; } else { $key = $consumerInfo[$this->CONSUMER_SECRET_KEY]; if (empty($key)) { throw new \Exception("Invalid key"); } if (strpos($key, "-----BEGIN") === false) { $strip_this = array(" ", "\n", "\r"); //removes breaklines and trim. $rsa_private_key = trim(str_replace($strip_this, "", $key)); $consumerSecret = ShindigOAuth::$BEGIN_PRIVATE_KEY . "\n"; $chunks = str_split($rsa_private_key, 64); foreach ($chunks as $chunk) { $consumerSecret .= $chunk . "\n"; } $consumerSecret .= ShindigOAuth::$END_PRIVATE_KEY; } else { $consumerSecret = $key; } $cache->set(md5("RSA_KEY_" . $serviceName), $consumerSecret); } } $kas = new ConsumerKeyAndSecret($consumerKey, $consumerSecret, $keyType); $this->storeConsumerKeyAndSecret($gadgetUri, $serviceName, $kas); } }