3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 class BasicGadgetOAuthTokenStore
extends GadgetOAuthTokenStore
{
23 /** default location for consumer keys and secrets */
24 private $OAUTH_CONFIG = "../config/oauth.json";
25 private $CONSUMER_SECRET_KEY = "consumer_secret";
26 private $CONSUMER_KEY_KEY = "consumer_key";
27 private $KEY_TYPE_KEY = "key_type";
29 public function __construct($store, $fetcher) {
30 parent
::__construct($store, $fetcher);
33 public function initFromConfigFile($fetcher) {
34 // Read our consumer keys and secrets from config/oauth.js
35 // This actually involves fetching gadget specs
37 $oauthConfigStr = file_get_contents($this->OAUTH_CONFIG
);
38 // remove all comments because this confuses the json parser
39 // note: the json parser also crashes on trailing ,'s in records so please don't use them
40 $contents = preg_replace('@/\\*(?:.|[\\n\\r])*?\\*/@', '', $oauthConfigStr);
41 $oauthConfig = json_decode($contents, true);
42 if ($oauthConfig == $contents) {
43 throw new GadgetException("OAuth configuration json failed to parse.");
45 foreach ($oauthConfig as $gadgetUri => $value) {
46 $this->storeConsumerInfos($gadgetUri, $value);
48 } catch (Exception
$e) {
49 throw new GadgetException($e);
53 private function storeConsumerInfos($gadgetUri, $oauthConfig) {
54 foreach ($oauthConfig as $key => $value) {
56 $consumerInfo = $value;
57 $this->storeConsumerInfo($gadgetUri, $serviceName, $consumerInfo);
61 private function storeConsumerInfo($gadgetUri, $serviceName, $consumerInfo) {
62 if (! isset($consumerInfo[$this->CONSUMER_SECRET_KEY
]) ||
! isset($consumerInfo[$this->CONSUMER_KEY_KEY
]) ||
! isset($consumerInfo[$this->KEY_TYPE_KEY
])) {
63 throw new Exception("Invalid configuration in oauth.json");
65 $consumerSecret = $consumerInfo[$this->CONSUMER_SECRET_KEY
];
66 $consumerKey = $consumerInfo[$this->CONSUMER_KEY_KEY
];
67 $keyTypeStr = $consumerInfo[$this->KEY_TYPE_KEY
];
68 $keyType = 'HMAC_SYMMETRIC';
69 if ($keyTypeStr == "RSA_PRIVATE") {
70 $keyType = 'RSA_PRIVATE';
71 $cache = Config
::get('data_cache');
72 $cache = new $cache();
73 if (($cachedRequest = $cache->get(md5("RSA_KEY_" . $serviceName))) !== false) {
74 $consumerSecret = $cachedRequest;
76 $key = $consumerInfo[$this->CONSUMER_SECRET_KEY
];
78 throw new Exception("Invalid key");
80 if (strpos($key, "-----BEGIN") === false) {
81 $strip_this = array(" ", "\n", "\r");
82 //removes breaklines and trim.
83 $rsa_private_key = trim(str_replace($strip_this, "", $key));
84 $consumerSecret = OAuth
::$BEGIN_PRIVATE_KEY . "\n";
85 $chunks = str_split($rsa_private_key, 64);
86 foreach ($chunks as $chunk) {
87 $consumerSecret .= $chunk . "\n";
89 $consumerSecret .= OAuth
::$END_PRIVATE_KEY;
91 $consumerSecret = $key;
93 $cache->set(md5("RSA_KEY_" . $serviceName), $consumerSecret);
96 $kas = new ConsumerKeyAndSecret($consumerKey, $consumerSecret, $keyType);
97 $this->storeConsumerKeyAndSecret($gadgetUri, $serviceName, $kas);