Skip to content

Commit b02f287

Browse files
Add a description field for internal reference. (#9)
* Fix issue #8: Add a description field for internal reference. * Switched the description field to string and updated project readme. * Add runtime validation to description --------- Co-authored-by: Ryan Chandler <ryangjchandler@gmail.com>
1 parent f4bfeee commit b02f287

File tree

7 files changed

+69
-4
lines changed

7 files changed

+69
-4
lines changed

README.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Alternatively, you can use the `RyanChandler\Bearer\Facades\Bearer` facade to `g
4646
```php
4747
use RyanChandler\Bearer\Facades\Bearer;
4848

49-
$token = Bearer::generate(domains: [], expiresAt: null);
49+
$token = Bearer::generate(domains: [], expiresAt: null, description: null);
5050
```
5151

5252
By default, Bearer uses time-ordered UUIDs for token strings. You can modify this behaviour by passing a `Closure` to `Bearer::generateTokenUsing`. This function must return a string for storage to the database.
@@ -61,7 +61,7 @@ Bearer::generateTokenUsing(static function (): string {
6161

6262
### Retrieving a `Token` instance
6363

64-
To retreive a `Token` instance from the `token` string, you can use the `RyanChandler\Bearer\Facades\Bearer` facade.
64+
To retrieve a `Token` instance from the `token` string, you can use the `RyanChandler\Bearer\Facades\Bearer` facade.
6565

6666
```php
6767
use RyanChandler\Bearer\Facades\Bearer;
@@ -127,6 +127,21 @@ If you attempt to use this token from any domain other than `https://laravel.com
127127

128128
> **Note**: domain checks include the scheme so be sure to add both cases for HTTP and HTTPS if needed.
129129
130+
### Set a token description
131+
132+
You can optionally set a description for the token.
133+
134+
```php
135+
$token = Bearer::find('my-token-string');
136+
137+
$token->update([
138+
'description' => 'Example description for the token.',
139+
]);
140+
```
141+
142+
> **Note**: The description field accepts a maximum of 255 characters.
143+
144+
130145
## Testing
131146

132147
```bash

database/factories/TokenFactory.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ public function domains($domains)
2929
]);
3030
}
3131

32+
public function description(string $description)
33+
{
34+
return $this->state([
35+
'description' => $description,
36+
]);
37+
}
38+
3239
public function definition()
3340
{
3441
return [

database/migrations/create_bearer_table.php.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ return new class extends Migration
1111
Schema::create('bearer_tokens', function (Blueprint $table) {
1212
$table->id();
1313
$table->string('token')->unique();
14+
$table->string('description')->nullable();
1415
$table->text('domains')->nullable();
1516
$table->datetime('expires_at')->nullable();
1617
$table->timestamps();

src/Bearer.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,18 @@ public function generateTokenUsing(Closure $callback)
3434
return $this;
3535
}
3636

37-
public function generate(array $domains = [], DateTimeInterface $expiresAt = null): Token
37+
public function generate(array $domains = [], ?DateTimeInterface $expiresAt = null, ?string $description = null): Token
3838
{
39+
if ($description !== null && strlen($description) > 255) {
40+
throw new \InvalidArgumentException('Token descriptions must be <= 255 characters.');
41+
}
42+
3943
$callback = $this->generateTokenCallback;
4044

4145
return Token::create([
4246
'token' => $callback(),
4347
'domains' => $domains,
48+
'description' => $description,
4449
'expires_at' => $expiresAt,
4550
]);
4651
}

src/Models/Token.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Token extends Model
1616
protected $table = 'bearer_tokens';
1717

1818
protected $fillable = [
19-
'token', 'domains', 'expires_at',
19+
'token', 'domains', 'description', 'expires_at',
2020
];
2121

2222
protected $casts = [
@@ -55,4 +55,12 @@ public function expires(DateTimeInterface $expiresAt): Token
5555

5656
return $this;
5757
}
58+
59+
public function setDescription(string $description): Token
60+
{
61+
$this->description = $description;
62+
63+
return $this;
64+
}
65+
5866
}

tests/BearerTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,16 @@ public function test_it_can_create_token_with_expires_at()
3838

3939
$this->assertTrue($time->equalTo($token->expires_at));
4040
}
41+
42+
public function test_it_can_create_token_with_description()
43+
{
44+
$description = "Example description for the token.";
45+
46+
$token = Bearer::generate([], null, $description);
47+
48+
$this->assertDatabaseHas('bearer_tokens', [
49+
'token' => $token->token,
50+
'description' => $description,
51+
]);
52+
}
4153
}

tests/TokenTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,21 @@ public function test_it_can_interacts_with_expiration_time_in_months()
117117
$this->assertTrue($token->expired);
118118
Carbon::setTestNow();
119119
}
120+
121+
public function test_it_can_have_description()
122+
{
123+
$token = Token::factory()->create();
124+
125+
$description = 'Example description for the token.';
126+
127+
$token->setDescription($description);
128+
129+
$this->assertSame($description, $token->description);
130+
131+
$newDescription = 'New example description for the token.';
132+
133+
$token->update(['description' => $newDescription]);
134+
135+
$this->assertSame($newDescription, $token->description);
136+
}
120137
}

0 commit comments

Comments
 (0)