<commit_before><?php namespace Mrynk\L4GoogleApi;

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\UserProviderInterface;
use Illuminate\Support\Facades\App;
use Illuminate\Auth\GenericUser;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\URL;

class GoogleUserProvider implements UserProviderInterface
{

    function __construct()
    {
        $this->client = App::make('google-client');
        $this->oauth2 = new \Google_Service_Oauth2( $this->client );

        if ( Session::has( $this->getTokenName() ) )
        {
            $this->client->setAccessToken( Session::get( $this->getTokenName() ) );
        }
    }

    public function retrieveByToken( $identifier, $token ){}
    public function updateRememberToken( UserInterface $user, $token ){}


    /**
     * Retrieve a user by their unique identifier.
     *
     * @param  mixed $identifier
     * @return \Illuminate\Auth\UserInterface|null
     */
    public function retrieveById( $identifier )
    {
        $user = $this->retrieveByCredentials(array());
        if ($user && $user->getAuthIdentifier() == $identifier) {
            return $user;
        }
    }

    /**
     * Retrieve a user by the given credentials.
     *
     * @param  array $credentials
     * @return \Illuminate\Auth\UserInterface|null
     */
    public function retrieveByCredentials( array $credentials )
    {
        if ($this->client->getAccessToken()) {

            $userinfo = $this->oauth2->userinfo->get();
            $knownUser = $this->createModel()->newQuery()->where( 'google_id', $userinfo->id )->first();
            if( !$knownUser )
                $this->createModel()->fill( (array) $userinfo )->save();
            return $knownUser;
            //return new GenericUser( (array) $userinfo );
        }
    }

    /**
     * Validate a user against the given credentials.
     *
     * @param  \Illuminate\Auth\UserInterface $user
     * @param  array $credentials
     * @return bool
     */
    public function validateCredentials( UserInterface $user, array $credentials )
    {
        // this method doesn't make sense for Google auth
        return false;
    }

    public function getAuthUrl()
    {
        return $this->client->createAuthUrl() . ( \Config::get('l4-google-api::hd') ? '&hd='.\Config::get('l4-google-api::hd') : '' );
    }


    /**
     * Get a unique identifier for the auth session value.
     *
     * @return string
     */
    public function getTokenName()
    {
        return 'googleauth_' . md5( get_class( $this ) );
    }

    /**
     * If this request is the redirect from a successful authorization grant, store the access token in the session
     * and return a Laravel redirect Response to send the user to their requested page. Otherwise returns null
     * @return Response or null
     */
    public function finishAuthenticationIfRequired()
    {
        if ( isset( $_GET['code'] ) )
        {
            $this->client->authenticate( $_GET['code'] );
            Session::put( $this->getTokenName(), $this->client->getAccessToken() );

            // strip the querystring from the current URL
            $url = rtrim(preg_replace('|&?code=[^&]+|', '', URL::full()), '?');

            return Redirect::to(filter_var($url, FILTER_SANITIZE_URL));
        }
        return null;
    }
    
    /**
     * Create a new instance of the model.
     *
     * @return \Illuminate\Database\Eloquent\Model
     */
    public function createModel()
    {
        $class = '\\'.ltrim( \Config::get('auth.model'), '\\');

        return new $class;
    }

}<commit_msg>Fixed support for custom model but column 'google_id' is required for now.<commit_after><?php namespace Mrynk\L4GoogleApi;

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\UserProviderInterface;
use Illuminate\Support\Facades\App;
use Illuminate\Auth\GenericUser;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\URL;

class GoogleUserProvider implements UserProviderInterface
{

    function __construct()
    {
        $this->client = App::make('google-client');
        $this->oauth2 = new \Google_Service_Oauth2( $this->client );

        if ( Session::has( $this->getTokenName() ) )
        {
            $this->client->setAccessToken( Session::get( $this->getTokenName() ) );
        }
    }

    public function retrieveByToken( $identifier, $token ){}
    public function updateRememberToken( UserInterface $user, $token ){}


    /**
     * Retrieve a user by their unique identifier.
     *
     * @param  mixed $identifier
     * @return \Illuminate\Auth\UserInterface|null
     */
    public function retrieveById( $identifier )
    {
        $user = $this->retrieveByCredentials(array());
        if ($user && $user->getAuthIdentifier() == $identifier) {
            return $user;
        }
    }

    /**
     * Retrieve a user by the given credentials.
     *
     * @param  array $credentials
     * @return \Illuminate\Auth\UserInterface|null
     */
    public function retrieveByCredentials( array $credentials )
    {
        if ($this->client->getAccessToken()) {

            $userinfo = $this->oauth2->userinfo->get();
            $user = $this->createModel()->newQuery()->where( 'google_id', $userinfo->id )->first();
            if( !$user )
            {
                $user = $this->createModel();

                $fillWith = (array) $userinfo;
                $fillWith['google_id'] = $userinfo->id;
                unset( $fillWith['id'] );

                $user->fill( $fillWith )->save();
            }
            return $user;
            //return new GenericUser( (array) $userinfo );
        }
    }

    /**
     * Validate a user against the given credentials.
     *
     * @param  \Illuminate\Auth\UserInterface $user
     * @param  array $credentials
     * @return bool
     */
    public function validateCredentials( UserInterface $user, array $credentials )
    {
        // this method doesn't make sense for Google auth
        return false;
    }

    public function getAuthUrl()
    {
        return $this->client->createAuthUrl() . ( \Config::get('l4-google-api::hd') ? '&hd='.\Config::get('l4-google-api::hd') : '' );
    }


    /**
     * Get a unique identifier for the auth session value.
     *
     * @return string
     */
    public function getTokenName()
    {
        return 'googleauth_' . md5( get_class( $this ) );
    }

    /**
     * If this request is the redirect from a successful authorization grant, store the access token in the session
     * and return a Laravel redirect Response to send the user to their requested page. Otherwise returns null
     * @return Response or null
     */
    public function finishAuthenticationIfRequired()
    {
        if ( isset( $_GET['code'] ) )
        {
            $this->client->authenticate( $_GET['code'] );
            Session::put( $this->getTokenName(), $this->client->getAccessToken() );

            // strip the querystring from the current URL
            $url = rtrim(preg_replace('|&?code=[^&]+|', '', URL::full()), '?');

            return Redirect::to(filter_var($url, FILTER_SANITIZE_URL));
        }
        return null;
    }

    /**
     * Create a new instance of the model.
     *
     * @return \Illuminate\Database\Eloquent\Model
     */
    public function createModel()
    {
        $class = '\\'.ltrim( \Config::get('auth.model'), '\\');

        return new $class;
    }

}
{'fraction_non_alphanumeric': 0.13589810924369747, 'fraction_numerical': 0.001838235294117647, 'mean_word_length': 2.370353982300885, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 62, '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': '3823360', 'n_tokens_mistral': 2178, 'n_tokens_neox': 2182, 'n_words': 523}