# What an app is told about the person (App Gateway)

An app behind the [App Gateway](Security-App-Gateway.md) has no login page of its own to rely on: CrushMFT has signed the person in. This page is about what CrushMFT tells the app, and how the app (and its network) makes sure a request really came through CrushMFT.

The settings are in the app's **Identity** and **Destination** parts ([Preferences](Prefs-App-Gateway.md)).

## Header fields

With **Tell the app who the user is, in header fields** on, each forwarded request carries the fields ticked:

| Field | Holds |
|---|---|
| `X-CrushMFT-User` | the user name |
| `X-CrushMFT-User-Id` | the user's id, which never changes (a rename keeps it) |
| `X-CrushMFT-Email` | the email address |
| `X-CrushMFT-Name` | first and last name |
| `X-CrushMFT-Groups` | the user's groups, comma separated |
| `X-CrushMFT-Roles` | the user's roles, comma separated |

The prefix can be changed for apps that expect another name (`X-Remote-` gives `X-Remote-User`). Whatever the browser sent in fields with the prefix, or in any `X-CrushMFT-` field, is removed first, so a person cannot claim to be someone else. CrushMFT also sends `X-Forwarded-For`, `X-Forwarded-Host` and `X-Forwarded-Proto`, and never its own cookies.

Many apps can take the user from such a field ("trusted header" or "reverse proxy" authentication). It is safe only when nothing but CrushMFT can reach the app: anyone who reaches it directly can send the same fields. So close the app to everything else ([below](#the-network)), and add a client certificate or the signed assertion where the app can check one.

## The signed assertion

With **Send a signed assertion** on, each request carries `X-CrushMFT-Assertion`: a JWT signed by CrushMFT with ES256. The app checks the signature with CrushMFT's public keys and refuses a request without a valid one, which proves that the request came through CrushMFT, whatever the network allows.

| Claim | Holds |
|---|---|
| `iss` | the issuer: the sign-in address, or the issuer set on the Identity tab |
| `aud` | the app's id |
| `sub` | the user's id (never the name) |
| `preferred_username`, `name`, `email`, `groups` | the person; `roles` when the app sends the Roles field |
| `sid` | a value that stands for the app session |
| `iat`, `nbf`, `exp` | made now, valid from 5 seconds ago, for 60 seconds |
| `jti` | a unique id |

The keys are published at `/.well-known/crushmft-gateway/jwks.json` on the sign-in address (the Identity tab shows the full address). **New signing key** on the Identity tab makes a new key; the old one stays published for ten minutes, longer than any assertion it signed lives, so an app that fetches the keys never refuses a valid request.

Checking it in Python with PyJWT:

```python
import jwt

keys = jwt.PyJWKClient("https://files.example.com/.well-known/crushmft-gateway/jwks.json")

def person(request):
    token = request.headers["X-CrushMFT-Assertion"]
    key = keys.get_signing_key_from_jwt(token)
    return jwt.decode(token, key.key, algorithms=["ES256"], audience="wiki", issuer="https://files.example.com")
```

In Node.js with `jose`:

```js
import { createRemoteJWKSet, jwtVerify } from "jose";

const keys = createRemoteJWKSet(new URL("https://files.example.com/.well-known/crushmft-gateway/jwks.json"));

export async function person(req) {
  const { payload } = await jwtVerify(req.headers["x-crushmft-assertion"], keys, { issuer: "https://files.example.com", audience: "wiki", algorithms: ["ES256"] });
  return payload;
}
```

## A Basic login

An old app that knows nothing but `Authorization: Basic` can be given one service account (**Basic login**, **Set…**). CrushMFT signs every request in as that account, and removes whatever `Authorization` the browser sent. The password is kept sealed with the server's other secrets and is never shown again. The app then sees every person as the same account: use it for apps where that is enough, or together with the header fields.

## A client certificate

Under **Client certificate** (Destination), pick a certificate with its key from the [certificate store](Prefs-Certificates.md). CrushMFT shows it when the inside site asks for one, and the site refuses every connection without it, before the app sees a byte. A private authority for this is quickest made in the certificate store; give the inside site that authority's certificate.

nginx:

```nginx
server {
    listen 8443 ssl;
    ssl_certificate         /etc/nginx/wiki.crt;
    ssl_certificate_key     /etc/nginx/wiki.key;
    ssl_client_certificate  /etc/nginx/crushmft-gateway-ca.crt;
    ssl_verify_client       on;
    location / { proxy_pass http://127.0.0.1:8080; }
}
```

Apache httpd:

```apache
<VirtualHost *:8443>
    SSLEngine on
    SSLCertificateFile    /etc/httpd/wiki.crt
    SSLCertificateKeyFile /etc/httpd/wiki.key
    SSLCACertificateFile  /etc/httpd/crushmft-gateway-ca.crt
    SSLVerifyClient require
    SSLVerifyDepth  2
</VirtualHost>
```

Then set the app's inside address to `https://…:8443/`. Its own certificate is checked against the trusted authorities and its name, or pinned the first time (**TLS to it**). **Test from every server** shows whether the client certificate was shown and accepted. With TLS 1.3, a site that wants a certificate CrushMFT does not show refuses only after the handshake: people then see "… did not answer".

## The network

The inside site should take connections only from the CrushMFT server(s) that forward to it. In these examples CrushMFT is 10.0.1.20 and the app listens on port 8080.

Linux, nftables:

```
table inet wiki {
    chain input {
        type filter hook input priority 0; policy accept;
        tcp dport 8080 ip saddr 10.0.1.20 accept
        tcp dport 8080 drop
    }
}
```

Windows Firewall (PowerShell):

```powershell
New-NetFirewallRule -DisplayName "Wiki only from CrushMFT" -Direction Inbound -Protocol TCP -LocalPort 8080 -RemoteAddress 10.0.1.20 -Action Allow
```

and no other rule that allows port 8080 (Windows refuses what no rule allows).

A cloud security group: one inbound rule for TCP 8080 whose source is the CrushMFT server's address or its own security group, and nothing else for that port.

With a cluster, allow every CrushMFT server of it. A DMZ server must not be allowed: **Test from every server** asks each DMZ whether it can reach the inside address, and shows in red when it can.

## Signing in with OpenID Connect or SAML instead

An app that can sign people in with OpenID Connect or SAML can use CrushMFT for that instead of (or besides) the header fields: see [CrushMFT as an OpenID Connect provider](Security-App-Gateway-OIDC.md) and [CrushMFT as a SAML identity provider](Security-App-Gateway-SAML.md). The app then keeps its own session after the sign-in, and the gateway passes its cookies through.
