You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+32-1Lines changed: 32 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -56,6 +56,33 @@ protected function mapApiRoutes()
56
56
57
57
The middleware will authenticate API requests, ensuring they contain an API key that is valid.
58
58
59
+
### Generating API keys
60
+
61
+
You can generate new API keys by calling the `createApiKey()` method from the `Keyable` trait.
62
+
63
+
When you do so, it returns an instance of `NewApiKey`, which is a simple class the contains the actual `ApiKey` instance that was just created, and also contains the plain text api key, which is the one you should use to authenticate requests.
64
+
65
+
```php
66
+
$newApiKey = $keyable->createApiKey();
67
+
68
+
$newApiKey->plainTextApiKey // This is the key you should use to authenticate requests
69
+
$newApiKey->apiKey // The instance of ApiKey just created
70
+
```
71
+
72
+
You can also manually create API keys without using the `createApiKey` from the `Keyable` trait, in that case, the instance you get back will have a property called `plainTextApikey` populated with the plain text API key.
73
+
74
+
```php
75
+
$myApiKey = ApiKey::create([
76
+
'keyable_id' => $account->getKey(),
77
+
'keyable_type' => Account::class,
78
+
'name' => 'My api key',
79
+
]);
80
+
81
+
$myApiKey->plainTextApikey // Token to be used to authenticate requests
82
+
```
83
+
84
+
Keep in mind `plainTextApikey` will only be populated immediately after creating the key.
85
+
59
86
### Accessing keyable models in your controllers
60
87
The model associated with the key will be attached to the incoming request as ```keyable```:
61
88
@@ -263,14 +290,18 @@ Route::get('/users/{user}/posts/{post}', function (User $user, Post $post) {
ATTENTION: It is highly recommended that you generate a backup of your database before going through the steps below, just to be safe in case something goes wrong.
6
+
7
+
#### Step 1: `api_keys` table updates
8
+
9
+
Implement the following changes on your `api_keys` table.
10
+
11
+
- Add a new nullable string column called `name`.
12
+
- Modify the existing `key` column to increase its length from 40 to 64.
13
+
14
+
#### Step 2: Update the package to version 3.0.0
15
+
16
+
```bash
17
+
composer require givebutter/laravel-keyable:3.0.0
18
+
```
19
+
20
+
#### Step 3. Turn on `compatibility_mode`
21
+
22
+
A new configuration flag was introduced in the `keyable.php` config file on version `3.0.0`, it is called `compatibility_mode`, make sure to publish the package's config file to be able to access it.
23
+
24
+
By default it is set to `false`, but when it is set to `true` the package will handle both hashed and non hashed API keys, which should keep your application running smoothly while you complete all upgrade steps.
25
+
26
+
It is specially useful if you have a very large `api_keys` table, which could take a while to hash all existing API keys.
27
+
28
+
It points to an environment variable called `KEYABLE_COMPATIBILITY_MODE`, but you can update it to whatever you need of course.
29
+
30
+
Make sure to update `KEYABLE_COMPATIBILITY_MODE` to `true` if you want to make use of that feature.
31
+
32
+
#### Step 4. Hash existing API keys
33
+
34
+
A command was added to hash existing API keys that are not currently hashed, it will ensure existing API keys will continue working properly once you finish all upgrade steps.
35
+
36
+
```bash
37
+
php artisan api-key:hash
38
+
```
39
+
40
+
It is also possible to hash a single API key at a time, by passing an `--id` option.
41
+
42
+
```bash
43
+
php artisan api-key:hash --id=API_KEY_ID
44
+
```
45
+
46
+
Be very careful with this option, as each API key should be hashed only once.
47
+
48
+
Ideally you should only use it for testing and on your own API keys.
49
+
50
+
The command tries to avoid hashing an API key twice by comparing the length of the `key` column, if it is already 64 then the command understands the key is already hashed and won't do it again.
51
+
52
+
#### Step 5. Turn off compatibility mode
53
+
54
+
If you are making use of the compatibility mode, it can now be turned off by setting `KEYABLE_COMPATIBILITY_MODE` to `false`, it is not needed anymore.
0 commit comments