======
Tokens
======

Authorization tokens can be generated by users and admins to provide others with access to private/authenticated channels or with a specific set of permissions when using the Package Security Manager CLI or API.

This token can be used in place of username-based authentication, and is particularly useful (and sometimes necessary) for third-party applications or automation tools.

.. _token_types:

Token Types
===========

There are two types of tokens within Package Security Manager:

Resource tokens
---------------

Resource tokens provide the token holder with *read*, *write*, or *manage* permissions for a specific set of channels.

Scope tokens
------------

Scope tokens provide the token holder with a fine-grained set of custom user permissions, limiting the available actions that they are able to perform. A scope combines permission categories with specific actions users can perform into a concatenated string, like this: 

``category:action`` or ``category.subcategory:action``

.. note:: 
   
   Tokens cannot contain both resources and scopes.

   Some actions are also tied to permission levels. For example, if you provide the scope ``channel:edit``, you are also providing ``manage`` level permissions over channels.

.. dropdown:: Available scopes 
   :color: primary
   :animate: fade-in

      .. csv-table:: 
         :file: ../csv/scopes.csv
         :widths: 30, 25, 35
         :header-rows: 1

Generating tokens
=================

.. tab-set:: 

    .. tab-item:: Resource tokens

        To provide token holders with access to a channel:

        #. Log in to Package Security Manager.
        #. Select |token| **Tokens** from the left-hand navigation. 
        #. Click **Generate Token**.
        #. Enter a name for your token. 
        #. Set an expiration date for your token.
        #. Open the **Type** dropdown menu and select **Resources**.
        #. Open the **Channel** dropdown menu and select an available channel to associate with this token. 
        #. Select the level of permissions to associate with this token. 
        #. If necessary, click the **Add Resource** button to include an additional channel with this token, repeat as needed.
        #. Provide a brief description of the token's use. 
        #. Click **Create**. 

            .. thumbnail:: ../img/as_gen_resource_token.png
              :align: center

        #. Save your token string in a secure location. 

            .. caution::

                Generated tokens appear in the upper right corner, and will remain visible until you close the notification, or navigate away from the page. 
                
                Save your token in a secure location! *If you lose your token, there is no way to recover the token string.* You must issue a new token if you lose your current token.

                .. thumbnail:: ../img/generate_token.png
                    :align: center

    .. tab-item:: Scope tokens

        To provide token holders with a custom set of permissions:

        #. Log in to Package Security Manager.
        #. Select |token| **Tokens** from the left-hand navigation.
        #. Click **Generate Token**.
        #. Enter a name for your token. 
        #. Set an expiration date for your token.
        #. Open the **Type** dropdown menu and select **Scopes**.
        #. Open the **Select a scope** dropdown menu and select a scope to add to the token. Repeat as necessary to build a custom set of permissions for your token holders.
        #. Click **Create**.

            .. thumbnail:: ../img/as_gen_scope_token.png
                :align: center

        #. Save your token string in a secure location. 

            .. caution::

                Generated tokens appear at the bottom of the page and will remain visible until you close the notification or navigate away from the page. 
                
                Save your token in a secure location! *If you lose your token, there is no way to recover the token string.* You must issue a new token if you lose your current token.

                .. thumbnail:: ../img/generate_token.png
                    :align: center
    
Editing a token
===============

You can edit a token to change its name or to update its expiration date. You can choose to issue a new token while editing as well, which will invalidate the previous string associated with the token.

To edit a token:

#. Log in to Package Security Manager.
#. Select |token| **Tokens** from the left-hand navigation.
#. Select the token's |pencil| **Edit** icon.
#. Make any necessary changes to the token.
#. If necessary, select the **Issue a new token** checkbox. A warning appears at the top of the window to inform you that you will receive a new token.
#. Click **Edit**. 

Deleting a token
================

To delete a token: 

#. Log in to Package Security Manager.
#. Select |token| **Tokens** from the left-hand navigation. 
#. Select |trash| **Delete Token** beside the token you want to delete.
#. Click **Delete**.

Setting/using a token
=====================

Once you have generated a token, you can share it with other users by any method you wish. 

If you have received a resource token string from another user and you would like to use it to access packages on a channel, add the channel to your ``.condarc`` file by running the following command:

.. code-block:: none

   # Replace <FQDN> with the URL of your Package Security Manager fully qualified domain name
   # Replace <TOKEN> with the generated/shared token string
   # Replace <CHANNEL> with the channel name
   conda config --add channels http(s)://<FQDN>/api/repo/t/<TOKEN>/<CHANNEL>

Managing tokens via the CLI
===========================

For more information on working with tokens using the CLI, see :ref:`Package Security Manager CLI <tokens_cli>`. 

Managing tokens via the API
===========================

Listing available authorization scopes
--------------------------------------

.. code-block:: none

   GET /api/system/tokens


Listing the user authorization tokens
-------------------------------------

.. code-block:: none

   GET /api/account/token


Creating a new user authorization token
---------------------------------------

.. code-block:: none

   POST /api/account/tokens


Removing a user authorization token
-----------------------------------

.. code-block:: none

   DELETE /api/account/tokens/<TOKEN_ID>

.. |pencil| raw:: html

   <i class="fa-light fa-pencil"></i>

.. |trash| raw:: html

   <i class="fa-light fa-trash" style="color: #ff0000"></i>

.. |token| raw:: html

    <svg class="inline" width="1.2em" height="1em" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 576 512"><path d="M0 128C0 92.7 28.7 64 64 64H512c35.3 0 64 28.7 64 64v66c0 7.3-4.9 13.7-12 15.5c-20.7 5.3-36 24.1-36 46.5s15.3 41.2 36 46.5c7.1 1.8 12 8.2 12 15.5v66c0 35.3-28.7 64-64 64H64c-35.3 0-64-28.7-64-64V318c0-7.3 4.9-13.7 12-15.5c20.7-5.3 36-24.1 36-46.5s-15.3-41.2-36-46.5C4.9 207.7 0 201.3 0 194V128zM64 96c-17.7 0-32 14.3-32 32v54.7C60.2 195 80 223.2 80 256s-19.8 61-48 73.3V384c0 17.7 14.3 32 32 32H512c17.7 0 32-14.3 32-32V329.3c-28.2-12.3-48-40.5-48-73.3s19.8-61 48-73.3V128c0-17.7-14.3-32-32-32H64z"/></svg>