Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 48 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| DataStore | |
0.00% |
0 / 48 |
|
0.00% |
0 / 7 |
240 | |
0.00% |
0 / 1 |
| findOauthConsumerResource | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| getOauthConsumer | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
42 | |||
| lookup_consumer | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| lookup_token | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| lookup_nonce | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| new_request_token | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| new_access_token | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 19 | * (under the project TAO & TAO2); |
| 20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 21 | * (under the project TAO-TRANSFER); |
| 22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 24 | * 2013 (update and modification) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 25 | */ |
| 26 | |
| 27 | namespace oat\tao\model\oauth; |
| 28 | |
| 29 | use oat\tao\model\oauth\lockout\LockoutInterface; |
| 30 | use oat\tao\model\TaoOntology; |
| 31 | use IMSGlobal\LTI\OAuth\OAuthDataStore; |
| 32 | use IMSGlobal\LTI\OAuth\OAuthConsumer; |
| 33 | use IMSGlobal\LTI\OAuth\OAuthToken; |
| 34 | use oat\oatbox\service\ConfigurableService; |
| 35 | use oat\generis\model\OntologyAwareTrait; |
| 36 | |
| 37 | /** |
| 38 | * Tao Implementation of an OAuthDatastore |
| 39 | * Does not yet implement the nonce and request/access token |
| 40 | * |
| 41 | * @access public |
| 42 | * @author Joel Bout, <joel@taotesting.com> |
| 43 | * @package tao |
| 44 | */ |
| 45 | class DataStore extends ConfigurableService implements ImsOauthDataStoreInterface |
| 46 | { |
| 47 | use OntologyAwareTrait; |
| 48 | |
| 49 | public const OPTION_NONCE_STORE = 'nonce'; |
| 50 | |
| 51 | public const CLASS_URI_OAUTH_CONSUMER = 'http://www.tao.lu/Ontologies/TAO.rdf#OauthConsumer'; |
| 52 | public const PROPERTY_OAUTH_KEY = 'http://www.tao.lu/Ontologies/TAO.rdf#OauthKey'; |
| 53 | public const PROPERTY_OAUTH_SECRET = 'http://www.tao.lu/Ontologies/TAO.rdf#OauthSecret'; |
| 54 | public const PROPERTY_OAUTH_CALLBACK = 'http://www.tao.lu/Ontologies/TAO.rdf#OauthCallbackUrl'; |
| 55 | |
| 56 | /** |
| 57 | * Helper function to find the OauthConsumer RDF Resource |
| 58 | * |
| 59 | * @access public |
| 60 | * @author Joel Bout, <joel@taotesting.com> |
| 61 | * @param string consumer_key |
| 62 | * @return \core_kernel_classes_Resource |
| 63 | */ |
| 64 | public function findOauthConsumerResource($consumer_key) |
| 65 | { |
| 66 | $returnValue = null; |
| 67 | |
| 68 | $class = $this->getClass(self::CLASS_URI_OAUTH_CONSUMER); |
| 69 | $instances = $class->searchInstances( |
| 70 | [self::PROPERTY_OAUTH_KEY => $consumer_key], |
| 71 | ['like' => false, 'recursive' => true] |
| 72 | ); |
| 73 | if (count($instances) == 0) { |
| 74 | $oauthService = $this->getServiceLocator()->get(OauthService::SERVICE_ID); |
| 75 | /** @var LockoutInterface $lockoutService */ |
| 76 | $lockoutService = $oauthService->getSubService(OauthService::OPTION_LOCKOUT_SERVICE); |
| 77 | $lockoutService->logFailedAttempt(); |
| 78 | throw new \tao_models_classes_oauth_Exception('No Credentials for consumer key ' . $consumer_key); |
| 79 | } |
| 80 | if (count($instances) > 1) { |
| 81 | throw new \tao_models_classes_oauth_Exception('Multiple Credentials for consumer key ' . $consumer_key); |
| 82 | } |
| 83 | $returnValue = current($instances); |
| 84 | |
| 85 | return $returnValue; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Returns the OAuthConsumer for the provided credentials |
| 90 | * |
| 91 | * @param \common_http_Credentials $consumer |
| 92 | * @throws \tao_models_classes_oauth_Exception |
| 93 | * @return \IMSGlobal\LTI\OAuth\OAuthConsumer |
| 94 | */ |
| 95 | public function getOauthConsumer(\common_http_Credentials $credentials) |
| 96 | { |
| 97 | if (!$credentials instanceof \core_kernel_classes_Resource) { |
| 98 | throw new \tao_models_classes_oauth_Exception('Unsupported credential type ' . get_class($credentials)); |
| 99 | } |
| 100 | $values = $credentials->getPropertiesValues([ |
| 101 | self::PROPERTY_OAUTH_KEY, |
| 102 | self::PROPERTY_OAUTH_SECRET, |
| 103 | self::PROPERTY_OAUTH_CALLBACK |
| 104 | ]); |
| 105 | if (empty($values[self::PROPERTY_OAUTH_KEY]) || empty($values[self::PROPERTY_OAUTH_SECRET])) { |
| 106 | throw new \tao_models_classes_oauth_Exception( |
| 107 | 'Incomplete oauth consumer definition for ' . $credentials->getUri() |
| 108 | ); |
| 109 | } |
| 110 | $consumer_key = (string)current($values[self::PROPERTY_OAUTH_KEY]); |
| 111 | $secret = (string)current($values[self::PROPERTY_OAUTH_SECRET]); |
| 112 | if (!empty($values[self::PROPERTY_OAUTH_CALLBACK])) { |
| 113 | $callbackUrl = (string)current($values[self::PROPERTY_OAUTH_CALLBACK]); |
| 114 | if (empty($callbackUrl)) { |
| 115 | $callbackUrl = null; |
| 116 | } |
| 117 | } else { |
| 118 | $callbackUrl = null; |
| 119 | } |
| 120 | return new OAuthConsumer($consumer_key, $secret, $callbackUrl); |
| 121 | } |
| 122 | |
| 123 | |
| 124 | /** |
| 125 | * returns the OauthConsumer for the specified key |
| 126 | * |
| 127 | * @access public |
| 128 | * @author Joel Bout, <joel@taotesting.com> |
| 129 | * @param consumer_key |
| 130 | * @return OAuthConsumer |
| 131 | * |
| 132 | * phpcs:disable PSR1.Methods.CamelCapsMethodName |
| 133 | */ |
| 134 | public function lookup_consumer($consumer_key) |
| 135 | { |
| 136 | $returnValue = null; |
| 137 | |
| 138 | $consumer = $this->findOauthConsumerResource($consumer_key); |
| 139 | $secret = (string)$consumer->getUniquePropertyValue($this->getProperty(self::PROPERTY_OAUTH_SECRET)); |
| 140 | $callbackUrl = null; |
| 141 | |
| 142 | $returnValue = new OAuthConsumer($consumer_key, $secret, $callbackUrl); |
| 143 | |
| 144 | return $returnValue; |
| 145 | } |
| 146 | // phpcs:enable PSR1.Methods.CamelCapsMethodName |
| 147 | |
| 148 | /** |
| 149 | * Should verify if the token exists and return it |
| 150 | * Always returns an token with an empty secret for now |
| 151 | * |
| 152 | * @access public |
| 153 | * @author Joel Bout, <joel@taotesting.com> |
| 154 | * @param consumer |
| 155 | * @param token_type |
| 156 | * @param token |
| 157 | * @return mixed |
| 158 | * |
| 159 | * phpcs:disable PSR1.Methods.CamelCapsMethodName |
| 160 | */ |
| 161 | public function lookup_token($consumer, $token_type, $token) |
| 162 | { |
| 163 | \common_Logger::d(__CLASS__ . '::' . __FUNCTION__ . ' called for token ' . $token . ' of type ' . $token_type); |
| 164 | return new OAuthToken($consumer, ""); |
| 165 | } |
| 166 | // phpcs:enable PSR1.Methods.CamelCapsMethodName |
| 167 | |
| 168 | /** |
| 169 | * Should verify if a nonce has already been used |
| 170 | * always return NULL, meaning that nonces can be reused |
| 171 | * |
| 172 | * @access public |
| 173 | * @author Joel Bout, <joel@taotesting.com> |
| 174 | * @param OAuthConsumer $consumer |
| 175 | * @param OAuthToken $token |
| 176 | * @param string $nonce |
| 177 | * @param string $timestamp |
| 178 | * @return mixed |
| 179 | * |
| 180 | * phpcs:disable PSR1.Methods.CamelCapsMethodName |
| 181 | */ |
| 182 | public function lookup_nonce($consumer, $token, $nonce, $timestamp) |
| 183 | { |
| 184 | $store = $this->getSubService(self::OPTION_NONCE_STORE); |
| 185 | return $store->isValid($timestamp . '_' . $consumer->key . '_' . $nonce) ? null : true; |
| 186 | } |
| 187 | // phpcs:enable PSR1.Methods.CamelCapsMethodName |
| 188 | |
| 189 | /** |
| 190 | * Should create a new request token |
| 191 | * not implemented |
| 192 | * |
| 193 | * @access public |
| 194 | * @author Joel Bout, <joel@taotesting.com> |
| 195 | * @param consumer |
| 196 | * @param callback |
| 197 | * @return mixed |
| 198 | * |
| 199 | * phpcs:disable PSR1.Methods.CamelCapsMethodName |
| 200 | */ |
| 201 | public function new_request_token($consumer, $callback = null) |
| 202 | { |
| 203 | \common_Logger::d(__CLASS__ . '::' . __FUNCTION__ . ' called'); |
| 204 | return null; |
| 205 | } |
| 206 | // phpcs:enable PSR1.Methods.CamelCapsMethodName |
| 207 | |
| 208 | /** |
| 209 | * Should create a new access token |
| 210 | * not implemented |
| 211 | * |
| 212 | * @access public |
| 213 | * @author Joel Bout, <joel@taotesting.com> |
| 214 | * @param token |
| 215 | * @param consumer |
| 216 | * @return mixed |
| 217 | * |
| 218 | * phpcs:disable PSR1.Methods.CamelCapsMethodName |
| 219 | */ |
| 220 | public function new_access_token($token, $consumer, $verifier = null) |
| 221 | { |
| 222 | \common_Logger::d(__CLASS__ . '::' . __FUNCTION__ . ' called'); |
| 223 | return null; |
| 224 | } |
| 225 | // phpcs:enable PSR1.Methods.CamelCapsMethodName |
| 226 | } |