key_auth.class.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. use \ChamiloSession as Session;
  3. /**
  4. * Used to authenticate user with an access token. By default this method is disabled.
  5. * Method used primarily to make API calls: Rss, file upload.
  6. *
  7. * Access is granted only for the services that are enabled.
  8. *
  9. * To be secured this method must
  10. *
  11. * 1) be called through httpS to avoid sniffing (note that this is the case anyway with other methods such as cookies)
  12. * 2) the url/access token must be secured
  13. *
  14. * This authentication method is session less. This is to ensure that the navigator
  15. * do not receive an access cookie that will grant it access to other parts of the
  16. * application.
  17. *
  18. *
  19. * Usage:
  20. *
  21. * Enable KeyAuth for a specific service. Add the following lines so that
  22. * the key authentication method is enabled for a specific service before
  23. * calling global.inc.php.
  24. *
  25. * KeyAuth::enable_services('my_service');
  26. * include_once '.../main/inc/global.inc.php';
  27. *
  28. *
  29. * Enable url access for a short period of time:
  30. *
  31. * token = KeyAuth::create_temp_token();
  32. * url = '...?access_token=' . $token ;
  33. *
  34. * @see AccessToken
  35. * @license see /license.txt
  36. * @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva
  37. */
  38. class KeyAuth
  39. {
  40. const PARAM_ACCESS_TOKEN = 'access_token';
  41. protected static $services = array();
  42. public static function create_temp_token($service = null, $duration = 60, $user_id = null)
  43. {
  44. return UserApiKeyManager::create_temp_token($service, $duration, $user_id);
  45. }
  46. /**
  47. * Returns enabled services
  48. *
  49. * @return array
  50. */
  51. public static function get_services()
  52. {
  53. return self::$services;
  54. }
  55. /**
  56. * Name of the service for which we are goint to check the API Key.
  57. * If empty it disables authentication.
  58. *
  59. * !! 10 chars max !!
  60. */
  61. public static function enable_services($_)
  62. {
  63. $args = func_get_args();
  64. $names = array();
  65. foreach ($args as $arg) {
  66. if (is_object($arg)) {
  67. $f = array($arg, 'get_service_name');
  68. $name = call_user_func($f);
  69. } else {
  70. $name = $arg;
  71. }
  72. $name = substr($name, 0, 10);
  73. self::$services[$name] = $name;
  74. }
  75. }
  76. public static function disable_services($_)
  77. {
  78. $args = func_get_args();
  79. $names = array();
  80. foreach ($args as $name) {
  81. $name = substr($name, 0, 10);
  82. unset(self::$services[$name]);
  83. }
  84. }
  85. public static function is_service_enabled($service)
  86. {
  87. $services = self::get_services();
  88. foreach ($services as $s) {
  89. if ($s == $service) {
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. public static function clear_services()
  96. {
  97. self::$services[$name] = array();
  98. }
  99. /**
  100. * Enable key authentication for the default service - i.e. chamilo
  101. */
  102. public static function enable()
  103. {
  104. self::enable_services(UserApiKeyManager::default_service());
  105. }
  106. public static function disable()
  107. {
  108. self::$services[$name] = array();
  109. }
  110. /**
  111. * Returns true if the key authentication method is enabled. False otherwise.
  112. * Default to false.
  113. *
  114. * @return bool
  115. */
  116. public static function is_enabled()
  117. {
  118. return !empty(self::$services);
  119. }
  120. /**
  121. * @return KeyAuth
  122. */
  123. public static function instance()
  124. {
  125. static $result = null;
  126. if (empty($result)) {
  127. $result = new self();
  128. }
  129. return $result;
  130. }
  131. protected function __construct()
  132. {
  133. }
  134. /**
  135. * Returns true if authentication accepts to run otherwise returns false.
  136. *
  137. * @return boolean
  138. */
  139. public function accept()
  140. {
  141. /**
  142. * Authentication method must be enabled
  143. */
  144. if (!self::is_enabled()) {
  145. return false;
  146. }
  147. $token = $this->get_access_token();
  148. if ($token->is_empty()) {
  149. return false;
  150. }
  151. $key = UserApiKeyManager::get_by_id($token->get_id());
  152. if (empty($key)) {
  153. return false;
  154. }
  155. /**
  156. * The service corresponding to the key must be enabled.
  157. */
  158. $service = $key['api_service'];
  159. if (!self::is_service_enabled($service)) {
  160. return false;
  161. }
  162. /**
  163. * User associated with the key must be active
  164. */
  165. $user = UserManager::get_user_info_by_id($token->get_user_id());
  166. if (empty($user)) {
  167. return false;
  168. }
  169. if (!$user['active']) {
  170. return false;
  171. }
  172. /**
  173. * Token must be valid.
  174. */
  175. return $token->is_valid();
  176. }
  177. /**
  178. * If accepted tear down session, log in user and returns true.
  179. * If not accepted do nothing and returns false.
  180. *
  181. * @return boolean
  182. */
  183. public function login()
  184. {
  185. if (!$this->accept()) {
  186. return false;
  187. }
  188. /**
  189. * ! important this is to ensure we don't grant access for other parts
  190. */
  191. Session::destroy();
  192. /**
  193. * We don't allow redirection since access is granted only for this call
  194. */
  195. global $no_redirection, $noredirection;
  196. $no_redirection = true;
  197. $noredirection = true;
  198. Session::write('noredirection', $noredirection);
  199. $user_id = $this->get_user_id();
  200. $course_code = $this->get_course_code();
  201. $group_id = $this->get_group_id();
  202. Login::init_user($user_id, true);
  203. Login::init_course($course_code, true);
  204. Login::init_group($group_id, true);
  205. return true;
  206. }
  207. /**
  208. * Returns the request access token
  209. *
  210. * @return AccessToken
  211. */
  212. public function get_access_token()
  213. {
  214. $string = Request::get(self::PARAM_ACCESS_TOKEN);
  215. return AccessToken::parse($string);
  216. }
  217. public function get_user_id()
  218. {
  219. return $this->get_access_token()->get_user_id();
  220. }
  221. public function get_course_code()
  222. {
  223. return Request::get('cidReq', 0);
  224. }
  225. public function get_group_id()
  226. {
  227. return Request::get('gidReq', 0);
  228. }
  229. }