# Configuring SwitchAAI Authentication

## Prerequisites

* A MedCo network is up and running, with one or more functional Keycloak within the network.
* One or several identity provider(s) part of the SwitchAAI federation is/are chosen to be used as user source.
* The institution at which the Keycloak of MedCo is deployed is ready to accept being registered as the home organization.&#x20;
* You have access to the [SwitchAAI Resource Registry](https://rr.aai.switch.ch/).

{% hint style="info" %}
Right now the SwitchAAI WAYF (Where Are You From) mechanism is not supported (i.e. the web UI used to select with institution the user wishes to login). This means that you will need to register in Keycloak each identity provider you wish to support.
{% endhint %}

{% hint style="warning" %}
The process described in this guide will need to be repeated for each instance of Keycloak deployed, if there are more than one in the MedCo network.
{% endhint %}

## Configure the identity provider(s) in Keycloak

The following instructions are to be executed on the administration UI of Keycloak, e.g. `https://medco-demo.epfl.ch/auth/admin`.

### Configure the first login flow

The behavior of Keycloak during the very first login of users through the identity provider is highly customisable. We propose below an example of a working flow but this can be changed to fit your need.&#x20;

* Navigate to *Authentication* > *Flows,* select *First Broker Login* and make a *Copy* of it. Name it for example `SwitchAAI-Test Demo IdP First Broker Login`.
* Change the list of executions to make it look like the following image.

![Resulting configuration for the authentication flow.](https://2790528991-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lk4c5V-JXuVKRYCJWrb%2F-LwOjuGLVD6pvgaVHxtN%2F-LwTGxwRLs3_RR1eqjXo%2FScreenshot%20from%202019-12-19%2014-05-45.png?alt=media\&token=fe9976eb-36d1-4e70-a4dd-2ec6bcbc86d4)

### Add the identity provider

* In the *Identity Providers* menu, choose `Add provider...` > `SAML v2.0`
* Specify an *Alias*. Note this will not be changeable later without redoing the whole process. Example: `SwitchAAI-Test`.
* Specify a *Display Name*, which will be displayed to the user in the login page. Example: `SwitchAAI-Test Demo IdP`.
* Specify the *Single Sign-On Service URL* of the identity provider you are linking with. Example: `https://aai-demo-idp.switch.ch/idp/profile/SAML2/POST/SSO`.
* Specify the *First Login Flow* previously configured to use. Example: `SwitchAAI-Test Demo IdP First Broker Login`.
* Toggle to `ON` the following buttons:&#x20;
  * *Enabled*
  * *Trust Email*
  * *HTTP-POST Binding Response*
  * *HTTP-POST Binding for AuthnRequest*
  * *Validate Signature*
* Specify the *NameID Policy Format* as `Persistent`.
* Add the certificate(s) (PEM format, separated by commas if there are several of them) of the identity provider you are linking with in *Validating X509 Certificates*.
* Save the changes.

![Resulting configuration for the identity provider.](https://2790528991-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lk4c5V-JXuVKRYCJWrb%2F-LwOjuGLVD6pvgaVHxtN%2F-LwTHgpJIQnw60-38tcY%2FScreenshot%20from%202019-12-19%2014-12-54.png?alt=media\&token=ca8793e9-e57a-4a82-9d11-6efcbac564d2)

### Add the username mapper

We need to import a unique but intelligible username in Keycloak from the identity provider. For this we use the SwitchAAI mandatory attribute *swissEduPersonUniqueID*.

* Open the *Mappers* tab and click *Create*.
* Fill the field as:
  * *Name:* `SwitchAAI Unique ID`.
  * *Mapper Type:* `Username Template Importer`.
  * *Template:* `${ATTRIBUTE.swissEduPersonUniqueID}`
* Save the changes.

![Resulting configuration for the username mapper.](https://2790528991-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lk4c5V-JXuVKRYCJWrb%2F-LwE8UKCefInJW7B8qf0%2F-LwEcPvDSP31Z6T-bgLM%2FScreenshot%20from%202019-12-16%2017-53-51.png?alt=media\&token=24007dd8-5b91-4dc6-b690-19605ad08b95)

### Setup a certificate

A certificate compliant with the SwitchAAI federation needs to be generated and configured. First[ follow this SwitchAAI guide](https://www.switch.ch/aai/support/certificates/embeddedcerts-requirements-appendix-a/) to generate a self-signed certificate that meets their requirements. You will need from the Keycloak instance:

* Its *FQDN* (fully-qualified domain name). Example: `medco-demo.epfl.ch`.
* Its SAML *entityID*, that you can find out in the XML descriptor from the *Export* tab of the previously configured Keycloak identity provider. Example: `https://medco-demo.epfl.ch/auth/realms/master`.

Once you have generated the certificate, set it up in Keycloak:

* Navigate to the settings page *Realm Settings* > *Keys* > *Providers* and select *Add Keystore...* > *rsa*.
* Specify a name in *Console Display Name*. Example: `rsa-switchaaitest`.
* Specify a *Priority* higher than any other RSA key. Example: `150`.
* In *Private RSA Key* and *X509 Certificate* fields, copy/paste the respective PEM parts of both the private key and the certificate that were previously generated.

![Resulting configuration.](https://2790528991-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lk4c5V-JXuVKRYCJWrb%2F-LwE8UKCefInJW7B8qf0%2F-LwEcDHdm0fXMwgsmcZZ%2FScreenshot%20from%202019-12-16%2017-52-46.png?alt=media\&token=54ebf0a5-fecd-4d61-b0f4-a17e09ed5590)

## Register Keycloak instance as a Service Provider in SwitchAAI

The following instructions are to be executed in the [AAI Resource Registry](https://rr.aai.switch.ch/menu_res_options.php). As a result, a Keycloak instance will be registered as a service provider linked to a home organization in the SwitchAAI federation.

### Register new resource

Click *Add a Resource Description* and fill the 7 categories of information according to the following instructions. Note that if some fields are not listed in this documentation, their value are not important for the registration of the Keycloak instance and can be set according to the explanations provided by the resource registry.

#### 1. Basic Resource Information

* *Entity ID*: the same SAML entityID you used to generate the certificate. Example: `https://medco-demo.epfl.ch/auth/realms/master`.
* *Home Organization*: the organization that hosts the Keycloak instance currently being registered. The responsible persons of the organization specified here will need to approve the registration. This will typically be the the institution where the MedCo node is deployed. For the purpose of our test we are using `AAI Demo Home Organization (aai-demo-idp.switch.ch, AAI Test)`.
* *Home URL*: the address of the MedCo node, at which the UI Glowing Bear can be accessed. Example: `https://medco-demo.epfl.ch/`.

![Example configuration for the basic resource information.](https://2790528991-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lk4c5V-JXuVKRYCJWrb%2F-LwTICXVe0oikBq0ercp%2F-LwTOrUklflRoZUIBGDS%2FScreenshot%20from%202019-12-19%2014-44-26.png?alt=media\&token=249585d2-dcc0-450d-9ae6-171d1af86d24)

#### 2. Descriptive Information

![Example configuration for the descriptive information.](https://2790528991-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lk4c5V-JXuVKRYCJWrb%2F-LwTICXVe0oikBq0ercp%2F-LwTP6I-FgEI3v0tpTmh%2FScreenshot%20from%202019-12-19%2014-45-42.png?alt=media\&token=32361f4b-f245-4e99-990a-9059fbe40355)

#### 3. Contacts

![Example configuration for the contacts.](https://2790528991-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lk4c5V-JXuVKRYCJWrb%2F-LwTICXVe0oikBq0ercp%2F-LwTPNdj7yK1ERMU4qzQ%2FScreenshot%20from%202019-12-19%2014-46-51.png?alt=media\&token=2ffd3455-0d88-476f-88d4-a5fb48575b8d)

#### 4. Service Locations

* *SAML2 HTTP POST binding* (x2): the URL at witch the SwitchAAI infrastructure will communicate with the Keycloak instance. You will find it in the configuration page of the configured identity provider in Keycloak under *Redirect URI*. Example: `https://medco-demo.epfl.ch/auth/realms/master/broker/SwitchAAI-Test/endpoint`

![Example configuration for the service locations.](https://2790528991-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lk4c5V-JXuVKRYCJWrb%2F-LwTICXVe0oikBq0ercp%2F-LwTRi_0J5vQ-r_O5n1M%2FScreenshot%20from%202019-12-19%2014-57-01.png?alt=media\&token=6a6a7b87-0273-4f60-a01c-2dcb07f85d76)

#### 5. Certificates

Copy/paste in this field the PEM part of the certificate that was previously generated. Note that in the example showed below the certificate has already been validated through a separate channel.&#x20;

![Example configuration for the certificates.](https://2790528991-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lk4c5V-JXuVKRYCJWrb%2F-LwTICXVe0oikBq0ercp%2F-LwTST80B5BHxR5j8aBJ%2FScreenshot%20from%202019-12-19%2014-59-45.png?alt=media\&token=15aff96d-3f01-4f17-bfc3-6d5e4b39fc89)

#### 6. Requested Attributes

Put on `Required` at least the following attributes. Note that the release of attributes needs to have a justification.

* *E-mail (email)*. Example reason: `Identify user for being able to assign them specific authorizations.`
* *Unique ID (swissEduPersonUniqueID)*. Example reason: `Get a unique ID of user.`

![Example configuration for the requested attributes.](https://2790528991-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lk4c5V-JXuVKRYCJWrb%2F-LwTICXVe0oikBq0ercp%2F-LwTSmPPosM9KIvBl0hX%2FScreenshot%20from%202019-12-19%2015-01-38.png?alt=media\&token=b329ede1-2bc6-47ae-9896-4c578fbbd6c7)

#### 7. Intended Audience and Interfederation

![Example configuration for the intended audience.](https://2790528991-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lk4c5V-JXuVKRYCJWrb%2F-LwTjxcqe7Jou-I4ItLz%2F-LwTkRuD67qecxRF9zET%2FScreenshot%20from%202019-12-19%2016-22-11.png?alt=media\&token=97b54603-8af6-4f5e-9125-b44f04c105c5)

### Get the new resource approved

Once submitted, the responsible persons from the home organization will need to approve the new resource and validate the fingerprint of the certificate submitted. This is a manual process that will most likely be done through email.&#x20;

Once this is done, the setup should be functional, and the users will be able to select the configured identity provider to login. Don't forget that this covers only users' authentication, their authorization needs to be handled manually through Keycloak after they login at least once.
