Identity, sessions, and security
The identity and security API controls how people sign in, how the server keeps a login active, and how each request gains permission to act. It supports local passwords, LDAP, OIDC, MFA, session cookies, bearer API tokens, global permissions, and audit records for security-sensitive actions.
Authentication methods
Authentication proves who is making a request. The API presents the enabled login choices to clients and turns a successful choice into a verified user session or a bearer token.
Password and directory login
Password login checks a local account. LDAP login checks the user's credentials against a configured directory and can update access from directory groups. External accounts do not use a local password.
The login metadata endpoint tells the client whether password login is enabled and which LDAP, OIDC, and MFA methods are available. A failed login returns an unauthorized result for bad credentials or a disabled method. Provider failures remain distinct so operators can tell a bad login from an unavailable identity service.
References: api/login.go• L444-492 api/login.go• L496-657
OpenID Connect login
OpenID Connect (OIDC) lets an external identity provider prove the user's identity through a browser redirect. The API checks the provider response and uses the stable sub value as the external identity key.
The flow uses a state value tied to a browser cookie. This protects the redirect from an unrelated sign-in attempt. The provider can also supply claims used for the user's profile and role mapping. A verified external identity can create a new external account or sign in to an account that already owns that identity.
References: api/login.go• L705-781 api/login.go• L784-839 api/login.go• L1094-1344
User identities
An external identity is a stable account key held by an LDAP or OIDC provider and linked to one Semaphore user. The link lets later logins find the same user without treating a changeable email address as proof of ownership.
Identity matching and provisioning
The API first looks for the exact provider and external ID. If no link exists, it may match a verified external email, subject to the configured matching mode. It never uses an external match to take over a local account.
If no safe match exists, the API creates an external user and links the provider identity. Provider profile fields can be refreshed for external users on later logins. Local profile data remains authoritative for local accounts.
References: api/login_identity.go• L27-116 api/login_identity.go• L119-163 api/login_identity.go• L203-225
Identity linking and removal
An already signed-in user can link an LDAP or OIDC identity to the current account. Linking requires proof through the provider's own login flow. The API rejects an identity that belongs to another user and allows only one identity for a given provider on an account.
Users can list and remove their linked identities through the user API. An external user cannot remove its last external identity because that would remove its only sign-in path. Each link and unlink is recorded for audit.
References: api/user.go• L63-123 api/login.go• L798-814 api/login.go• L1228-1273 api/users.go• L403-463
Sessions and MFA
A session is the server-side login state represented to the browser by the semaphore cookie. The session records the user and whether the required verification step is complete. The API accepts the session for protected work only after it is valid and verified.
Session lifetime and request checks
The API decodes the cookie, loads the matching active session, and rejects it when it is expired. Expiration can come from the absolute session lifetime or from inactivity. A valid session is touched as requests use it, while logout expires the session and clears the cookie.
Browser session cookies are HTTP-only and use SameSite protection. State-changing cookie requests with a different origin are blocked. Bearer-token requests do not need this browser CSRF check because browsers do not attach their bearer token automatically.
The request path is therefore:
flowchart LR
C[Session cookie] --> D[Decode and load session]
D --> E{Active and within lifetime?}
E -- No --> R[Unauthorized]
E -- Yes --> V{MFA complete?}
V -- No --> M[Verification required]
V -- Yes --> A[Authenticated user request]
A --> T[Refresh activity time]
References: api/auth.go• L23-67 api/auth.go• L259-347 api/login.go• L234-299 api/login.go• L660-703 services/session_svc.go• L12-64
TOTP and recovery
Time-based one-time passwords (TOTP) add a second factor to a user's login. When TOTP is enabled for the account, login creates a session that is not yet verified. The client must submit the passcode to the verification endpoint before protected requests can proceed.
The user API enables and disables TOTP and can return its setup QR image. When recovery is enabled, the API issues a recovery code and accepts it once to complete recovery. Failed passcodes and recovery attempts do not complete the session and are recorded as failed MFA events.
References: api/login.go• L234-299 api/auth.go• L69-185 api/auth.go• L187-257 api/users.go• L465-570
Email verification
Email is an available session verification method when the edition supplies its email verification service. The common API keeps the session pending and delegates the email check to that service. Clients should treat the verification response as the contract rather than assume that every deployment has email verification enabled.
References: api/auth.go• L187-205 pro/…/auth_verify.go• L9-12 api/login.go• L417-427
Tokens and public keys
The API has two different token roles. User-created API tokens authenticate API calls, while signed JWTs carry short-lived task-run data for systems that need to verify a task token.
User API tokens
An API token is a bearer credential tied to one user. The user API creates tokens with an optional expiry, lists only token prefixes, and deletes tokens by a user-owned prefix. The full token is returned at creation so the client can store it; later audit records use a fingerprint rather than the secret.
Requests send the token in the Authorization: Bearer header. The API rejects an unknown or expired token with an unauthorized response and records the reason. A token authenticates the user but does not bypass the user's permissions.
References: api/user.go• L125-212 api/auth.go• L259-304 db/…/session.go• L27-62
JWT signing and JWKS
JSON Web Tokens (JWTs) issued by the server carry task identity and time claims. A JSON Web Key Set (JWKS) is the public-key document that lets another service verify those signatures without receiving the private signing key.
The public endpoint /.well-known/jwks.json exists only when JWT support is enabled and a signer is available. The signer exposes the current public key and key ID. Task tokens have an issuer, audience, task subject, lifetime, and Semaphore task identifiers; they are separate from user API tokens.
References: api/router.go• L151-153 api/jwks.go• L11-40 pkg/…/signer.go• L21-29 pkg/…/signer.go• L104-146 pkg/…/claims.go• L28-62
Global permissions
Global permissions control actions across the whole Semaphore instance. They are separate from project permissions so an ability such as managing users or reading the audit log cannot be granted by a project role.
Effective permission model
An administrator has all global permissions. Other users gain the union of the permissions in their assigned global roles. The user API exposes the effective mask and a decision for each catalog entry so clients can show both the result and the role that grants it.
The global permission catalog currently covers user management, global role management, system management, and audit-log access. A user manager cannot change an administrator or grant access beyond the manager's own effective permissions.
References: db/Role.go• L10-45 db/Role.go• L156-216 api/global_permissions.go• L21-94 api/user.go• L39-60
Protected global API
The router applies a required global permission before it reaches protected instance-wide routes. This includes user creation, system settings and operations, and global role and assignment management. An administrator passes these checks through the administrator flag even if role data cannot be read.
Denied permission checks return a forbidden response and create an authorization-deny audit event. Project resource and task access use their own project-level rules, so this page's global permissions do not replace the controls described in Project resource API and Task and runner API.
References: api/router.go• L216-240 api/router.go• L280-306 api/global_permissions.go• L56-73
Security audit behavior
The audit API gives operators a record of authentication, identity, permission, and security events. Events identify the user or system actor, the target, the outcome, and a fixed reason or method where one applies.
Authentication and identity events
Successful login and logout form the session lifecycle. Login metadata identifies password, LDAP, or OIDC and the configured provider. MFA verification, recovery, token rejection, and CSRF blocking use separate event kinds so an operator can distinguish an incomplete login from a rejected request.
User changes also record account creation, password changes or resets, MFA changes, identity links and unlinks, and API token creation or deletion. The audit stream records token fingerprints instead of token values.
References: services/…/kind.go• L23-45 services/…/types.go• L81-129 services/…/catalog.go• L36-61 api/login.go• L383-408 api/user.go• L178-209
Audit access and denial records
Reading the audit log is itself a global permission. Authorization and CSRF denials are recorded with a stable permission or route target and a small metadata record. Failure reasons use a fixed set such as invalid credentials, invalid MFA passcode, unknown token, expired token, or cross-origin request.
This gives downstream audit consumers stable categories instead of relying on error text. Changes to an event kind, reason, target, or metadata shape must remain aligned with the audit catalog because the catalog validates the event contract.
References: api/router.go• L232-235 services/…/catalog.go• L38-59 services/…/catalog.go• L186-215 api/auth.go• L440-483
Change checklist
When these files or sections are changed, remember to consider these:
- Keep login metadata, enabled provider routes, and the session verification methods in sync so clients do not offer a path that the server cannot complete.
- Preserve the external identity ownership rules when changing OIDC or LDAP claim matching. A verified provider ID must remain the primary identity key.
- Keep session expiry checks, cookie settings, CSRF checks, and bearer-token handling aligned. They form one security boundary.
- Update the global permission catalog and every route middleware that depends on a new global permission.
- Keep audit event kinds, allowed failure reasons, metadata types, and actor authentication methods aligned with the catalog.
- Do not expose full API tokens or private JWT signing material through responses or audit records.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| Session is expired by inactivity or absolute lifetime | Protected request is unauthorized and the session is expired | Load and validate the server-side session on each request |
| MFA session is used before verification | Request is rejected with the required verification step | Mark the session verified only after TOTP or email verification succeeds |
| OIDC state is missing or does not match its cookie | Login callback is rejected | Check the state value before exchanging the provider code |
| External identity matches a local account | Login cannot adopt the local account | Match only safe external accounts and use the provider's stable ID |
| API token is unknown or expired | Bearer request is unauthorized | Store active-token state and reject failed lookups or expired tokens |
| Cross-origin state change uses a session cookie | Request is forbidden | Apply same-origin checks to cookie-authenticated state changes |
| User lacks a required global permission | Instance-wide route returns forbidden | Combine administrator status or assigned global roles before the handler |
| Audit event uses an unknown reason or metadata shape | Event fails audit validation | Keep event producers aligned with the audit catalog |
Key files
| File | Why to read it |
|---|---|
| api/router.go | Shows the public authentication routes and the middleware boundaries for sessions, CSRF, and global permissions. |
| api/login.go | Describes login methods, OIDC flow, session creation, logout, and login audit events. |
| api/auth.go | Shows request authentication, MFA verification, token rejection, session activity, and CSRF behavior. |
| api/login_identity.go | Defines how LDAP and OIDC identities resolve to users and how takeover protection works. |
| api/users.go | Exposes MFA, identity, password, and user-management behavior. |
| api/user.go | Exposes the current user's API tokens, linked LDAP identity, and effective permission view. |
| api/global_permissions.go | Explains how global roles become route-level permission decisions. |
| db/Role.go | Defines the global permission model and effective permission results. |
| api/jwks.go and pkg/…/signer.go | Explain the public key endpoint and the task JWT signing contract. |
| services/…/catalog.go and services/…/kind.go | Define the stable audit event contract for security and identity actions. |