From 7a38e29fa5e71fd41cd3aaf89e7c239874170bcf Mon Sep 17 00:00:00 2001 From: Antonio Dal Sie Date: Tue, 28 Jan 2020 21:41:56 +0100 Subject: [PATCH 1/4] :fire: Remove unused import --- src/ConciergeServiceProvider.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ConciergeServiceProvider.php b/src/ConciergeServiceProvider.php index d2d5f3a..73460bb 100755 --- a/src/ConciergeServiceProvider.php +++ b/src/ConciergeServiceProvider.php @@ -3,7 +3,6 @@ namespace Exodusanto\Concierge; use Illuminate\Support\ServiceProvider; -use Exodusanto\Concierge\MakeMigrationCommand; class ConciergeServiceProvider extends ServiceProvider { From 9b1dddda28e6c51c3386d89d791816675b594a88 Mon Sep 17 00:00:00 2001 From: Antonio Dal Sie Date: Tue, 28 Jan 2020 21:42:26 +0100 Subject: [PATCH 2/4] :sparkles: Add "getApiToken" and "getApiTokenRefreshedAt" methods --- src/Contracts/RefreshApiTokenContract.php | 37 +++++++++++++++++++++++ src/Http/Middleware/RefreshApiToken.php | 4 +-- src/RefreshApiToken.php | 23 +++++++++++++- 3 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/Contracts/RefreshApiTokenContract.php b/src/Contracts/RefreshApiTokenContract.php index 2e4c470..1b52d74 100644 --- a/src/Contracts/RefreshApiTokenContract.php +++ b/src/Contracts/RefreshApiTokenContract.php @@ -4,8 +4,45 @@ interface RefreshApiTokenContract { + /** + * Get the name of the "api_token" column + * + * @return string + */ public function getApiTokenName(); + + /** + * Get the api_token of the user + * + * @return string|null + */ + public function getApiToken(); + + /** + * Get the name of the "api_token_refreshed_at" column + * + * @return string + */ public function getApiTokenRefreshedAtName(); + + /** + * Get the api_token_refreshed_at timestamp + * + * @return Carbon|null + */ + public function getApiTokenRefreshedAt(); + + /** + * Refresh the api token + * + * @return void + */ public function refreshApiToken(); + + /** + * Revoke the api token + * + * @return void + */ public function revokeApiToken(); } diff --git a/src/Http/Middleware/RefreshApiToken.php b/src/Http/Middleware/RefreshApiToken.php index cf3167b..d841f7b 100644 --- a/src/Http/Middleware/RefreshApiToken.php +++ b/src/Http/Middleware/RefreshApiToken.php @@ -56,8 +56,8 @@ protected function getProviderName($model) protected function refreshTokenIfExpired(RefreshApiTokenContract $user, $provider) { if ($timeout = $this->getTimeout($provider)) { - /** @var Carbon $lastRefreshedAt */ - $lastRefreshedAt = $user->{$user->getApiTokenRefreshedAtName()}; + /** @var Carbon|null $lastRefreshedAt */ + $lastRefreshedAt = $user->getApiTokenRefreshedAt(); if (!$lastRefreshedAt || now()->gt($lastRefreshedAt->addSeconds($timeout))) { $user->refreshApiToken(); diff --git a/src/RefreshApiToken.php b/src/RefreshApiToken.php index 312dee9..331e4c3 100644 --- a/src/RefreshApiToken.php +++ b/src/RefreshApiToken.php @@ -2,6 +2,7 @@ namespace Exodusanto\Concierge; +use Carbon\Carbon; use Illuminate\Support\Str; trait RefreshApiToken @@ -16,6 +17,16 @@ public function getApiTokenName() return 'api_token'; } + /** + * Get the api_token of the user + * + * @return string|null + */ + public function getApiToken() + { + return $this->{$this->getApiTokenName()}; + } + /** * Get the name of the "api_token_refreshed_at" column * @@ -26,6 +37,16 @@ public function getApiTokenRefreshedAtName() return 'api_token_refreshed_at'; } + /** + * Get the api_token of the user + * + * @return Carbon|null + */ + public function getApiTokenRefreshedAt() + { + return $this->{$this->getApiTokenRefreshedAtName()}; + } + /** * Generate a new token * @@ -52,7 +73,7 @@ public function refreshApiToken() } /** - * Revoke the api token by assigning null value + * Revoke the api token * * @return void */ From c15e4c2b69cecef30d57ad2f94f702d7af06002c Mon Sep 17 00:00:00 2001 From: Antonio Dal Sie Date: Tue, 28 Jan 2020 21:43:12 +0100 Subject: [PATCH 3/4] :white_check_mark: Update tests using new methods --- tests/CustomUserAttributesTest.php | 10 +++++----- tests/MiddlewareTest.php | 20 +++++++++++--------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/tests/CustomUserAttributesTest.php b/tests/CustomUserAttributesTest.php index 850af89..d20c19e 100644 --- a/tests/CustomUserAttributesTest.php +++ b/tests/CustomUserAttributesTest.php @@ -21,7 +21,7 @@ public function create_token() $this->createAndHandleRequest(); - $this->assertNotEquals(null, $user->fresh()->token); + $this->assertNotEquals(null, $user->getApiToken()); } /** @test */ @@ -31,13 +31,13 @@ public function refresh_token() $user = $this->loginUser(CustomUser::class); $user->refreshApiToken(); - $token = $user->fresh()->token; + $token = $user->getApiToken(); Carbon::setTestNow(now()->addHour(1)); $this->createAndHandleRequest(); - $this->assertNotEquals($token, $user->fresh()->token); + $this->assertNotEquals($token, $user->getApiToken()); } /** @test */ @@ -47,13 +47,13 @@ public function refresh_token_no_timeout_skip() $user = $this->loginUser(CustomUser::class); $user->refreshApiToken(); - $token = $user->fresh()->token; + $token = $user->getApiToken(); Carbon::setTestNow(now()->addMinutes(10)); $this->createAndHandleRequest(); - $this->assertEquals($token, $user->fresh()->token); + $this->assertEquals($token, $user->getApiToken()); } protected function createAndHandleRequest($method = 'GET') diff --git a/tests/MiddlewareTest.php b/tests/MiddlewareTest.php index a96cf22..ccfe29e 100644 --- a/tests/MiddlewareTest.php +++ b/tests/MiddlewareTest.php @@ -19,7 +19,7 @@ public function create_token() $this->createAndHandleRequest(); - $this->assertNotEquals(null, $user->fresh()->api_token); + $this->assertNotEquals(null, $user->getApiToken()); } /** @test */ @@ -29,13 +29,13 @@ public function refresh_token() $user = $this->loginUser(); $user->refreshApiToken(); - $token = $user->fresh()->api_token; + $token = $user->getApiToken(); Carbon::setTestNow(now()->addHour(1)); $this->createAndHandleRequest(); - $this->assertNotEquals($token, $user->fresh()->api_token); + $this->assertNotEquals($token, $user->getApiToken()); } /** @test */ @@ -45,19 +45,21 @@ public function refresh_token_no_timeout_skip() $user = $this->loginUser(); $user->refreshApiToken(); - $token = $user->fresh()->api_token; + $token = $user->getApiToken(); Carbon::setTestNow(now()->addMinutes(10)); $this->createAndHandleRequest(); - $this->assertEquals($token, $user->fresh()->api_token); + $this->assertEquals($token, $user->getApiToken()); } /** @test */ public function user_without_contract_skip() { $this->setConfig(); + + /** @var UserWithoutContract $user */ $user = $this->loginUser(UserWithoutContract::class); $this->createAndHandleRequest(); @@ -73,7 +75,7 @@ public function no_concierge_config_skip() $this->createAndHandleRequest(); - $this->assertEquals(null, $user->fresh()->api_token); + $this->assertEquals(null, $user->getApiToken()); } /** @test */ @@ -84,7 +86,7 @@ public function post_request_skip() $this->createAndHandleRequest('POST'); - $this->assertEquals(null, $user->fresh()->api_token); + $this->assertEquals(null, $user->getApiToken()); } /** @test */ @@ -104,11 +106,11 @@ public function revoke_token() $user->refreshApiToken(); - $this->assertNotNull($user->fresh()->api_token); + $this->assertNotNull($user->getApiToken()); $user->revokeApiToken(); - $this->assertNull($user->fresh()->api_token); + $this->assertNull($user->getApiToken()); } protected function createAndHandleRequest($method = 'GET') From 2f92620132616f9f1be8a69607c52567380fd9de Mon Sep 17 00:00:00 2001 From: Antonio Dal Sie Date: Tue, 28 Jan 2020 21:47:49 +0100 Subject: [PATCH 4/4] :pencil: Update CHANGELOG --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 607be41..ca03c94 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to `laravel-concierge` will be documented in this file +## 0.3.1 - 2020-01-28 + +- add "getApiToken" and "getApiTokenRefreshedAt" methods (9b1dddda28e6c51c3386d89d791816675b594a88) + ## 0.3.0 - 2020-01-28 - refactor trait to handle different column name (dc0c6271e427e2c1702d15be269ab6162f4eb1eb)