Skip to content

Commit 8e0a5dd

Browse files
authored
Add note about how to integrate with silverstripe-apikey
https://github.com/sminnee/silverstripe-apikey/
1 parent cd669b3 commit 8e0a5dd

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,47 @@ fetch('/api/v1/projects/createProject', {
218218
})
219219
```
220220

221+
## Authenication via silverstripe-apikeys
222+
223+
If you would prefer to use API keys rather than JWT tokens, you can use
224+
https://github.com/sminnee/silverstripe-apikey and configure it as a route
225+
specific middleware
226+
227+
```yml
228+
SilverStripe\Core\Injector\Injector:
229+
ApiRouteMiddleware:
230+
class: SilverStripe\Control\Middleware\RequestHandlerMiddlewareAdapter
231+
properties:
232+
RequestHandler: '%$MyProjectApi'
233+
Middlewares:
234+
CustomMiddleware: '%$ApiKeyRequestMiddleware'
235+
MyProjectApi:
236+
class: MyProjectApi
237+
ApiKeyRequestMiddleware:
238+
class: Sminnee\ApiKey\ApiKeyRequestMiddleware
239+
SilverStripe\Control\Director:
240+
rules:
241+
api:
242+
Controller: '%$ApiRouteMiddleware'
243+
244+
```
245+
246+
Out of the box, the silverstripe-apikey module will not throw an error if
247+
no API key is provided (but it will if a wrong one is). So in the short term
248+
you're best to double check and handle if the API key is not provided
249+
250+
```
251+
public function projects()
252+
{
253+
if (!$this->ensureUserLoggedIn()) {
254+
return $this->failure(401);
255+
}
256+
257+
// ..
258+
}
259+
```
260+
261+
221262
## UUIDs
222263
223264
https://stackoverflow.com/questions/56576985/is-it-a-bad-practice-to-expose-the-database-id-to-the-client-in-your-rest-api/56577271

0 commit comments

Comments
 (0)