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

How to list all available api keys from account. #197

Closed
MatthiasMuller opened this issue Mar 8, 2022 · 1 comment
Closed

How to list all available api keys from account. #197

MatthiasMuller opened this issue Mar 8, 2022 · 1 comment
Labels
question Further information is requested

Comments

@MatthiasMuller
Copy link

Hey! Im currently using this amazing library to store API Keys for users, each API Key is used for some services in my API.

Each User is connected to a APIKey.

class UserApiKey(AbstractAPIKey):
    """
    APIKey model
    """

    user = models.ForeignKey(User, on_delete=models.DO_NOTHING,
                             related_name='api_keys',
                             verbose_name='User')

As a user, I want to retrieve all available API Keys, is there any way to fetch them all? My problem is that API Keys are hashed, and I want to list all client available API Keys.

@florimondmanca florimondmanca added the question Further information is requested label Mar 12, 2022
@florimondmanca
Copy link
Owner

florimondmanca commented Mar 12, 2022

Hi @MatthiasMuller,

This library was actually designed to (mostly) prevent the server from reading API keys in cleartext once they've been generated. (I write mostly because the server can theoretically access them on a per-request basis in views, see #98.) I wasn't expecting that users of this library use it to manage API keys, although that makes total sense now. My original use case was "internal API keys", e.g. between an internal frontend app and the Django API server. So there's still some work required to make it easier to manage "external API keys"… There are a couple of other issues around here that sort of prompt this too.

If your use case is something like "show available API keys to clients" and you don't actually need to get the actual API key, would the API key prefixes be sufficient?

api_keys = UserApiKey.objects.all()
prefixes = [api_key.prefix for api_key in api_keys]  # ['PdIgApov', ...]

This is actually what the default API key admin displays, see:

class APIKeyModelAdmin(admin.ModelAdmin):
model: typing.Type[AbstractAPIKey]
list_display = (
"prefix",
"name",
"created",
"expiry_date",
"_has_expired",
"revoked",
)

@MatthiasMuller MatthiasMuller changed the title How to list al available api keys from account. How to list all available api keys from account. May 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants