Skip to content

Commit 03b07e7

Browse files
Improve use cases documentation
1 parent 1285f92 commit 03b07e7

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,23 @@ API key permissions for the [Django REST Framework](https://www.django-rest-fram
2929
- 🔒 **As secure as possible**: API keys are treated with the same level of care than user passwords. They are hashed using the default password hasher before being stored in the database, and only visible at creation.
3030
- 🎨 **Customizable**: satisfy specific business requirements by building your own customized API key models, permission classes and admin panels. (Currently in public beta.)
3131

32-
### Example use cases
32+
### Should I use API keys?
3333

34-
- Using the built-in `APIKey` model, you can generate an API key and embed it in your frontend app server so that only it can access your API.
35-
- By customizing API key models and permissions, you can associate API keys to an entity (e.g. a user, person, organization…), and then build endpoints to allow them to manage their API keys.
34+
There are important security aspects you need to consider before switching to an API key access control scheme. We've listed some of these in [Security caveats](security.md#caveats), including serving your API over HTTPS.
35+
36+
Besides, see [Why and when to use API keys](https://cloud.google.com/endpoints/docs/openapi/when-why-api-key#top_of_page) for hints on whether API keys can fit your use case.
37+
38+
API keys are ideal in the following situations:
39+
40+
- Blocking anonymous traffic.
41+
- Implementing API key-based [throttling](https://www.django-rest-framework.org/api-guide/throttling/). (Note that Django REST Framework already has may built-in utilities for this use case.)
42+
- Identifying usage patterns by logging request information along with the API key.
43+
44+
They can also present enough security for authorizing internal services, such as your API server and an internal frontend application.
45+
46+
> Please note that this package is NOT meant for authentication. You should NOT use this package to identify individual users, either directly or indirectly.
47+
>
48+
> If you need server-to-server authentication, you may want to consider OAuth instead. Libraries such as [django-oauth-toolkit](https://django-oauth-toolkit.readthedocs.io/en/latest/index.html) can help.
3649
3750
## Quickstart
3851

docs/guide.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,9 @@ This package provides various customization APIs that allow you to extend its ba
157157

158158
If the built-in `APIKey` model doesn't fit your needs, you can create your own by subclassing `AbstractAPIKey`. This is particularly useful if you need to **store extra information** or **link API keys to other models** using a `ForeignKey` or a `ManyToManyField`.
159159

160+
!!! warning
161+
Associating API keys to users, directly or indirectly, can present a security risk. See also: [Should I use API keys?](/#should-i-use-api-keys).
162+
160163
#### Example
161164

162165
Here's how you could link API keys to an imaginary `Organization` model:

docs/index.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,24 @@
3535
- 🔒 **As secure as possible**: API keys are treated with the same level of care than user passwords. They are hashed using the default password hasher before being stored in the database, and only visible at creation.
3636
- 🎨 **Customizable**: satisfy specific business requirements by building your own customized API key models, permission classes and admin panels. (Currently in public beta.)
3737

38-
!!! info
39-
There are important security aspects you need to consider before switching to an API key access control scheme. See [Security caveats](security.md#caveats).
38+
### Should I use API keys?
4039

41-
### Example use cases
40+
There are important security aspects you need to consider before switching to an API key access control scheme. We've listed some of these in [Security caveats](security.md#caveats), including serving your API over HTTPS.
4241

43-
- Using the built-in `APIKey` model, you can generate an API key and embed it in your frontend app server so that only it can access your API.
44-
- By customizing API key models and permissions, you can associate API keys to an entity (e.g. a user, person, organization…), and then build endpoints to allow them to manage their API keys.
42+
Besides, see [Why and when to use API keys](https://cloud.google.com/endpoints/docs/openapi/when-why-api-key#top_of_page) for hints on whether API keys can fit your use case.
43+
44+
API keys are ideal in the following situations:
45+
46+
- Blocking anonymous traffic.
47+
- Implementing API key-based [throttling](https://www.django-rest-framework.org/api-guide/throttling/). (Note that Django REST Framework already has may built-in utilities for this use case.)
48+
- Identifying usage patterns by logging request information along with the API key.
49+
50+
They can also present enough security for authorizing internal services, such as your API server and an internal frontend application.
51+
52+
!!! warning
53+
Please note that this package is NOT meant for authentication. You should NOT use this package to identify individual users, either directly or indirectly.
54+
55+
If you need server-to-server authentication, you may want to consider OAuth instead. Libraries such as [django-oauth-toolkit](https://django-oauth-toolkit.readthedocs.io/en/latest/index.html) can help.
4556

4657
## Quickstart
4758

docs/security.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Security
22

3-
## Key generation scheme
3+
## Implementation details
4+
5+
### Key generation scheme
46

57
An API key is composed of two items:
68

@@ -14,7 +16,7 @@ The generated key that clients use to [make authorized requests](#making-authori
1416

1517
[^1]: All hashers provided by Django should be supported. `djangorestframework-api-key` is tested against the [default list of `PASSWORD_HASHERS`](https://docs.djangoproject.com/en/2.2/ref/settings/#std:setting-PASSWORD_HASHERS). See also [How Django stores passwords](https://docs.djangoproject.com/en/2.2/topics/auth/passwords/#how-django-stores-passwords) for more information.
1618

17-
## Grant scheme
19+
### Grant scheme
1820

1921
Access is granted if and only if all of the following is true:
2022

@@ -27,9 +29,11 @@ Access is granted if and only if all of the following is true:
2729

2830
## Caveats
2931

30-
[API keys ≠ Security](https://nordicapis.com/why-api-keys-are-not-enough/): depending on your situation, you should probably not rely on API keys only to authenticate/authorize your clients.
32+
[API keys ≠ Security](https://nordicapis.com/why-api-keys-are-not-enough/): depending on your situation, you should probably not use API keys only to authorize your clients.
33+
34+
Besides, you do NOT recommend using this package for authentication, i.e. retrieving user information from API keys.
3135

32-
**Using API keys shifts the responsability of Information Security on your clients**. This induces risks, especially if detaining an API key gives access to confidential information or write operations. For example, an attacker could impersonate clients if they let their API keys leak.
36+
Inded, **using API keys shifts the responsability of Information Security on your clients**. This induces risks, especially if detaining an API key gives access to confidential information or write operations. For example, an attacker could impersonate clients if they let their API keys leak.
3337

3438
As a best practice, you should apply the _Principle of Least Privilege_: allow only those who require resources to access those specific resources. In other words: **if your client needs to access an endpoint, add API permissions on that endpoint only** instead of the whole API.
3539

0 commit comments

Comments
 (0)