# Let only chosen groups sign in to an app

> Restrict an app to chosen groups: fill a Group from Active Directory or an email domain, list it in allowedGroups, map roles from the groups claim.

This guide gives an application to the people in one or two groups and to nobody else.
It fills two groups from an Active Directory, lists them in the application's
[`OIDCClient`](https://www.nauthera.io/docs/v1/oidc-client), and has the application map its roles from the
`groups` claim. Grafana is the example; any OpenID Connect application works the same
way. You need a Nauthera release with its [operator](https://www.nauthera.io/docs/v1/operator) running, the
namespace `nauthera` admitted to the main organization, and `kubectl`. At the end, a user
outside both groups is refused by Nauthera before the application sees them, and leaving
a group takes access away at the user's next refresh. The main limit: a directory
group's members are read when they sign in through the directory, so a change there
reaches Nauthera at that user's next sign-in.

| Step | Resource | What it does |
|---|---|---|
| [Read the directory's groups](#1-read-the-directorys-groups) | [`LDAPDirectory`](https://www.nauthera.io/docs/v1/ldap-directory-resource) | Each sign-in through the directory reads the user's groups, nested ones included |
| [Declare the groups](#2-declare-the-groups) | [`Group`](https://www.nauthera.io/docs/v1/group-resource) | Each group follows a directory group, an email domain, or members added by hand |
| [Restrict the client](#3-restrict-the-client) | [`OIDCClient`](https://www.nauthera.io/docs/v1/oidc-client#who-may-sign-in) | Only members of the listed groups sign in; the `groups` claim names theirs |
| [Map roles in the application](#4-map-roles-in-the-application) | Grafana's configuration | Admins get Admin, everyone else let in gets Viewer |

## 1. Read the directory's groups

Skip this step if your groups follow email domains or are filled by hand.

The directory connection reads a user's groups only when its `spec.groups` says how. On
Active Directory, `inChain` follows nested groups in one search:

```yaml
apiVersion: nauthera.io/v1alpha1
kind: LDAPDirectory
metadata:
  name: corp
  namespace: nauthera
spec:
  url: ldaps://dc1.corp.example:636
  userBaseDn: OU=Staff,DC=corp,DC=example
  userFilter: (objectClass=user)
  usernameAttribute: sAMAccountName
  groups:
    lookup: inChain
    baseDn: OU=Groups,DC=corp,DC=example
```

The client must also offer the directory, in its `spec.ldapDirectories` (step 3). On
another LDAP server, use `lookup: search` or `memberOf`; see
[Reading a user's groups](https://www.nauthera.io/docs/v1/ldap-directory-resource#reading-a-users-groups) for
what each reads and what it misses.

## 2. Declare the groups

One `Group` for the people who administer Grafana, and one for everyone else who may
use it. `directoryGroups` follows a group in the directory by its DN, spelled as the
directory spells it; `emailDomains` admits every verified address at a domain:

```yaml
apiVersion: nauthera.io/v1alpha1
kind: Group
metadata:
  name: grafana-admins
  namespace: nauthera
spec:
  displayName: Grafana Admins
  directoryGroups:
    - directory: corp
      dn: CN=Grafana Admins,OU=Groups,DC=corp,DC=example
---
apiVersion: nauthera.io/v1alpha1
kind: Group
metadata:
  name: grafana-users
  namespace: nauthera
spec:
  displayName: Grafana Users
  directoryGroups:
    - directory: corp
      dn: CN=Engineering,OU=Groups,DC=corp,DC=example
  emailDomains: [corp.example]
```

Both must be `Ready` before a client can name them:

```bash
kubectl -n nauthera get groups
```

A group fills as its people sign in: a user joins when a session is established for
them and a mapper matches. An owner can also add someone by hand on the group's page in
the [admin dashboard](https://www.nauthera.io/docs/v1/admin-dashboard), which no mapper undoes. See
[Mappers](https://www.nauthera.io/docs/v1/group-resource#mappers) for how members come and go.

## 3. Restrict the client

The application's `OIDCClient` lists the Groups by resource name in `allowedGroups`, and
asks for the `groups` scope so the application can tell the two apart:

```yaml
apiVersion: nauthera.io/v1alpha1
kind: OIDCClient
metadata:
  name: grafana
  namespace: nauthera
spec:
  clientId: grafana
  redirectUris: [https://grafana.example.com/login/generic_oauth]
  grantTypes: [authorization_code, refresh_token]
  allowRefresh: true
  scopes: [openid, profile, email, groups]
  allowedGroups: [grafana-admins, grafana-users]
  ldapDirectories:
    - slug: corp
      allowSignIn: true
      allowSignUp: true
  secret:
    generate: true
```

The Groups must be in the client's namespace. The operator waits for each to be declared
on the server, reporting `GroupNotFound` until it is, and never registers the client with
a shorter list than the manifest says. The operator writes the client's credentials into
the Secret `grafana`, under `client-id`, `client-secret` and `issuer`.

```bash
kubectl -n nauthera get oidcclient grafana
```

## 4. Map roles in the application

Nauthera has already refused everyone outside both groups, so the application only
decides who gets which role. In Grafana:

```ini
[auth.generic_oauth]
enabled = true
name = Nauthera
client_id = grafana
client_secret = $__file{/etc/secrets/grafana/client-secret}
scopes = openid profile email groups
auth_url = https://auth.example.com/authorize
token_url = https://auth.example.com/token
api_url = https://auth.example.com/userinfo
use_pkce = true
login_attribute_path = preferred_username
groups_attribute_path = groups
role_attribute_path = contains(groups[*], 'Grafana Admins') && 'Admin' || 'Viewer'
```

Mount the Secret `grafana` at `/etc/secrets/grafana`. The claim carries display names,
so the expression names `Grafana Admins`, not the resource name `grafana-admins`.

## Check it

- **A member signs in** and lands in Grafana with the role the expression gives.
- **Someone outside both groups** signs in to Nauthera and is sent back to Grafana with
  `access_denied`. The [audit log](https://www.nauthera.io/docs/v1/audit-log) records it as
  `oauth.authorization.requested` with the cause `group_not_allowed`, and the
  [`access_denied` runbook](https://www.nauthera.io/docs/v1/runbooks/oauth/access_denied) walks through it.
- **The client's page** in the admin dashboard lists the two groups under who may sign in.

## Taking access away

- **Leaving the directory group** withdraws the user from the `Group` at their next
  sign-in through the directory, and their next refresh is refused with `invalid_grant`.
  Membership is as of that sign-in: someone disabled in the directory, who never signs
  in again, stays a member, and a refresh token they already hold keeps working. End
  their sessions from their page in the admin dashboard, which retires those refresh
  tokens, or remove them from the group by hand.
- **Removing someone by hand** on the group's page takes effect at their next refresh,
  and lasts until their next sign-in if a mapper still matches them; change the
  directory group or the mapper to keep them out.
- **An access token already issued** stays valid until it expires, since revocation does
  not reach access tokens (`issuer.access_token_ttl`, ten minutes by default). Keep it
  short for applications that need access to end promptly.

## Related

- [Group](https://www.nauthera.io/docs/v1/group-resource): mappers, members by hand, and the `groups` claim
- [OIDCClient](https://www.nauthera.io/docs/v1/oidc-client#who-may-sign-in): `allowedGroups` and `GroupNotFound`
- [LDAPDirectory](https://www.nauthera.io/docs/v1/ldap-directory-resource#reading-a-users-groups): how a sign-in reads groups
- [Operator](https://www.nauthera.io/docs/v1/operator#who-may-change-what): who may write `Group` and `OIDCClient`
