Usa le chiavi API con ambito limitato

Le chiavi API ci consentono di integrare Discourse con altri sistemi o automatizzare diverse azioni. Tuttavia, un grande potere comporta una grande responsabilità. Se un attore malevolo ha accesso a una chiave API, potrebbe accedere a dati sensibili o apportare modifiche al tuo sito. Per mitigare questo rischio e aggiungere un ulteriore livello di sicurezza, ora puoi limitare le azioni consentite a una chiave API utilizzando gli scope.

Scope e parametri consentiti

Quando crei una nuova chiave API, puoi selezionare gli scope desiderati. Se passi il mouse sull’icona :grey_question:, vedrai una breve descrizione delle sue funzioni. Il pulsante :link: ti mostrerà gli URL associati a quello scope.

Facoltativamente, puoi anche specificare quali parametri sono accettati. Utilizza le virgole per separare più valori.

Migrazione delle chiavi esistenti

Quando abbiamo aggiunto questa funzionalità, le chiavi esistenti sono diventate chiavi “globali”. Per migrarle agli scope, dovrai revocarle e crearne di nuove.

Aggiunta di scope personalizzati

I plugin possono aggiungere scope personalizzati chiamando il metodo add_api_key_scope:

resource è un simbolo utilizzato per raggruppare scope correlati, mentre action è un hash con i seguenti attributi:

  • actions: Un elenco contenente le azioni del controller consentite. Il formato è controller_name#method_name.
  • methods: Un elenco di metodi HTTP da consentire (ad esempio %i[get]). Utilizzalo al posto di actions per abbinare in base al metodo HTTP piuttosto che all’azione del controller.
  • params: Un elenco contenente i nomi dei parametri consentiti.
  • aliases: Un hash contenente un nome diverso per un parametro consentito.

Se vuoi sapere come sono definiti gli scope di base, consulta:

24 Mi Piace

It seems I get 403 response for getting categories with a read only API key

curl -X GET "https://mysite/categories.json" \
-H "Api-Key: mykey" \
-H "Api-Username: system"

where I have read & read_list permissions on topics for the API key. /top.json works with the same key for instance. The endpoint works when I use a ‘Global Key’

I’d like to put the API key in my client for reading the list of categories and topics, so important to have a read only key!

Any pointers?

1 Mi Piace

Interestingly it works if no credentials are used at all!

k
i.e. curl -X GET "https://mysite/categories.json"

1 Mi Piace

Unfortunately, we only include a handful of scopes out of the box due to the high number of available endpoints. We may add new ones in the future, but in the meantime, you’ll have to extend them to suit your needs.

If you use a scoped API Key, calling endpoints not included by the scopes you choose won’t work. You can click the :link: button to see which URLs are accepted.

This only works for public sites. Also, you won’t see things like private categories unless you are logged in and have enough permissions.

6 Mi Piace

Are you taking suggestions for further scopes?

One thing I’d be interested in using our site’s API for would be for another site to check if a user exists with an email address and add to a group if so. I trust the site (another site within our parent organisation) but tightening down access options to only what’s needed seems wise if it were an option.

1 Mi Piace

I’m super happy that they have opened up an api for plugins. You can start with a plugin and see if it can be incorporated in core.

2 Mi Piace

I believe that pr-welcome for any generally useful scopes you can come up with. E.g. group_membership.edit ?

5 Mi Piace