<commit_before><?php
/**
 * This file is part of the turanct/showpad-api library
 *
 * @category Library
 * @package  Showpad
 * @author   <NAME> <<EMAIL>>
 * @license  MIT http://opensource.org/licenses/MIT
 * @link     http://www.sumocoders.be
 */

namespace Showpad;

/**
 * Basic Authentication Client
 */
class Authentication
{
    /**
     * @var ConfigInterface The configuration object
     */
    protected $config;

    /**
     * @var Adapter http client
     */
    protected $client;

    /**
     * Construct
     *
     * @param ConfigInterface $config The config object
     * @param Adapter         $client The http client
     */
    public function __construct(ConfigInterface $config, Adapter $client)
    {
        $this->config = $config;
        $this->client = $client;
    }

    /**
     * Authentication Start (OAuth2 Step 1)
     *
     * This method will return the url where users can authenticate our application
     *
     * @param string $redirectUrl The url where we want to redirect users to (and where we'll catch the OAuth2 code)
     *
     * @return string
     */
    public function authenticationStart($redirectUrl)
    {
        $url = $this->config->getEndpoint();
        $url .= '/oauth2/authorize';
        $url .= '?client_id=' . $this->config->getClientId();
        $url .= '&redirect_uri=' . urlencode($redirectUrl);
        $url .= '&response_type=code';

        return $url;
    }

    /**
     * Authentication Finish (OAuth2 Step 2)
     *
     * This method will request our access tokens using the code obtained in step 1
     *
     * @param string $code        The OAuth2 code obtained in step 1
     * @param string $redirectUrl The url where we want to redirect users to (and where we'll catch the OAuth2 tokens)
     *
     * @return New token pair
     */
    public function authenticationFinish($code, $redirectUrl)
    {
        $resource = $this->config->getEndpoint() . '/oauth2/token';

        $parameters = array(
            'grant_type' => 'authorization_code',
            'code' => $code,
            'redirect_uri' => $redirectUrl,
        );

        // Auth with basic auth
        $data = $this->client->request(
            'POST',
            $resource,
            $parameters,
            array('Authorization' => 'Basic ' . base64_encode($this->config->getClientId() . ':' . $this->config->getClientSecret()))
        );

        // Overwrite $this->config with new settings
        $this->config->setAccessToken($data['access_token']);
        $this->config->setRefreshToken($data['refresh_token']);

        // Return token data
        return $data;
    }

    /**
     * Refresh tokens
     *
     * @return array New token pair
     */
    public function refreshTokens()
    {
        $resource = $this->config->getEndpoint() . '/oauth2/token';

        $parameters = array(
            'grant_type' => 'refresh_token',
            'refresh_token' => $this->config->getRefreshToken(),
        );

        // Auth with basic auth
        $data = $this->client->request(
            'POST',
            $resource,
            $parameters,
            array('Authorization: Basic ' . base64_encode($this->config->getClientId() . ':' . $this->config->getClientSecret()))
        );

        // Overwrite $this->config with new settings
        $this->config->setAccessToken($data['access_token']);
        $this->config->setRefreshToken($data['refresh_token']);

        // Return token data
        return $data;
    }

    /**
     * Send an authenticated api request
     *
     * @param string $method     The HTTP method
     * @param string $endpoint   The api endpoint to send the request to
     * @param array  $parameters The parameters for the request (assoc array)
     *
     * return mixed<commit_msg>Fix authorization header for refreshing access tokens.<commit_after><?php
/**
 * This file is part of the turanct/showpad-api library
 *
 * @category Library
 * @package  Showpad
 * @author   <NAME> <<EMAIL>>
 * @license  MIT http://opensource.org/licenses/MIT
 * @link     http://www.sumocoders.be
 */

namespace Showpad;

/**
 * Basic Authentication Client
 */
class Authentication
{
    /**
     * @var ConfigInterface The configuration object
     */
    protected $config;

    /**
     * @var Adapter http client
     */
    protected $client;

    /**
     * Construct
     *
     * @param ConfigInterface $config The config object
     * @param Adapter         $client The http client
     */
    public function __construct(ConfigInterface $config, Adapter $client)
    {
        $this->config = $config;
        $this->client = $client;
    }

    /**
     * Authentication Start (OAuth2 Step 1)
     *
     * This method will return the url where users can authenticate our application
     *
     * @param string $redirectUrl The url where we want to redirect users to (and where we'll catch the OAuth2 code)
     *
     * @return string
     */
    public function authenticationStart($redirectUrl)
    {
        $url = $this->config->getEndpoint();
        $url .= '/oauth2/authorize';
        $url .= '?client_id=' . $this->config->getClientId();
        $url .= '&redirect_uri=' . urlencode($redirectUrl);
        $url .= '&response_type=code';

        return $url;
    }

    /**
     * Authentication Finish (OAuth2 Step 2)
     *
     * This method will request our access tokens using the code obtained in step 1
     *
     * @param string $code        The OAuth2 code obtained in step 1
     * @param string $redirectUrl The url where we want to redirect users to (and where we'll catch the OAuth2 tokens)
     *
     * @return New token pair
     */
    public function authenticationFinish($code, $redirectUrl)
    {
        $resource = $this->config->getEndpoint() . '/oauth2/token';

        $parameters = array(
            'grant_type' => 'authorization_code',
            'code' => $code,
            'redirect_uri' => $redirectUrl,
        );

        // Auth with basic auth
        $data = $this->client->request(
            'POST',
            $resource,
            $parameters,
            array('Authorization' => 'Basic ' . base64_encode($this->config->getClientId() . ':' . $this->config->getClientSecret()))
        );

        // Overwrite $this->config with new settings
        $this->config->setAccessToken($data['access_token']);
        $this->config->setRefreshToken($data['refresh_token']);

        // Return token data
        return $data;
    }

    /**
     * Refresh tokens
     *
     * @return array New token pair
     */
    public function refreshTokens()
    {
        $resource = $this->config->getEndpoint() . '/oauth2/token';

        $parameters = array(
            'grant_type' => 'refresh_token',
            'refresh_token' => $this->config->getRefreshToken(),
        );

        // Auth with basic auth
        $data = $this->client->request(
            'POST',
            $resource,
            $parameters,
            array('Authorization' => 'Basic ' . base64_encode($this->config->getClientId() . ':' . $this->config->getClientSecret()))
        );

        // Overwrite $this->config with new settings
        $this->config->setAccessToken($data['access_token']);
        $this->config->setRefreshToken($data['refresh_token']);

        // Return token data
        return $data;
    }

    /**
     * Send an authenticated api request
     *
     * @param string $method     The HTTP method
     * @param string $endpoint   The api endpoint to send the request to
     * @param array  $parameters The parameters for the request (assoc array)
     *
     * return mixed
{'fraction_non_alphanumeric': 0.12704221670369886, 'fraction_numerical': 0.004182459809175271, 'mean_word_length': 2.145088368269626, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 90, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5300396', 'n_tokens_mistral': 2131, 'n_tokens_neox': 2080, 'n_words': 583}