diff --git a/src/Connection.php b/src/Connection.php index 678ec06..754ab96 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -1,28 +1,29 @@ authenticated(); } @@ -33,12 +34,13 @@ public function authenticated($integration) * @return self * */ - public function withOptions($options) - { + public function withOptions($options) + { $this->options = $options; $this->provider = new GenericProvider($options); + return $this; - } + } /** * Handle dynamic method calls into the model. Forward calls to the provider @@ -51,5 +53,4 @@ public function __call($method, $parameters) { return $this->forwardCallTo($this->provider, $method, $parameters); } - -} \ No newline at end of file +} diff --git a/src/Contracts/Connection.php b/src/Contracts/Connection.php index 0a550dc..2cf7a2a 100644 --- a/src/Contracts/Connection.php +++ b/src/Contracts/Connection.php @@ -4,7 +4,4 @@ interface Connection { - - - } diff --git a/src/Contracts/Token.php b/src/Contracts/Token.php index 0da3fe5..5660357 100644 --- a/src/Contracts/Token.php +++ b/src/Contracts/Token.php @@ -1,25 +1,23 @@ authenticated($integration)){ + if ($connection->authenticated($integration)) { throw new AlreadyAuthenticatedException($integration); } // If not then we need to ask for a token $config = config($integration); - if($config == []){ + if ($config == []) { throw new ConfigDoesntExistException($integration); } @@ -40,16 +37,16 @@ public function create(Connection $connection, $integration) return redirect()->away($url); } - public function store(OAuth2Validation $request, Connection $connection, $integration) + public function store(OAuth2Validation $request, Connection $connection, $integration) { // Are we already authenticated? - if($connection->authenticated($integration)){ + if ($connection->authenticated($integration)) { throw new AlreadyAuthenticatedException($integration); } // If not then to prcess the token and save the access token $config = config($integration); - if($config == []){ + if ($config == []) { throw new ConfigDoesntExistException($integration); } @@ -58,12 +55,13 @@ public function store(OAuth2Validation $request, Connection $connection, $integr try { // Try to get an access token using the authorization code grant. $accessToken = $connection->getAccessToken('authorization_code', [ - 'code' => $request->code + 'code' => $request->code, ]); $token = new $config['tokenProcessor']($accessToken, $integration); Cookie::forget('oauth2state'); + return redirect($config['authorisedRedirect']); } catch (IdentityProviderException $e) { return redirect($config['failedRedirect'], ['error' => $e->getMessage()]); diff --git a/src/Integration.php b/src/Integration.php index ac17f09..2e5aebc 100644 --- a/src/Integration.php +++ b/src/Integration.php @@ -5,13 +5,13 @@ class Integration implements Model { - /** + /** * The attributes that are mass assignable. * * @var array */ protected $fillable = [ - 'name', 'accessToken', 'refreshToken', 'expires', 'additional' + 'name', 'accessToken', 'refreshToken', 'expires', 'additional', ]; /** @@ -31,7 +31,4 @@ class Integration implements Model protected $casts = [ 'additional' => 'array', ]; - - - -} \ No newline at end of file +} diff --git a/src/Providers/OAuth2ServiceProvider.php b/src/Providers/OAuth2ServiceProvider.php index 7455579..d239820 100644 --- a/src/Providers/OAuth2ServiceProvider.php +++ b/src/Providers/OAuth2ServiceProvider.php @@ -6,7 +6,7 @@ class OAuth2ServiceProvider extends ServiceProvider { - public function boot() + public function boot() { $this->loadRoutesFrom(__DIR__.'/../../routes/web.php'); diff --git a/src/Support/AuthorisationProcessor.php b/src/Support/AuthorisationProcessor.php index dc206f3..6a02688 100644 --- a/src/Support/AuthorisationProcessor.php +++ b/src/Support/AuthorisationProcessor.php @@ -4,11 +4,11 @@ class AuthorisationProcessor { - public function __construct($accessToken, $integration) + public function __construct($accessToken, $integration) { - $config = config($integration); - $token = $config['tokenModel']; + $config = config($integration); + $token = $config['tokenModel']; + return (new $token($integration))->updateAccessToken($accessToken); } - -} \ No newline at end of file +} diff --git a/src/Support/Providers/GenericProvider.php b/src/Support/Providers/GenericProvider.php index 6bc982f..c982a4c 100644 --- a/src/Support/Providers/GenericProvider.php +++ b/src/Support/Providers/GenericProvider.php @@ -5,5 +5,4 @@ class GenericProvider extends LeagueGenericProvider { - -} \ No newline at end of file +} diff --git a/src/Support/Token/Base.php b/src/Support/Token/Base.php index 4378c79..8f385c5 100644 --- a/src/Support/Token/Base.php +++ b/src/Support/Token/Base.php @@ -6,83 +6,88 @@ abstract class Base implements Token { + protected $accessToken; + protected $refreshToken; + protected $expires; + protected $integration; + + public function set($options) + { + foreach ($options as $key => $item) { + $this->$key = $item; + } + + return $this; + } + + public function setAccessToken($token) + { + $this->accessToken = $token; + + return $this; + } + + public function accessToken() + { + return $this->accessToken; + } + + public function setRefreshToken($token) + { + $this->refreshToken = $token; + + return $this; + } + + public function refreshToken() + { + return $this->refreshToken; + } + + public function setExpires($timeStamp) + { + $this->expires = $timeStamp; - protected $accessToken; - protected $refreshToken; - protected $expires; - protected $integration; - - public function set($options){ - foreach($options as $key => $item) - { - $this->$key = $item; - } - return $this; - } - - public function setAccessToken($token) - { - $this->accessToken = $token; - return $this; - } - - public function accessToken() - { - return $this->accessToken; - } - - public function setRefreshToken($token) - { - $this->refreshToken = $token; - return $this; - } - - public function refreshToken() - { - return $this->refreshToken; - } - - public function setExpires($timeStamp) - { - $this->expires = $timeStamp; - return $this; - } - - public function expires() - { - return $this->expires; - } - - public function hasExpired() - { - return time() > $this->expires; - } - - public function authenticated() - { - return $this->accessToken != null; - } - - public function renewToken() - { - if($this->hasExpired()){ - $config = config($this->integration); - $provider = Connection::withOptions(array_merge(['redirectUri' => route('oauth2.callback', ['integration' => $this->integration])], $config['oauth2'])); - $accessToken = $provider->getAccessToken('refresh_token', [ - 'refresh_token' => $this->refreshToken() - ]); - $this->updateAccessToken($accessToken); - } - return $this; - } - - public function updateAccessToken($accessToken){ - $this->set([ - 'accessToken' => $accessToken->getToken(), - 'refreshToken' => $accessToken->getRefreshToken(), - 'expires' => $accessToken->getExpires(), - ])->save(); return $this; - } + } + + public function expires() + { + return $this->expires; + } + + public function hasExpired() + { + return time() > $this->expires; + } + + public function authenticated() + { + return $this->accessToken != null; + } + + public function renewToken() + { + if ($this->hasExpired()) { + $config = config($this->integration); + $provider = Connection::withOptions(array_merge(['redirectUri' => route('oauth2.callback', ['integration' => $this->integration])], $config['oauth2'])); + $accessToken = $provider->getAccessToken('refresh_token', [ + 'refresh_token' => $this->refreshToken(), + ]); + $this->updateAccessToken($accessToken); + } -} \ No newline at end of file + return $this; + } + + public function updateAccessToken($accessToken) + { + $this->set([ + 'accessToken' => $accessToken->getToken(), + 'refreshToken' => $accessToken->getRefreshToken(), + 'expires' => $accessToken->getExpires(), + ])->save(); + + return $this; + } +} diff --git a/src/Support/Token/DB.php b/src/Support/Token/DB.php index 739adfc..2dbeb6f 100644 --- a/src/Support/Token/DB.php +++ b/src/Support/Token/DB.php @@ -1,48 +1,49 @@ model = Integration::where('name', $integration)->firstOrNew(); - $this->setFromModel($this->model); - return $this; - } - - public function setFromModel($model) - { - $this->setAccessToken($model->accessToken); - $this->setRereshToken($model->refreshToken); - $this->setExpires($model->expires); - $this->setAdditional($model->additional); - return $this; - } - - public function setAdditional(array $additional) - { - $this->additional = $additional; - return $this; - } - - public function additional() - { - return $this->additional; - } - - public function save() - { - $this->model->accessToken = $this->accessToken(); - $this->model->refreshToken = $this->refreshToken(); - $this->model->expires = $this->expires(); - $this->model->additional = $this->additional(); - $this->model->save(); - return $this; - } - -} \ No newline at end of file + protected $model; + protected $additional; + + public function __construct($integration) + { + $this->model = Integration::where('name', $integration)->firstOrNew(); + $this->setFromModel($this->model); + + return $this; + } + + public function setFromModel($model) + { + $this->setAccessToken($model->accessToken); + $this->setRereshToken($model->refreshToken); + $this->setExpires($model->expires); + $this->setAdditional($model->additional); + + return $this; + } + + public function setAdditional(array $additional) + { + $this->additional = $additional; + + return $this; + } + + public function additional() + { + return $this->additional; + } + + public function save() + { + $this->model->accessToken = $this->accessToken(); + $this->model->refreshToken = $this->refreshToken(); + $this->model->expires = $this->expires(); + $this->model->additional = $this->additional(); + $this->model->save(); + + return $this; + } +} diff --git a/src/Support/Token/File.php b/src/Support/Token/File.php index 6488308..65e889c 100644 --- a/src/Support/Token/File.php +++ b/src/Support/Token/File.php @@ -2,47 +2,47 @@ namespace MacsiDigital\OAuth2\Support\Token; use Illuminate\Support\Facades\Storage; -use MacsiDigital\OAuth2\Support\Token\Base; class File extends Base { - protected $disk; - - public function __construct($integration) - { - if(Storage::disk('local')->exists('oauth2/'.$integration.'.php')){ - $config = include(storage_path('app/oauth2/').$integration.'.php'); - } else { - $config = []; - } - $this->set($config); - $this->integration = $integration; - $this->disk = Storage::disk('local'); - return $this; - } - - public function save() - { - if(!$this->disk->exists('oauth2')){ - $this->disk->makeDirectory('oauth2'); - } - $this->disk->put('/oauth2/'.$this->integration.'.php', $this->generateContent()); - return $this; - } - - public function delete() - { - $this->disk->delete('/oauth2/'.$this->integration.'.php'); - } - - public function generateContent() - { - return "exists('oauth2/'.$integration.'.php')) { + $config = include(storage_path('app/oauth2/').$integration.'.php'); + } else { + $config = []; + } + $this->set($config); + $this->integration = $integration; + $this->disk = Storage::disk('local'); + + return $this; + } + + public function save() + { + if (! $this->disk->exists('oauth2')) { + $this->disk->makeDirectory('oauth2'); + } + $this->disk->put('/oauth2/'.$this->integration.'.php', $this->generateContent()); + + return $this; + } + + public function delete() + { + $this->disk->delete('/oauth2/'.$this->integration.'.php'); + } + + public function generateContent() + { + return " '".$this->accessToken."', 'refreshToken' => '".$this->refreshToken."', 'expires' => '".$this->expires."', ];"; - } - -} \ No newline at end of file + } +} diff --git a/src/Traits/ForwardsCalls.php b/src/Traits/ForwardsCalls.php index ad9b926..2fd83cd 100644 --- a/src/Traits/ForwardsCalls.php +++ b/src/Traits/ForwardsCalls.php @@ -48,8 +48,9 @@ protected function forwardCallTo($object, $method, $parameters) protected static function throwBadMethodCallException($method) { throw new BadMethodCallException(sprintf( - 'Call to undefined method %s::%s()', static::class, $method + 'Call to undefined method %s::%s()', + static::class, + $method )); } - } diff --git a/tests/Model/User.php b/tests/Model/User.php index 963f13e..d7f4e83 100644 --- a/tests/Model/User.php +++ b/tests/Model/User.php @@ -6,5 +6,4 @@ class User extends Model { - } diff --git a/tests/Unit/ModelTest.php b/tests/Unit/ModelTest.php index 389806a..80cd638 100644 --- a/tests/Unit/ModelTest.php +++ b/tests/Unit/ModelTest.php @@ -9,6 +9,5 @@ class ModelTest extends TestCase /** @test */ public function a_model_can_be_instantiated() { - } }