# Identity headers

Source: https://tiltas.openpanel.sh/docs/services/identity-headers

> Let the program behind a Web Service know which Device, and which person, opened it.

With **Tell the program who is asking** on, a Web Service passes each request to its program with headers that say who
made it. The program can then sign people in without a password of its own, or keep its own accounts and use the
headers to match them.

Turn it on in the Service's form (**Services** → the Service → **Edit**), or send `"identity_headers": true` in the
[API](/docs/api/services/updateService). It exists for Web Services only.

## The headers

| Header | Sent for | Example |
|---|---|---|
| `Tiltas-Device` | every request | `macbook` |
| `Tiltas-Device-Id` | every request | `dev_3n7q` |
| `Tiltas-Member-Id` | a Personal device | `mem_jonas` |
| `Tiltas-Email` | a Personal device | `jonas@example.org` |
| `Tiltas-Name` | a Personal device, URL-encoded | `Jonas%20%C5%BDukas` |

A request from a Server carries only the two Device headers: a Server belongs to the Account, not to a person. Decode
`Tiltas-Name` before you show it; it is URL-encoded so names in any language survive the trip.

Match people by `Tiltas-Member-Id`, which never changes. An email address can change, and a Device can be renamed.

## They cannot be forged

The hosting Device removes every `Tiltas-` header the visitor sent, whatever its spelling, also with underscores instead
of dashes, and then sets its own from what the Coordinator says about the visitor's Device address. A visitor cannot
claim to be someone else.

That guarantee holds only for requests that come through the Service. Keep the program listening on `127.0.0.1` and the
Service's target on `localhost`, so nothing else can reach it: see [Keep the program on localhost](/docs/services/targets#keep-the-program-on-localhost).

Headers under the names used before the rename are removed too, so those cannot be forged either. A hosting Device that
has not updated since the rename still sends the headers under the old names: [the migration note](/docs/moving-from-quietlane)
lists them.

## Read them

Each of these answers on port 3000 of the hosting Device, so the Service's target is `localhost:3000`. Without the
headers, the request did not come through Tiltas.

```js tab="Node.js"
import { createServer } from "node:http"

createServer((req, res) => {
  const device = req.headers["tiltas-device"]
  const memberId = req.headers["tiltas-member-id"]
  const email = req.headers["tiltas-email"]
  const name = req.headers["tiltas-name"] ? decodeURIComponent(req.headers["tiltas-name"]) : undefined

  res.setHeader("Content-Type", "text/plain; charset=utf-8")
  if (!device) return res.writeHead(401).end("Open this through Tiltas.\n")
  if (!memberId) return res.end(`Hello, Server ${device}.\n`)
  res.end(`Hello, ${name} <${email}> on ${device}.\n`)
}).listen(3000, "127.0.0.1")
```

```python tab="Python"
from http.server import BaseHTTPRequestHandler, HTTPServer
from urllib.parse import unquote

class Handler(BaseHTTPRequestHandler):
    def do_GET(self):
        device = self.headers.get("Tiltas-Device")
        member_id = self.headers.get("Tiltas-Member-Id")
        email = self.headers.get("Tiltas-Email")
        name = unquote(self.headers.get("Tiltas-Name", ""))

        if not device:
            body, status = "Open this through Tiltas.\n", 401
        elif not member_id:
            body, status = f"Hello, Server {device}.\n", 200
        else:
            body, status = f"Hello, {name} <{email}> on {device}.\n", 200

        self.send_response(status)
        self.send_header("Content-Type", "text/plain; charset=utf-8")
        self.end_headers()
        self.wfile.write(body.encode())

HTTPServer(("127.0.0.1", 3000), Handler).serve_forever()
```

```go tab="Go"
package main

import (
	"fmt"
	"net/http"
	"net/url"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		device := r.Header.Get("Tiltas-Device")
		memberID := r.Header.Get("Tiltas-Member-Id")
		email := r.Header.Get("Tiltas-Email")
		name, _ := url.PathUnescape(r.Header.Get("Tiltas-Name"))

		switch {
		case device == "":
			http.Error(w, "Open this through Tiltas.", http.StatusUnauthorized)
		case memberID == "":
			fmt.Fprintf(w, "Hello, Server %s.\n", device)
		default:
			fmt.Fprintf(w, "Hello, %s <%s> on %s.\n", name, email, device)
		}
	})
	http.ListenAndServe("127.0.0.1:3000", nil)
}
```

Opened from the Personal device `macbook` of Jonas, each one answers:

```text title="Output" noCopy
Hello, Jonas Žukas <jonas@example.org> on macbook.
```

The Node.js sample is an ES module: save it as `server.mjs`. The Python and Go samples use only the standard library.

## The other headers

The program also gets `X-Forwarded-For` with the visitor's Device address, and `X-Forwarded-Host` and
`X-Forwarded-Proto`. See [What a target can be](/docs/services/targets#web-targets-speak-http).
