Skip to content

Commit

Permalink
🔖 Release v0.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
exodusanto committed Jan 28, 2020
2 parents a81e096 + 2f92620 commit 6efaf8f
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 0 additions & 1 deletion src/ConciergeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Exodusanto\Concierge;

use Illuminate\Support\ServiceProvider;
use Exodusanto\Concierge\MakeMigrationCommand;

class ConciergeServiceProvider extends ServiceProvider
{
Expand Down
37 changes: 37 additions & 0 deletions src/Contracts/RefreshApiTokenContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
4 changes: 2 additions & 2 deletions src/Http/Middleware/RefreshApiToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
23 changes: 22 additions & 1 deletion src/RefreshApiToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Exodusanto\Concierge;

use Carbon\Carbon;
use Illuminate\Support\Str;

trait RefreshApiToken
Expand All @@ -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
*
Expand All @@ -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
*
Expand All @@ -52,7 +73,7 @@ public function refreshApiToken()
}

/**
* Revoke the api token by assigning null value
* Revoke the api token
*
* @return void
*/
Expand Down
10 changes: 5 additions & 5 deletions tests/CustomUserAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function create_token()

$this->createAndHandleRequest();

$this->assertNotEquals(null, $user->fresh()->token);
$this->assertNotEquals(null, $user->getApiToken());
}

/** @test */
Expand All @@ -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 */
Expand All @@ -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')
Expand Down
20 changes: 11 additions & 9 deletions tests/MiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function create_token()

$this->createAndHandleRequest();

$this->assertNotEquals(null, $user->fresh()->api_token);
$this->assertNotEquals(null, $user->getApiToken());
}

/** @test */
Expand All @@ -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 */
Expand All @@ -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();
Expand All @@ -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 */
Expand All @@ -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 */
Expand All @@ -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')
Expand Down

0 comments on commit 6efaf8f

Please sign in to comment.