/** * Purge all expired API keys from the database */ public function removeOldKeys() { $keys = \CMF\Model\User\Apikey::select('item')->andWhere('item.expires_at < :now')->setParameter('now', new \DateTime())->getQuery()->getResult(); foreach ($keys as $key) { \D::manager()->remove($key); } \D::manager()->flush(); }
public function authorise() { // If there's a valid session already, allow access $user_type = \Input::param('user_type') ?: 'Admin\\Model_User'; if (\CMF\Auth::logged_in(null, $user_type)) { return; } $auth = explode(' ', \Input::headers('Authorization', ' ')); $sent_key = \Arr::get($auth, 1); // Try and find a valid key $key = \CMF\Model\User\Apikey::select('item')->where('item.access_token = :key')->andWhere('item.expires_at > :now')->setParameter('key', $sent_key)->setParameter('now', new \DateTime())->getQuery()->getResult(); // Check the scope of the key, if one was found if (count($key)) { $key = $key[0]; if ($key->scope == 'api') { return; } } throw new \HttpException('Login Required', \HttpException::UNAUTHORIZED); }