# AAI Lab Notes

## Lab goal

Experiment with a mock Authentication and Authorisation Infrastructure (AAI) environment:

- X509 Certificate
- SAML identity provider: SimpleSAMLphp
- OpenID Connect (OIDC) provider: INDIGO IAM
- Service provider: demo client application
- Logs inspection
- Issuring a certificate via OIDC

The lab also introduces AAI security concepts. The supplied IAM instance intentionally contains security flaws for investigation.

### Extended excercise
- Find vulnerabilities in the system

## Lab environment
- INDIGO IAM: <https://iam.osmosis-hosting-operation-hong-kong-1.eu.org>
    - Main IAM
- Logs: <https://logs.osmosis-hosting-operation-hong-kong-1.eu.org/>
    - OpenSearch Dashboard behind a oauth proxy
    - Inspect the logs as you are working throughn the excercises
- Mail catcher:
    - <https://mail.osmosis-hosting-operation-hong-kong-1.eu.org>
    - Check mail sent by the IAM
- SAML IdP: <https://saml.osmosis-hosting-operation-hong-kong-1.eu.org>
    - SimpleSAMLphp
- Demo Client: <https://oidc.osmosis-hosting-operation-hong-kong-1.eu.org/>
- Demo CA: <https://step-ca.osmosis-hosting-operation-hong-kong-1.eu.org:443>

## 0. Examine a x509 certificate
- click on the lock icon next to the url
- bring up certificate information

## 1. Set up INDIGO IAM and inspect SAML

1. Install and open the SAML-tracer browser extension. <https://chromewebstore.google.com/detail/saml-tracer/mpdajninpobndbfcldcmbpnnbhibjmch>
2. Open the application
3. Reserve a lab account from the shared sheet.
4. Sign in to the IAM service at <https://iam..osmosis-hosting-operation-hong-kong-1.eu.org/> using the account details supplied for the lab.
5. Complete the registration form. A dummy email address is acceptable; complete the remaining details sensibly.
6. Allow roughly one to two minutes for approval.

### Inspect the SAML response

Use SAML-tracer to examine the SAML response and identify:

- the SAML provider;
- the `eduPersonUniqueId` attribute and its OID;
- the service provider entity ID.

### Inspect the logs
- Authenticated successfully

### Enable MFA

In INDIGO IAM, choose **Enable MFA**, then copy the string to a TOTP-compatible authenticator (The QR-code is for importing into Google Authenticator etc.), such as TOTP Generator <https://totp.danhersam.com/>.

## 2.1 Use prepared OIDC consumer and inspect the flow

### Inspect the Well-known endpoint
- https://iam.osmosis-hosting-operation-hong-kong-1.eu.org/.well-known/openid-configuration
### Inspect the public key of the token
- https://iam.osmosis-hosting-operation-hong-kong-1.eu.org/jwk

Open <https://oidc.osmosis-hosting-operation-hong-kong-1.eu.org/> focus on section 1 on the page and sign in through the OIDC flow.

Inspect the issued tokens:

- header;
   - encryption and key information;
- payload;
   - encoded information
- signature.

### Token exchange
- hit the exchange token button,
- observe what changed

### Token claims and groups
- Remove the `offline_access` scope, what happened after you remove the scope
- Add yourself to a group, then observe how the ID token changes.

### Requested scopes

Change the requested scopes and observe the result. If the request fails, determine why the client or user is not permitted to obtain the requested scope.

## 3. Create and configure your own OIDC client

<https://iam.osmosis-hosting-operation-hong-kong-1.eu.org>
1. In the IAM dashboard, open **Client**.
2. Select **+ New Client**.
3. Save the client initially, then use its client ID and secret in the demo application.

### Client authentication

Try incorrect client credentials, then sign in. The request fails because a client needs authentication. Without it, anyone who knows an OIDC endpoint could prompt a user for authorisation. We do allow no auth client.

There are also client_credentials auth type which is for machine-to-machine communication but it is considered less secure since it bypass normal user flow like MFA.

### Redirect URI

Enter the correct client ID and secret. Another error occurs, configure an approved redirect URI for the token's intended use:

```text
https://oidc.osmosis-hosting-operation-hong-kong-1.eu.org/callback
```

In the IAM dashboard, open your client settings, add the URI under **Redirect URIs**, and save the client.

### Allowed scopes and refresh tokens

- Try assigning different scope
    - scope are typically a string matcher
    - `storage.read:/` etc. are wlcg scope
    - `storage.read:/aaaa`, Does it work?


## 4. Experiment with WLCG scopes

The client lists the scopes it is allowed to request.

1. Add `storage.read:/` to the client.
2. Request `storage.read:/`.
3. Then request `storage.read:/aaaa`.

Although the latter appears not to be in the literal scope list, it works because INDIGO IAM supports WLCG scopes using regular-expression matching rather than simple string matching.

## 5. Certificate authority (optional)
1. Install step-ca <https://smallstep.com/docs/step-ca/installation/>
2. Initialise the ca config `step ca bootstrap --ca-url https://step-ca.osmosis-hosting-operation-hong-kong-1.eu.org:443 --fingerprint a291c4e0be9396eddcc091b21aabb5510c6b4b8d9e728747a77a470c915dbd0`
    - Please note down the configuration so that you can clean this up afterwards
3. Issue cert and key
`step ca certificate "user@domain.com" cert.crt cert.key   --ca-url https://step-ca.osmosis-hosting-operation-hong-kong-1.eu.org:443   --provisioner="osmosis-iam"`
4. Examine issued cert `openssl x509 -in cert.crt -noout -text`
5. Join and remove yourself from a group to observe how this produce a different certificate


## 6. Security exercises

### Advisory and incident handling

Good code matters, but security incidents still occur. Established processes and communities help coordinate response and can significantly reduce the impact of an incident.
- https://github.com/indigo-iam/iam/releases/tag/v1.14.1

## Learning points

- Work mock AAI infrastructure.
- Examine and experiment with SAML and OIDC flows.
- Inspect tokens, certificates, and MFA flows.
- Inspected logs and audit trail of an IAM.
- Understand client authentication, redirect URIs, scopes, and refresh tokens.
- Recognise that secure engineering also depends on incident-response processes and community support.
