setemares/freee-socialite

Freee OAuth2 Provider for Laravel Socialite

v0.1.2 2019-02-06 04:38 UTC

This package is not auto-updated.

Last update: 2024-11-18 17:37:49 UTC


README

This repository contains Freee provider for Laravel Socialite.

Contents

Installation

$ composer require setemares/freee-socialite
  • You will need to extend Socialite in order to add Freee provider, go to AppServiceProvider.php and add:
use SeteMares\Freee\Provider as FreeeProvider;
  • To the boot() method add:
    $this->bootFreeeSocialite();
  • And create this method:
    private function bootFreeeSocialite()
    {
        $socialite = $this->app->make('Laravel\Socialite\Contracts\Factory');
        $socialite->extend('freee', function ($app) use ($socialite) {
            $config = $app['config']['services.freee'];
            return $socialite->buildProvider(FreeeProvider::class, $config);
        });
    }

Configuration

  • Add to config/services.php:
'freee' => [
    'client_id' => env('FREEE_CLIENT_ID', ''),
    'client_secret' => env('FREEE_CLIENT_SECRET', ''),
    'redirect' => env('FREEE_CALLBACK', 'urn:ietf:wg:oauth:2.0:oob')
]
  • Add config variables to your .env file:
# Freee
FREEE_CLIENT_ID=Your_client_id
FREEE_CLIENT_SECRET=Your_client_secret
FREEE_CALLBACK=Your_callback_url

Usage

  • To get token use as any other socialite provider, e.g.
    public function oauthRedirect()
    {
        return Socialite::driver('freee')
            ->with(['access_type' => 'offline'])
            ->redirect();
    }
    public function oauthCallback()
    {
        try {
            $user = Socialite::driver('freee')
                ->user();
        } catch (\Exception $e) {
            return $this->respondError(Lang::getFromJson("No user in oauth response"), 422);
        }
        // $user contains freee user `id` and `companies` array besides
        // token information (`token`, `expiresIn`, `refreshToken`)
    }
  • To refresh token, call provided method refreshToken($refresh_token)
    try {
        $data = Socialite::driver('freee')->refreshToken($refreshToken);
    } catch (\Exception $e) {
        // GuzzleHttp\Exception\ClientException:
        return $e->getMessage());
    }

Problems

  • Freee will reject issuing access code needed for obtaining access token if there is anything besides urn:ietf:wg:oauth:2.0:oob configured in redirect_uri, having urn:ietf:wg:oauth:2.0:oob in it will present token on screen, requiring user to manually copy the code and paste it into your app to continue the authentication flow.
  • Another, more serious problem is with the ability to refresh token, when access token being issued without problem but upon it's expiration Freee api will refuse to provide new access token with invalid_grant error requiring user to repeat authentication flow.

Credits

License

The MIT License (MIT). Please see License File for more information.