scim

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2026 License: Apache-2.0 Imports: 18 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package scim is a SCIM 2.0 service provider (RFC 7643, RFC 7644) over a directory.Directory: a plain http.Handler serving /Users, /Groups, /ServiceProviderConfig, /ResourceTypes and /Schemas, so an identity provider such as Microsoft Entra ID or Okta can create, update, deactivate and delete the accounts and groups it manages.

Trust model

A SCIM client is a provisioning source. It authenticates with a bearer token of its own (TokenPrefix followed by random bytes, stored only as its apikey hash in a TokenStore), and its name is the source every row it creates is stamped with (external_source). All of its work goes through a directory.Provisioner for that source, which:

  • reads, changes and deletes only the users and groups stamped with its source, so it never reaches a locally created row or another client's;
  • never sets roles, passwords or sign-in identities, and has no operation on roles, bindings, teams, projects or service accounts;
  • skips the grant check on membership of its own groups: the identity provider decides who is in the groups it manages, and the role bindings an operator makes on those groups decide what membership grants;
  • names a new group so that no stored group holds the name and no binding names it, so a new group inherits no grant made for another.

Deactivating an account (active=false) disables it through the directory, which ends its sessions and kills its tokens; DELETE deletes it, or disables it with Config.DeleteDisables.

What the store holds

A user keeps userName, one email (the primary one, else the first), active and externalId; groups is read-only. A group keeps displayName, externalId and members. Every other core attribute (name, displayName and nickName of a user, phoneNumbers, addresses, roles, password, …) and every extension attribute is accepted on write and not stored, so a client's default attribute mapping works unchanged. A group's name, which bindings refer to, is derived from its first displayName and does not follow later renames.

Filters support eq, ne, co, sw, ew, gt, ge, lt, le, pr, and, or, not, grouping and value paths, over id, userName, externalId and emails (value, type, primary) for users, and id, displayName, externalId and members.value for groups; anything else is invalidFilter. PATCH supports add, replace and remove with attribute paths, value paths with an optional sub-attribute, and no path at all, with case-insensitive operation names and boolean strings for active.

Out of scope: bulk operations, sorting, ETags, /Me, POST .search, password changes, and the admin API that creates and revokes clients, which is the product's (CreateClient and a TokenStore are its building blocks).

Index

Constants

View Source
const TokenPrefix = "scim_"

TokenPrefix starts every client token, so a leaked one is recognisable in logs and secret scanners.

Variables

View Source
var (
	// ErrClientExists reports a create for a name a client holds or held: a revoked client's name is not issued again.
	ErrClientExists = errors.New("scim: client exists")
	// ErrNoClient reports an operation on a client that does not exist.
	ErrNoClient = errors.New("scim: no such client")
	// ErrClientRevoked reports a rotation of a revoked client, whose name is never issued a token again.
	ErrClientRevoked = errors.New("scim: client is revoked")
)
View Source
var ErrRefused = errors.New("scim: refused")

ErrRefused is wrapped by a Config.CheckDeactivate error that refuses the write. Its message is the client's to read, so it names no other account.

Functions

func ClientFrom

func ClientFrom(ctx context.Context) (string, bool)

ClientFrom returns the name of the client the request authenticated as, for code running under the handler.

Types

type Client

type Client struct {
	Name string
	// TokenHash is apikey.Hash of the token; the token itself is never stored.
	TokenHash string
	// TokenPrefix is the start of the token, for telling tokens apart in a list.
	TokenPrefix string
	CreatedAt   time.Time
	// LastUsedAt is when the token last authenticated; zero for never.
	LastUsedAt time.Time
	Revoked    bool
}

Client is one provisioning source with its bearer token. Its name is the source stamped on every row it creates.

func CreateClient

func CreateClient(ctx context.Context, store TokenStore, name string) (token string, c *Client, err error)

CreateClient mints a token for a new client named name and stores the client. The token is returned once and kept nowhere. The name must be a DNS-1123 label, as it is stamped on every row the client writes.

func RotateClient

func RotateClient(ctx context.Context, store TokenStore, name string) (token string, c *Client, err error)

RotateClient mints a new token for the live client named name and stores it in place of the old one, which stops working at once. The token is returned once and kept nowhere.

type Config

type Config struct {
	Directory *directory.Directory
	Tokens    TokenStore
	// DeleteDisables makes DELETE /Users/{id} disable the account instead of deleting it, keeping its history and bindings.
	DeleteDisables bool
	// BaseURL is the external URL the handler is mounted at, such as https://example.com/scim/v2. It fills meta.location and $ref; empty leaves them out.
	BaseURL string
	// OnWrite hears every write a client attempted, after it ran, with its error; nil drops them.
	OnWrite func(ctx context.Context, w Write)
	// CheckDeactivate runs before a client disables or deletes an enabled account, so a product can keep a rule such as one enabled admin. An error wrapping ErrRefused refuses the write with 409; any other error is a 500. Nil allows every one.
	CheckDeactivate func(ctx context.Context, u *directory.User) error
	// Logger receives internal errors; nil is slog.Default().
	Logger *slog.Logger
}

Config wires a Handler.

type Error

type Error struct {
	Status int
	// Type is the scimType, empty for errors RFC 7644 gives none.
	Type   string
	Detail string
}

Error is a SCIM error response.

func (*Error) Error

func (e *Error) Error() string

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler is the SCIM service provider. It serves paths relative to its mount point, so mount it under a prefix with http.StripPrefix.

func New

func New(cfg Config) (*Handler, error)

New returns a handler over cfg; Directory and Tokens are required.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type TokenStore

type TokenStore interface {
	// ClientByTokenHash returns the client whose token hashes to hash, revoked or not, or (nil, nil).
	ClientByTokenHash(ctx context.Context, hash string) (*Client, error)
	// MarkUsed records that the client authenticated at t. A store may skip writes closer together than it cares to keep.
	MarkUsed(ctx context.Context, name string, t time.Time) error
	// CreateClient stores c. A name any client holds, revoked or not, is ErrClientExists: the name is the source stamped on the rows a client provisions, so issuing it again would hand those rows to the new token.
	CreateClient(ctx context.Context, c *Client) error
	// ListClients returns every client ordered by name.
	ListClients(ctx context.Context) ([]*Client, error)
	// RevokeClient revokes the client's token for good; a missing name is ErrNoClient, and revoking twice is not an error.
	RevokeClient(ctx context.Context, name string) error
	// RotateClient replaces the token of the live client c.Name with c.TokenHash and c.TokenPrefix in one write, so the old token stops working at once; the name, and so every row it provisioned, stays. A missing name is ErrNoClient and a revoked one ErrClientRevoked.
	RotateClient(ctx context.Context, c *Client) error
}

TokenStore keeps the clients. Lookups go by token hash, so finding a client costs the same whatever the token.

type Write

type Write struct {
	Client string
	// Kind is "user" or "group".
	Kind string
	// Op is "create", "update", "disable", "enable" or "delete".
	Op   string
	ID   string
	Name string
	// Fields are the SCIM attributes an update changed.
	Fields []string
	// Err is why the write failed; a refused reach for a row the client did not provision matches directory.ErrForeign, and a write Config.CheckDeactivate refused matches ErrRefused.
	Err error
}

Write is one write a client attempted, for a product's audit log.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL