Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add refreshTokensExpireIn and personalAccessTokensExpireIn methods #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,14 +172,16 @@ LumenPassport::allowMultipleTokens();

### Different TTLs for different password clients

Laravel Passport allows to set one global TTL for access tokens, but it may be useful sometimes
Laravel Passport allows to set one global TTL for tokens or refresh tokens or personal access tokens, but it may be useful sometimes
to set different TTLs for different clients (eg. mobile users get more time than desktop users).

Simply do the following in your service provider:

```php
// Second parameter is the client Id
LumenPassport::tokensExpireIn(Carbon::now()->addYears(50), 2);
LumenPassport::refreshTokensExpireIn(Carbon::now()->addMinutes(30), 4);
LumenPassport::personalAccessTokensExpireIn(Carbon::now()->addHours(1), 5);
```

If you don't specify client Id, it will simply fall back to Laravel Passport implementation.
Expand Down
62 changes: 60 additions & 2 deletions src/LumenPassport.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ class LumenPassport
*/
public static $tokensExpireAt = [];

/**
* The date when refresh tokens expire.
*
* @var \DateTimeInterface|null
*/
public static $refreshTokensExpireAt;

/**
* The date when personal access tokens expire.
*
* @var \DateTimeInterface|null
*/
public static $personalAccessTokensExpireAt;

/**
* Instruct Passport to keep revoked tokens pruned.
*/
Expand Down Expand Up @@ -56,10 +70,54 @@ public static function tokensExpireIn(DateTimeInterface $date = null, $clientId
return isset(static::$tokensExpireAt[$clientId])
? Carbon::now()->diff(static::$tokensExpireAt[$clientId])
: Passport::tokensExpireIn();
} else {
static::$tokensExpireAt[$clientId] = $date;
}

static::$tokensExpireAt[$clientId] = $date;

return new static;
}

/**
* Get or set when refresh tokens expire.
*
* @param \DateTimeInterface|null $date
* @param int $clientId
* @return \DateInterval|static
*/
public static function refreshTokensExpireIn(DateTimeInterface $date = null, $clientId = null)
{
if (! $clientId) return Passport::refreshTokensExpireIn($date);

if (is_null($date)) {
return isset(static::$refreshTokensExpireAt[$clientId])
? Carbon::now()->diff(static::$refreshTokensExpireAt[$clientId])
: Passport::refreshTokensExpireIn();
}

static::$refreshTokensExpireAt[$clientId] = $date;

return new static;
}

/**
* Get or set when personal access tokens expire.
*
* @param \DateTimeInterface|null $date
* @param int $clientId
* @return \DateInterval|static
*/
public static function personalAccessTokensExpireIn(DateTimeInterface $date = null, $clientId = null)
{
if (! $clientId) return Passport::personalAccessTokensExpireIn($date);

if (is_null($date)) {
return isset(static::$personalAccessTokensExpireAt[$clientId])
? Carbon::now()->diff(static::$personalAccessTokensExpireAt[$clientId])
: Passport::personalAccessTokensExpireIn();
}

static::$personalAccessTokensExpireAt[$clientId] = $date;

return new static;
}

Expand Down
42 changes: 42 additions & 0 deletions tests/LumenPassport/integration.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,46 @@ public function token_ttl_can_be_set_via_lumen_class()
$this->assertTrue(LumenPassport::tokensExpireIn() == Carbon::now()->diff(Carbon::now()->addYears(1)));
$this->assertTrue(Passport::tokensExpireIn() == Carbon::now()->diff(Carbon::now()->addYears(1)));
}

/**
* Test refresh token ttl can be set with lumen-passport class.
*
* @test
*
* @return void
*/
public function refreshToken()
{
// Default (global) client
LumenPassport::refreshTokensExpireIn(Carbon::now()->addYears(1));
$this->assertTrue(Passport::refreshTokensExpireIn() == Carbon::now()->diff(Carbon::now()->addYears(1)));
$this->assertTrue(LumenPassport::refreshTokensExpireIn() == Carbon::now()->diff(Carbon::now()->addYears(1)));

// Specific client
LumenPassport::refreshTokensExpireIn(Carbon::now()->addYears(5), 2);
$this->assertTrue(LumenPassport::refreshTokensExpireIn(null, 2) == Carbon::now()->diff(Carbon::now()->addYears(5)));
$this->assertTrue(LumenPassport::refreshTokensExpireIn() == Carbon::now()->diff(Carbon::now()->addYears(1)));
$this->assertTrue(Passport::refreshTokensExpireIn() == Carbon::now()->diff(Carbon::now()->addYears(1)));
}

/**
* Test personal access tokens ttl can be set with lumen-passport class.
*
* @test
*
* @return void
*/
public function personalAccessTokens()
{
// Default (global) client
LumenPassport::personalAccessTokensExpireIn(Carbon::now()->addYears(1));
$this->assertTrue(Passport::personalAccessTokensExpireIn() == Carbon::now()->diff(Carbon::now()->addYears(1)));
$this->assertTrue(LumenPassport::personalAccessTokensExpireIn() == Carbon::now()->diff(Carbon::now()->addYears(1)));

// Specific client
LumenPassport::personalAccessTokensExpireIn(Carbon::now()->addYears(5), 2);
$this->assertTrue(LumenPassport::personalAccessTokensExpireIn(null, 2) == Carbon::now()->diff(Carbon::now()->addYears(5)));
$this->assertTrue(LumenPassport::personalAccessTokensExpireIn() == Carbon::now()->diff(Carbon::now()->addYears(1)));
$this->assertTrue(Passport::personalAccessTokensExpireIn() == Carbon::now()->diff(Carbon::now()->addYears(1)));
}
}