# Authelia: Access restrictions by working hours

LLMS index: [llms.txt](/en/llms.txt)

---

Authelia provides fine-grained access control, and restricting internal service access to working hours is a common self-hosted infrastructure task. Using local accounts and Docker Compose, you can enforce time-based policies without external identity providers.

## 1. Preparation and compose

A typical Authelia deployment via Docker Compose includes the `authserver/auth-server` container, a PostgreSQL database for local users or LDAP, and volumes for configuration and data. A minimal `docker-compose.yml` may look like this:

```yaml
version: '3.8'
services:
  authelia:
    image: authserver/auth-server:latest
    container_name: authelia
    environment:
      - DATABASE_CONNECTIONSTRING=postgres://authelia:password@db/authelia?sslmode=disable
      - DEFAULT_LOGIN_RETURN_URL=https://auth.example.com
      - SESSION_SECRET=super-secret-key-change-in-production
    volumes:
      - ./configuration:/etc/authelia
    ports:
      - "9091:9091"
    depends_on:
      - db
  db:
    image: postgres:15
    container_name: authelia-db
    environment:
      - POSTGRES_USER=authelia
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=authelia
    volumes:
      - pg_data:/var/lib/postgresql/data
    restart: unless-stopped
volumes:
  pg_data:
```

After running `docker-compose up -d`, access to the Authelia web console opens at `https://auth.example.com` (replace with your domain with SSL configured). The first authentication uses the built-in account `admin@authelia.local` with the password set in `configuration.yml`, after which it is recommended to change the password and add real users.

> [!TIP]
> Further policy edits are best performed via `configuration.yml` editing or the *Access Control* web interface section, so you can roll back in case of policy errors.

## 2. Local users and groups setup

Authelia stores users in a PostgreSQL database. Adding a user and group happens either via SQL commands or through the web interface under *Identity Providers → Local Users*.

Example adding the user `devops-team` and group `engineers` via PostgreSQL console (connecting to the `authelia` container):

```bash
docker exec -it authelia psql -U authelia -d authelia
```

Inside the psql console:

```sql
INSERT INTO public.users (username, email, password, salt, disabled, created_at)
VALUES ('devops-team', 'devops@example.com', 'hashed-password', 'salt-value', false, now());

INSERT INTO public.groups (name, description)
VALUES ('engineers', 'DevOps engineers team');

INSERT INTO public.users_groups (user_id, group_id)
SELECT u.id, g.id FROM public.users u, public.groups g
WHERE u.username = 'devops-team' AND g.name = 'engineers';
```

Exit psql (`\q`) and return to the container shell.

Next, create an application (application) to which the access restriction will apply. In the config `configuration.yml` or via the *Applications* interface, add an entry linking the `engineers` group to the internal service, e.g., `sonarr` or `prowlarr`, and specify the service address behind the Authelia proxy.

> [!WARNING]
> When using the local storage, users and groups are not lost on container restart, as data is preserved in the `pg_data` volume. Transitioning to LDAP/SAML will change the policy structure, but the time-restriction logic remains similar.

## 3. Creating time-based access policy

The core of the restriction setup is the access control policy. In Authelia, a policy defines who (subject), to which resource (resource), and what action (action) is allowed, along with conditions, among which is time.

Policy structure in `configuration.yml` looks approximately like this (syntax may slightly differ depending on version, always check current documentation):

```yaml
access_control:
  policies:
    - subject:
        user:
          - group: engineers
      resource:
        ip:
          - 192.168.1.0/24
          - 10.0.0.0/8
      action:
        - type: access
      conditions:
        - time:
            not_before: "08:00:00+03:00"
            not_after: "18:00:00+03:00"
```

In this example, access is allowed only from 08:00 to 18:00 considering the `+03:00` time zone (set under your TZ). The `not_before` condition blocks entry before the specified time, `not_after` — after. If access needs to be limited to specific weekdays, this is configured via additional conditions or a combination of multiple policies.

Table of key access control policy fields:

| Field | Description | Example |
|-------|-------------|---------|
| `subject.user` | User or group the policy applies to | `group: engineers` |
| `resource.ip` | List of allowed IP addresses (optional) | `192.168.1.0/24` |
| `action.type` | Action type (usually `access`) | `access` |
| `conditions.time.not_before` | Early entry allow time | `08:00:00+03:00` |
| `conditions.time.not_after` | Late entry allow time | `18:00:00+03:00` |

Applying the policy:

1. Open the *Access Control* section in the Authelia web console or edit `configuration.yml`.
2. Add a new policy as shown above (or adapted to your IPs, groups, and hours).
3. Save the config and run `docker-compose up -d` to apply changes.

> [!NOTE]
| Time is specified in ISO 8601 format with time zone offset. If Authelia runs on a host with one TZ and users are in another, a mismatch will cause restrictions to trigger at the wrong hour. It is recommended to synchronize time on the host and inside the container via NTP. |

## 4. Verifying restrictions work

After restarting Authelia, check two situations: entry within the allowed time window and entry outside its limits.

1. **Within hours** (e.g., 10:00 local TZ): an attempt to access the service behind Authelia should succeed; the user is redirected to the login page, and after authentication — to the actual service.

2. **Outside hours** (e.g., 22:00): access should be blocked. Authelia returns a `403 Forbidden` page or a message that access is unavailable at the current time.

For diagnostics, you can view Authelia logs:

```bash
docker logs authelia --tail 50
```

Or via the API/status page `https://auth.example.com/api/status`. In logs, look for policy-blocking entries, e.g.:

```
time="2024-05-20T22:15:30+03:00" level=info msg="Access denied for user devops-team to resource sonarr, policy time condition not satisfied"
```

It is also useful to check how Authelia calculates time by executing a request to the internal time service or ensuring the container has correct `TZ` environment data or host time binding via `volumes: - /etc/localtime:/etc/localtime:ro`.

## 5. Common errors and typical failures

1. **Time zone mismatch.** The most frequent cause — Authelia runs on a server with one TZ (e.g., UTC), while users expect another. Result: access is blocked or allowed at the wrong hour. Solution: set `TZ=Europe/Moscow` in the Authelia container environment and ensure the host is also synchronized via NTP.

2. **Incorrect policy order.** Authelia applies policies in the order they are listed in the config. If a policy with time restriction is followed by a global `allow` without conditions, access remains open always. Ensure the policy with `conditions.time` is placed higher in the list, or use more specific rules.

3. **Group not linked to application.** A user may be added to a group, but if the group is not selected in the application (resource) settings, the policy will not take effect. Verify the connection `group: engineers` and `application: sonarr` in the interface or `configuration.yml`.

4. **Time format errors.** Using time without time zone offset (`08:00` instead of `08:00:00+03:00`) may cause Authelia to apply system UTC time, shifting boundaries. Always write the full ISO string with `+HH:MM`.

5. **NTP not running on host.** If the host machine does not synchronize time, Authelia will operate with drift. In production, always enable NTP or `systemd-timesyncd`, and in `docker-compose.yml` add `restart: unless-stopped` and control time via `environment: - TZ=Europe/Moscow`.

6. **Policy covers too broad a range.** Sometimes administrators set `not_before: "00:00:00+03:00"` and `not_after: "23:59:59+03:00"` for testing, which effectively disables the restriction. Use exact working hours and test at the boundaries.

If an unexpected block occurs, first check logs for `time condition` and ensure the time on the host and in the Authelia container matches within a second.
