<commit_before><?php

/**
 * OAuth 2.0 server implementation
 */

namespace Stalker\Lib\OAuth;

class OAuthServer
{
    private $access_handler;

    public function __construct(AccessHandler $access_handler){
        $this->access_handler = $access_handler;
    }

    public function handleAuthRequest(){

        $response = new OAuthResponse();

        $request  = new OAuthRequest();
        $response->setRequest($request);

        try{
            
            $request->parse();

            if ($request->isImplicitGrantAuth()){
                //todo: render auth page
            }else{

                if (!$this->access_handler->isValidClient($request->getClientId(), $request->getClientSecret())){
                    throw new OAuthInvalidClient("Client authentication failed", "http://tools.ietf.org/html/draft-ietf-oauth-v2-23#section-5.2");
                }

                if ($this->access_handler->checkUserAuth($request->getUsername(), $request->getPassword())){

                    $token = $this->access_handler->generateUniqueToken($request->getUsername());

                    if (!$token){
                        throw new OAuthServerError("Token making failed");
                    }

                    $response->setAccessToken($token);

                    $key = $this->access_handler->getSecretKey($request->getUsername());

                    $response->setMacKey($key);

                    $additional_params = $this->access_handler->getAdditionalParams($request->getUsername());
                    if (!empty($additional_params)){
                        $response->setAdditionalParams($additional_params);
                    }
                }else{
                    throw new OAuthInvalidClient("Username or password is incorrect");
                }
            }
            
        }catch(OAuthException $e){
            if ($request->isImplicitGrantAuth()){
                //todo: render error page
                echo $e->getMessage();
            }else{
                $response->setError($e->getCode(), $e->getMessage(), $e->getUrl());
            }
        }
        
        $response->send();
    }
}

class OAuthResponse
{
    protected $body = array('token_type' => 'mac', 'mac_algorithm' => 'hmac-sha-256');
    protected $request;
    private   $error_fields;

    public function __construct(){
        ob_start();
        $this->error_fields = array_fill_keys(array('status', 'error', 'debug'), true);

        if ($_SERVER['REQUEST_METHOD'] !== 'POST'){
            header($_SERVER["SERVER_PROTOCOL"]." 405 Method Not Allowed");
            echo "<pre>Method not allowed</pre>";
            exit;
        }
    }

    public function setRequest($request){
        $this->request = $request;
    }

    public function setError($error, $description = "", $error_uri = ""){

        $this->body['error'] = $error;

        if (!empty($description)){
            $this->body['error_description'] = $description;
        }

        if (!empty($error_uri)){
            $this->body['error_uri'] = $error_uri;
        }
    }

    public function setAccessToken($token){
        $this->body['access_token'] = $token;
    }

    public function setTokenType($token_type){
        $this->body['token_type'] = $token_type;
    }

    public function setMacKey($key){
        $this->body['mac_key'] = $key;
    }

    public function setMacAlgorithm($algorithm){
        $this->body['mac_algorithm'] = $algorithm;
    }
<commit_msg>Added mac and bearer token types support<commit_after><?php

/**
 * OAuth 2.0 server implementation
 */

namespace Stalker\Lib\OAuth;

class OAuthServer
{
    private $access_handler;
    private $token_type;

    public function __construct(AccessHandler $access_handler){
        $this->access_handler = $access_handler;
    }

    public function handleAuthRequest(){

        $response = new OAuthResponse();

        $request  = new OAuthRequest();
        $response->setRequest($request);
        $response->setTokenType($this->token_type);

        try{
            
            $request->parse();

            if ($request->isImplicitGrantAuth()){
                //todo: render auth page
            }else{

                if (!$this->access_handler->isValidClient($request->getClientId(), $request->getClientSecret())){
                    throw new OAuthInvalidClient("Client authentication failed", "http://tools.ietf.org/html/draft-ietf-oauth-v2-23#section-5.2");
                }

                if ($this->access_handler->checkUserAuth($request->getUsername(), $request->getPassword())){

                    $token = $this->access_handler->generateUniqueToken($request->getUsername());

                    if (!$token){
                        throw new OAuthServerError("Token making failed");
                    }

                    $response->setAccessToken($token);

                    if ($this->token_type == "mac"){
                        $key = $this->access_handler->getSecretKey($request->getUsername());
                        $response->setMacKey($key);
                    }

                    $additional_params = $this->access_handler->getAdditionalParams($request->getUsername());

                    if (!empty($additional_params)){
                        $response->setAdditionalParams($additional_params);
                    }
                }else{
                    throw new OAuthInvalidClient("Username or password is incorrect");
                }
            }
            
        }catch(OAuthException $e){
            if ($request->isImplicitGrantAuth()){
                echo $e->getMessage();
            }else{
                $response->setError($e->getCode(), $e->getMessage(), $e->getUrl());
            }
        }
        
        $response->send();
    }

    public function setTokenType($token_type){

        if (!in_array($token_type, array("bearer", "mac"))){
            throw new OAuthInvalidRequest("Not supported access type");
        }

        $this->token_type = $token_type;
    }
}

class OAuthResponse
{
    protected $body = array();
    protected $request;
    private   $error_fields;
    private   $token_type;

    public function __construct(){
        ob_start();
        $this->error_fields = array_fill_keys(array('status', 'error', 'error_description', 'error_uri', 'debug'), true);

        if ($_SERVER['REQUEST_METHOD'] !== 'POST'){
            header($_SERVER["SERVER_PROTOCOL"]." 405 Method Not Allowed");
            echo "<pre>Method not allowed</pre>";
            exit;
        }
    }

    public function setTokenType($token_type){
        $this->token_type = $token_type;

        $this->body['token_type'] = $this->token_type;

        if ($this->token_type == "mac"){
            $this->body['mac_algorithm'] = 'hmac-sha-256';
        }
    }

    public function setRequest($request){
        $this->request = $request;
    }

    public function setError($error, $description = "", $error_uri = ""){

        $this->body['error'] = $error;

        if (!empty($description)){
            $this->body['error_description'] = $description;
        }

        if (!empty($error_uri)){
            $this->body['error_uri'] = $error_uri;
        }
    }

    public function setAccessToken($token){
        $this->body['access_token'] = $token;
    }

    public function setMacKey($key){
        $this->body['mac_key'] = $key;
    }

    public function setMacAlgorithm($algorithm){
        $this->body['mac_algorithm'] = $algorithm;
    }

{'fraction_non_alphanumeric': 0.14314115308151093, 'fraction_numerical': 0.0034459907223326706, 'mean_word_length': 2.006374501992032, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 97, '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': '4996888', 'n_tokens_mistral': 2072, 'n_tokens_neox': 1955, 'n_words': 357}