Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ is no separate database to run and no config file to write before the first star
in the UI
- Release, prerelease and mixed version policies
- Public repositories readable without an account, private ones granted per user
- Single sign-on through any OpenID Connect provider, with group-based access and admin roles
- Deployment links: a fixed URL that downloads the newest matching file of an artifact, without
authentication, even from a private repository
- Repositories can be renamed, with the old name left redirecting until something else claims it
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ before the first start.
| Move off Sonatype Nexus | [Migrating from Nexus]({{ site.baseurl }}/migrating) |
| Put it on a server | [Deploying]({{ site.baseurl }}/deploying) |
| Run it day to day | [Managing an installation]({{ site.baseurl }}/managing) |
| Sign in through your identity provider | [Single sign-on]({{ site.baseurl }}/oidc) |

## Need help?
[Join our Discord!](https://discord.gg/cep5gXDVHf) Our lovely developer and friends will try to help you out.
Expand Down
90 changes: 90 additions & 0 deletions docs/oidc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
title: Single sign-on
layout: default
nav_order: 10
---

# Single sign-on

Arca can sign people in through an OpenID Connect provider such as Authentik, Keycloak or Entra ID.
Setting `OIDC_ISSUER` turns it on: the login page gains a **Sign in with ...** button next to the
password form, which keeps working for accounts that have a password.

## Configuration

| Flag | Environment | Default | Purpose |
| --- | --- | --- | --- |
| `--oidc-issuer` | `OIDC_ISSUER` | empty, off | issuer URL of the provider |
| `--oidc-client-id` | `OIDC_CLIENT_ID` | | client id, required with an issuer |
| `--oidc-client-secret` | `OIDC_CLIENT_SECRET` | | client secret, required with an issuer |
| `--oidc-name` | `OIDC_NAME` | `SSO` | provider name on the sign-in button |
| `--oidc-scopes` | `OIDC_SCOPES` | `openid profile email` | space-separated scopes to request |
| `--oidc-groups-claim` | `OIDC_GROUPS_CLAIM` | `groups` | ID token claim that lists the user's groups |
| `--oidc-admin-groups` | `OIDC_ADMIN_GROUPS` | empty | comma-separated groups whose members are administrators |
| `--oidc-allowed-groups` | `OIDC_ALLOWED_GROUPS` | empty, everyone | comma-separated groups allowed to sign in |
| `--oidc-link-existing` | `OIDC_LINK_EXISTING` | `false` | link a first sign-in to an existing account with the same email |

Register this redirect URI with the provider:

```
https://artifacts.example.com/api/auth/oidc/callback
```

It is built on `BASE_URL`, or on the address of the request when that is not set. The email, name
and groups are read from the ID token, so the provider has to put them there; an ID token without an
email is refused.

Accounts are matched on the provider's subject, so a user whose email changes at the provider keeps
their account. Only when no account is linked yet does the email matter: a free address gets a new
account, and one that already belongs to an account is refused unless `OIDC_LINK_EXISTING` is on.
Turn that on only for a provider that verifies email addresses, since it lets whoever holds that
address at the provider into the account. To limit who gets an account, use `OIDC_ALLOWED_GROUPS`.

The display name comes from the provider when the account is created and is not synced afterwards,
so a name changed in Arca stays. The email is updated at every sign-in, unless another account
already uses the new address.

## Setting up with single sign-on

With OIDC configured, the administrator step of the first-run wizard offers **Continue with ...**
next to the name, email and password fields. Signing in there makes that provider account the
administrator, so an instance can be set up and managed without any password account. The setup
token is still required.

With `OIDC_ADMIN_GROUPS` set, only a member of one of those groups can set the instance up through
the provider. Anyone else is turned away and can still use the password fields.

Until setup is finished, sign-in through the provider creates no accounts: it only fills in the
wizard's administrator step.

## Authentik example

In Authentik, create an **OAuth2/OpenID Provider** with a confidential client, set its redirect URI
to the one above, keep **Include claims in id_token** on, and add an application for it. The
default `profile` scope already carries a `groups` claim.

```sh
OIDC_ISSUER=https://auth.example.com/application/o/arca/
OIDC_CLIENT_ID=...
OIDC_CLIENT_SECRET=...
OIDC_NAME=Authentik
OIDC_ADMIN_GROUPS=arca-admins
OIDC_ALLOWED_GROUPS=arca-users,arca-admins
```

The issuer is the application's **OpenID Configuration Issuer**, trailing slash included.

## Things to know

When admin groups are set, the provider owns the role of every account linked to it. Every sign-in
makes a member of an admin group an administrator and everyone else a user, and the role of a linked
account cannot be changed under **Manage**. The one exception is the last enabled administrator, who
is never demoted. Accounts that never signed in through the provider keep a role set in Arca. With
`OIDC_ADMIN_GROUPS` empty, Arca never touches a role.

Removing someone at the provider only takes effect at their next sign-in. Sessions last 30 days and
API tokens do not expire, so to offboard someone, disable their user in Arca as well.

Build tools keep using API tokens. Maven, npm and Docker clients cannot follow a browser sign-in,
and an account created through single sign-on has no password, so it creates an API token under
**Account** and uses that.
18 changes: 12 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,21 +1,29 @@
module arca

go 1.26
go 1.26.0

require (
github.com/charmbracelet/log v1.0.0
github.com/coreos/go-oidc/v3 v3.21.0
github.com/glebarez/sqlite v1.11.0
github.com/go-jose/go-jose/v4 v4.1.9
github.com/gorilla/mux v1.8.1
golang.org/x/crypto v0.54.0
golang.org/x/oauth2 v0.38.0
gorm.io/gorm v1.31.2
)

require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc // indirect
github.com/charmbracelet/lipgloss v1.1.0 // indirect
github.com/charmbracelet/log v1.0.0 // indirect
github.com/charmbracelet/x/ansi v0.8.0 // indirect
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect
github.com/charmbracelet/x/term v0.2.1 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/glebarez/go-sqlite v1.21.2 // indirect
github.com/glebarez/sqlite v1.11.0 // indirect
github.com/go-logfmt/logfmt v0.6.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/mux v1.8.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
Expand All @@ -25,11 +33,9 @@ require (
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/crypto v0.54.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/sys v0.47.0 // indirect
golang.org/x/text v0.40.0 // indirect
gorm.io/gorm v1.31.2 // indirect
modernc.org/libc v1.22.5 // indirect
modernc.org/mathutil v1.5.0 // indirect
modernc.org/memory v1.5.0 // indirect
Expand Down
22 changes: 22 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,24 @@ github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd h1:vy0G
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd/go.mod h1:xe0nKWGd3eJgtqZRaN9RjMtK7xUYchjzPr7q6kcvCCs=
github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ=
github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
github.com/coreos/go-oidc/v3 v3.21.0 h1:wZo4Q9Pum8dYEj0eMUPrqR+kvuGkeUplbLpNCkBqoWM=
github.com/coreos/go-oidc/v3 v3.21.0/go.mod h1:DYCf24+ncYi+XkIH97GY1+dqoRlbaSI26KVTCI9SrY4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/glebarez/go-sqlite v1.21.2 h1:3a6LFC4sKahUunAmynQKLZceZCOzUthkRkEAl9gAXWo=
github.com/glebarez/go-sqlite v1.21.2/go.mod h1:sfxdZyhQjTM2Wry3gVYWaW072Ri1WMdWJi0k6+3382k=
github.com/glebarez/sqlite v1.11.0 h1:wSG0irqzP6VurnMEpFGer5Li19RpIRi2qvQz++w0GMw=
github.com/glebarez/sqlite v1.11.0/go.mod h1:h8/o8j5wiAsqSPoWELDUdJXhjAhsVliSn7bWZjOhrgQ=
github.com/go-jose/go-jose/v4 v4.1.9 h1:pKfeQkig1NmgK4hJrZYXLEYURXfoJB61Larw0uIW4gY=
github.com/go-jose/go-jose/v4 v4.1.9/go.mod h1:x4oUasVrzR7071A4TnHLGSPpNOm2a21K9Kf04k1rs08=
github.com/go-logfmt/logfmt v0.6.1 h1:4hvbpePJKnIzH1B+8OR/JPbTx37NktoI9LE2QZBBkvE=
github.com/go-logfmt/logfmt v0.6.1/go.mod h1:EV2pOAQoZaT1ZXZbqDl5hrymndi4SY9ED9/z6CO0XAk=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
Expand All @@ -34,25 +44,37 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
golang.org/x/crypto v0.54.0 h1:YLIA59K4fiNzHzjnZt2tUJQjQtUWfWbeHBqKtk3eScw=
golang.org/x/crypto v0.54.0/go.mod h1:KWL8ny2AZdGR2cWmzeHrp2azQPGogOv+HeQaVEXC2dk=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/oauth2 v0.38.0 h1:uW7XQbcu1eoiQS7uMJvLOjPMl04bTbmY1VeRPxvbspI=
golang.org/x/oauth2 v0.38.0/go.mod h1:IxwZNxUULJmpBFf9K/9NTMSIfZZuvuTy1gGxhigP/58=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/text v0.40.0 h1:Ub2Z6/xjgF1WrYQz2nuITOEegKFtiIy+rieRJ5lHZKs=
golang.org/x/text v0.40.0/go.mod h1:hpnzDAfGV753zIKo+wk3u1bVKCGPbrnF7+7LBF/UHVY=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ=
gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8=
gorm.io/gorm v1.31.2 h1:3o8FXNo9v9S858gil+3LlZA1LkCOzgb4g5BL64FgaCo=
gorm.io/gorm v1.31.2/go.mod h1:XyQVbO2k6YkOis7C2437jSit3SsDK72s7n7rsSHd+Gs=
modernc.org/libc v1.22.5 h1:91BNch/e5B0uPbJFgqbxXuOnxBQjlS//icfQEGmvyjE=
Expand Down
5 changes: 3 additions & 2 deletions internal/auth/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,15 @@ var decoyHash = sync.OnceValue(func() string {
// throttled per client address and per account, takes the same time whether the
// account exists or not, and remembers a verified pair for a while so a client
// that presents Basic credentials on every request is not charged a key
// derivation each time.
// derivation each time. An account without a password, one that signs in only
// through the identity provider, is rejected the same way as a missing one.
func (r *Resolver) Authenticate(address, login, password string) (*models.User, error) {
if ok, wait := r.guard.Allow(address, login); !ok {
return nil, ThrottledError{RetryAfter: wait}
}

user, err := r.store.UserByEmail(login)
if err != nil || user.Disabled {
if err != nil || user.Disabled || user.PasswordHash == "" {
VerifyPassword(password, decoyHash())
r.guard.Fail(address, login)
return nil, ErrInvalidCredentials
Expand Down
160 changes: 160 additions & 0 deletions internal/auth/oidc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package auth

import (
"cmp"
"context"
"errors"
"fmt"
"strings"
"sync"
"time"

"github.com/coreos/go-oidc/v3/oidc"
"golang.org/x/oauth2"

"arca/internal/config"
)

// oidcTimeout bounds each conversation with the provider, so one that hangs
// fails the sign-in instead of holding the request, and the discovery lock,
// for as long as the browser waits.
const oidcTimeout = 15 * time.Second

// OIDC signs users in through an OpenID Connect provider. Discovery waits for
// the first sign-in and is cached only once it succeeds, so a provider that is
// down neither blocks startup nor stays broken after it comes back.
type OIDC struct {
settings config.OIDC

mutex sync.Mutex
provider *oidc.Provider
}

// OIDCClaims is what a verified ID token says about the user.
type OIDCClaims struct {
Subject string
Email string
Name string
Groups []string
}

func NewOIDC(settings config.OIDC) *OIDC {
return &OIDC{settings: settings}
}

func (o *OIDC) discover(ctx context.Context) (*oidc.Provider, error) {
o.mutex.Lock()
defer o.mutex.Unlock()

if o.provider == nil {
provider, err := oidc.NewProvider(ctx, o.settings.Issuer)
if err != nil {
return nil, fmt.Errorf("discover %s: %w", o.settings.Issuer, err)
}
o.provider = provider
}
return o.provider, nil
}

func (o *OIDC) client(provider *oidc.Provider, redirectURI string) *oauth2.Config {
return &oauth2.Config{
ClientID: o.settings.ClientID,
ClientSecret: o.settings.ClientSecret,
Endpoint: provider.Endpoint(),
RedirectURL: redirectURI,
Scopes: o.settings.Scopes,
}
}

// AuthURL is where the browser is sent to sign in. The state, nonce and PKCE
// verifier have to be kept by the caller and handed back to Exchange.
func (o *OIDC) AuthURL(ctx context.Context, redirectURI, state, nonce, verifier string) (string, error) {
ctx, cancel := context.WithTimeout(ctx, oidcTimeout)
defer cancel()

provider, err := o.discover(ctx)
if err != nil {
return "", err
}
return o.client(provider, redirectURI).AuthCodeURL(state, oidc.Nonce(nonce), oauth2.S256ChallengeOption(verifier)), nil
}

// Exchange redeems the code the provider redirected back with and returns the
// claims of the ID token, once its signature, issuer, audience, expiry and
// nonce have been checked.
func (o *OIDC) Exchange(ctx context.Context, redirectURI, code, verifier, nonce string) (OIDCClaims, error) {
ctx, cancel := context.WithTimeout(ctx, oidcTimeout)
defer cancel()

provider, err := o.discover(ctx)
if err != nil {
return OIDCClaims{}, err
}

token, err := o.client(provider, redirectURI).Exchange(ctx, code, oauth2.VerifierOption(verifier))
if err != nil {
return OIDCClaims{}, fmt.Errorf("exchange code: %w", err)
}
raw, ok := token.Extra("id_token").(string)
if !ok {
return OIDCClaims{}, errors.New("token response carries no id_token")
}

idToken, err := provider.Verifier(&oidc.Config{ClientID: o.settings.ClientID}).Verify(ctx, raw)
if err != nil {
return OIDCClaims{}, fmt.Errorf("verify id_token: %w", err)
}
if idToken.Nonce != nonce {
return OIDCClaims{}, errors.New("id_token nonce does not match")
}

// The subject is what accounts are matched on, and the verifier lets an
// empty one through: everyone carrying it would share one account.
if idToken.Subject == "" {
return OIDCClaims{}, errors.New("id_token carries no subject")
}

var fields struct {
Email string `json:"email"`
Name string `json:"name"`
PreferredUsername string `json:"preferred_username"`
}
var all map[string]any
if err := idToken.Claims(&fields); err != nil {
return OIDCClaims{}, err
}
if err := idToken.Claims(&all); err != nil {
return OIDCClaims{}, err
}

email := strings.ToLower(strings.TrimSpace(fields.Email))
if email == "" {
return OIDCClaims{}, errors.New("id_token carries no email")
}
name := cmp.Or(strings.TrimSpace(fields.Name), strings.TrimSpace(fields.PreferredUsername), email)

return OIDCClaims{
Subject: idToken.Subject,
Email: email,
Name: name,
Groups: stringList(all[o.settings.GroupsClaim]),
}, nil
}

// stringList reads a groups claim, which providers send either as an array of
// strings or, with a single group, as a plain string.
func stringList(value any) []string {
switch typed := value.(type) {
case string:
return []string{typed}
case []any:
var entries []string
for _, entry := range typed {
if text, ok := entry.(string); ok {
entries = append(entries, text)
}
}
return entries
}
return nil
}
Loading