========================
Google IAM setup example
========================

In addition to providing out-of-the-box support for LDAP, Active Directory, SAML and Kerberos, Data Science & AI Workbench also enables you to 
configure the platform to use other external identity providers to authenticate users. If your enterprise uses Google's Cloud IAM (Identity and Access Management) to manage access to Google Cloud Platform (GCP) resources, for example, 
you can use the following process to configure the platform to use Cloud IAM as your identity provider. This will allow users to log in to the platform using their Google (or G-Suite) credentials.

**Before you begin:** 

* You'll need to configure a `Google Cloud project <https://console.cloud.google.com/getting-started>`_ on GCP.
* You'll need to :ref:`enable the Google+ API <enable-google>` for the project.
* You'll need to :ref:`create the credentials <create-credentials>` to use to authorize the platform to connect to Google IAM.

--------------

.. _enable-google:

Enabling the Google+ API
========================

With your project selected *in Google Cloud Platform*:

#. Select **APIs & Services** from the menu on the left.
#. Select **ENABLE APIs AND SERVICES**, then locate and select the Google+ API card in the API library.

   .. thumbnail:: ../../images/google-api.png
      :align: center

#. Click **ENABLE**.

.. thumbnail:: ../../images/enable-google.png
   :align: center

Now you can create credentials for the platform to access your Google Cloud project.

--------------

.. _create-credentials:

Creating Google+ credentials
============================

With your project selected *in Google Cloud Platform*:

#. Select **APIs & Services > Credentials** from the menu on the left.
#. Click **Create credentials** and select **Help me choose** from the drop-down menu.

   .. note::
      If you haven't already, be sure to :ref:`enable the Google+ API<enable-google>` before proceeding.

   .. thumbnail:: ../../images/create-credentials2.png
      :align: center

#. Select **Google+ API** from the API drop-down list, **Web server** from the next drop-down, and **User data** for the last question. 

   .. thumbnail:: ../../images/add-credentials2.png
      :align: center

#. Click **What credentials do I need?** to create the appropriate credentials for the platform.

   .. thumbnail:: ../../images/add-credentials3.png
      :align: center

#. Enter a meaningful name, such as ``Data Science & AI Workbench``, to identify the platform (and help differentiate it from any other web applications you may have configured to use Google IAM).
#. In the **Authorized JavaScript origins** field, provide the FQDN of the Workbench server instance.
#. *Open the Workbench Auth Center* (:ref:`see instructions below<configure-google>`), and copy and paste the value from the **Redirect URI** field into the **Authorized redirect URIs** field here.

   .. note::
      If the domain is not an authorized domain, you'll see an ``Invalid Redirect`` error, and be prompted to add it to the authorized domains list before proceeding.

#. Click **Create OAuth client ID**.
#. On the **OAuth consent screen** tab:

   * Set the **Application type** to **Public**.
   * Set the **Application name** to ``Data Science & AI Workbench`` (or something else meaningful to platform users).
   * Optionally, upload a logo to help users recognize Workbench.
   * Provide a **Support email** address for users to reach out for help. 
   * Provide the full path to the authorized homepage where users will access Anaconda Enteprise.
   * Optionally provide authorized links to a your organization's privacy policy and terms of service.

#. Click **Create** to display the OAuth client credentials that you'll need to copy and paste into Workbench, to enable the platform to authenticate with Google. (See Step 5 below.)

--------------

.. _configure-google:

Configuring Google to be your identity provider
===============================================

Now that you've configured your GCP project to work with Workbench, you need to use the Workbench Administrative Console's Authentication Center to configure Google as your external identity provider:

#. Open the |circle-user| **My Account** dropdown menu and select *Admin Console*.
#. Select |link-out| **Manage Users** to access the Keycloak user interface (UI). 
#. Log in using your Keycloak admin account credentials.
#. Select **Identity Providers** from the left-hand navigation, then select ``Google`` from the **Add provider** dropdown.
#. The **Settings** tab displays the **Redirect URI** you need to copy to the Google Cloud project's configuration. The Redirect URI will looking similar to this: ``https://<full-qualified-domain-name>/auth/realms/AnacondaPlatform/broker/google/endpoint``.
#. Copy and paste the credentials from GCP (Step 9 above) into the **Client ID** and **Client Secret** fields, and click **Save**. 

Now that you've completed the configuration, the login screen will include a Google login option. 

.. note::
    
    When users choose this option and log in to the platform, they'll be automatically added as new Workbench users. As an Administrator, you can then configure their group assignments and role mappings. For more information, see :doc:`groups`.

.. |circle-user| raw:: html
   
   <svg height="1em" width="1em" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M256 496c-54 0-103.9-17.9-144-48v0c0-61.9 50.1-112 112-112h64c61.9 0 112 50.1 112 112v0c5.3-4 10.4-8.2 15.4-12.6C409.1 370.6 354.5 320 288 320H224c-66.5 0-121.1 50.6-127.4 115.4C47.2 391.5 16 327.4 16 256C16 123.5 123.5 16 256 16s240 107.5 240 240s-107.5 240-240 240zm0 16A256 256 0 1 0 256 0a256 256 0 1 0 0 512zm80-304a80 80 0 1 0 -160 0 80 80 0 1 0 160 0zm-80-64a64 64 0 1 1 0 128 64 64 0 1 1 0-128z"/></svg>

.. |link-out| raw:: html

    <svg height="1em" width="1em" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path d="M320 33c0-18.2 14.8-33 33-33H480c17.7 0 32 14.3 32 32V159c0 18.2-14.8 33-33 33c-8.7 0-17.1-3.5-23.3-9.7L404 130.6 235.3 299.3c-6.2 6.2-16.4 6.2-22.6 0s-6.2-16.4 0-22.6L381.4 108 329.7 56.3C323.5 50.1 320 41.7 320 33zm33-1c-.5 0-1 .4-1 1c0 .3 .1 .5 .3 .7L478.3 159.7c.2 .2 .4 .3 .7 .3c.5 0 1-.4 1-1V32H353zM0 96C0 60.7 28.7 32 64 32H208c8.8 0 16 7.2 16 16s-7.2 16-16 16H64C46.3 64 32 78.3 32 96V448c0 17.7 14.3 32 32 32H416c17.7 0 32-14.3 32-32V304c0-8.8 7.2-16 16-16s16 7.2 16 16V448c0 35.3-28.7 64-64 64H64c-35.3 0-64-28.7-64-64V96z"/></svg>
