<commit_before><?php namespace API;
require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once dirname(dirname(__FILE__)) . '/controllers/auth/auth.controller.php';

class APIAuth {    
    
    const APISESSIONNAME = 'API_AUTHENTICATED_USER_ID';
    
    static function isAuthorized($app, $role = 'public') {
        if(strtolower($role) === 'public') {
            return true;
        }
        
        $user = AuthController::authorizeApiToken($app);
        if($user) {
            // Save that user id
            $_SESSION[self::APISESSIONNAME] = $user;
            return true;
        } else {
            $response = array('data' => array(
                'msg' => 'Unauthorized API Access', 
                'sent' => $app->request->post()), 
                'user' => $user,
                'meta' => array('error' => true, 'status' => 401));
            //$response = array('data' => array('msg' => 'Unauthorized API Access'), 'meta' => array('error' => true, 'status' => 401));
            $app->halt(401, json_encode($response));
            return false;
        }
    }
    
    static function getUserId() {        
        return (isset($_SESSION[self::APISESSIONNAME])) ? $_SESSION[self::APISESSIONNAME] : '0';
    }
    
}<commit_msg>Moved validate authorize api token method to the aut controller to remove user controller dependency.<commit_after><?php namespace API;
require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once dirname(dirname(__FILE__)) . '/controllers/user/user.controller.php';

use \Respect\Validation\Validator as v;

class APIAuth {    
    
    const APISESSIONNAME = 'API_AUTHENTICATED_USER_ID';
    
    static function isAuthorized($app, $role = 'public') {
        if(strtolower($role) === 'public') {
            return true;
        }
        
        $user = self::authorizeApiToken($app);
        if($user) {
            // Save that user id
            $_SESSION[self::APISESSIONNAME] = $user;
            return true;
        } else {
            $response = array('data' => array(
                'msg' => 'Unauthorized API Access', 
                'sent' => $app->request->post()), 
                'user' => $user,
                'meta' => array('error' => true, 'status' => 401));
            //$response = array('data' => array('msg' => 'Unauthorized API Access'), 'meta' => array('error' => true, 'status' => 401));
            $app->halt(401, json_encode($response));
            return false;
        }
    }
    
    private static function authorizeApiToken($app) {
        if(!v::key('apiKey', v::stringType())->validate($app->request->post()) || 
           !v::key('apiToken', v::stringType())->validate($app->request->post())) {
            return false;
        }
        $user = UserData::selectUserByIdentifierToken($app->request->post('apiKey'));
        if(!$user) {
            return "user";
        }
        if(!password_verify($app->request->post('apiToken'), $user->apiToken)) {
            return "password";
        }
        // Go now. Be free little brother.
        return $user->id;
    }
    
    static function getUserId() {        
        return (isset($_SESSION[self::APISESSIONNAME])) ? $_SESSION[self::APISESSIONNAME] : '0';
    }
    
}
{'fraction_non_alphanumeric': 0.16167481662591687, 'fraction_numerical': 0.006112469437652812, 'mean_word_length': 2.2056807051909892, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2106132', 'n_tokens_mistral': 946, 'n_tokens_neox': 926, 'n_words': 191}