oidc

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const Provider = "oidc"

Provider is the identity.Principal.Provider value for OIDC-authenticated principals.

Variables

View Source
var (
	ErrNoRolesMapped   = errors.New("oidc: no internal roles mapped for this user")
	ErrGroupNotAllowed = errors.New("oidc: user is not a member of any allowed group")
	ErrInvalidState    = errors.New("oidc: invalid or missing state parameter")
)

Sentinel errors returned by Service.

Functions

func ConsumePKCECookie

func ConsumePKCECookie(w http.ResponseWriter, r *http.Request, secure bool) (string, bool)

ConsumePKCECookie reads the PKCE verifier cookie and immediately clears it. Returns ("", false) when the cookie is absent.

func ConsumeStateCookie

func ConsumeStateCookie(w http.ResponseWriter, r *http.Request, secure bool) (string, bool)

ConsumeStateCookie reads the state cookie and immediately clears it by setting MaxAge = -1. Returns ("", false) when the cookie is absent.

func GeneratePKCE

func GeneratePKCE() (verifier, challenge string, err error)

GeneratePKCE returns a PKCE (RFC 7636) verifier and its S256 challenge. verifier is stored server-side (in a cookie); challenge is sent to the provider.

func GenerateState

func GenerateState() (string, error)

GenerateState returns a cryptographically secure random state string for inclusion in the OIDC authorization request.

func MapExternalRoles

func MapExternalRoles(external []string, mappings []RoleMapping) []string

MapExternalRoles maps external group identifiers to MIDAS canonical roles using explicit mappings only. Unknown groups are silently ignored. Output is deduplicated, deterministically sorted, and passed through identity.NormalizeRoles to ensure canonical form.

func SetPKCECookie

func SetPKCECookie(w http.ResponseWriter, verifier string, secure bool)

SetPKCECookie stores the PKCE verifier in an HttpOnly cookie.

func SetStateCookie

func SetStateCookie(w http.ResponseWriter, state string, secure bool)

SetStateCookie writes the CSRF state to an HttpOnly cookie. The cookie expires after 10 minutes — sufficient for any interactive login.

Types

type Claims

type Claims struct {
	Subject  string
	Username string
	Groups   []string
	Raw      map[string]interface{}
}

Claims holds the extracted identity claims from an ID token.

type Config

type Config struct {
	ProviderName string

	IssuerURL string
	AuthURL   string
	TokenURL  string

	ClientID     string
	ClientSecret string
	RedirectURL  string

	Scopes []string

	SubjectClaim  string
	UsernameClaim string
	GroupsClaim   string

	DomainHint    string
	AllowedGroups []string

	RoleMappings []RoleMapping

	DenyIfNoRoles bool
	UsePKCE       bool
}

Config holds runtime OIDC configuration passed to NewService. This mirrors config.PlatformOIDCConfig; main.go converts between them.

type RoleMapping

type RoleMapping struct {
	External string
	Internal string
}

RoleMapping maps a single external group identifier to a MIDAS canonical role.

type Service

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

Service provides OIDC authorization code flow for platform/Explorer login. It handles the redirect, code exchange, token validation, and principal construction. Session creation is delegated to localiam (see httpapi).

func NewService

func NewService(ctx context.Context, cfg Config) (*Service, error)

NewService initialises an OIDCService by performing OIDC discovery against cfg.IssuerURL. Returns an error if discovery fails or the config is invalid. The provided context is used only during initialisation.

func (*Service) AuthURL

func (s *Service) AuthURL(state, pkceChallenge string) string

AuthURL returns the authorization URL to redirect the user to. state is the CSRF token. pkceChallenge is included when cfg.UsePKCE is true (pass an empty string to skip PKCE regardless of config).

func (*Service) BuildPrincipal

func (s *Service) BuildPrincipal(claims *Claims) (*identity.Principal, error)

BuildPrincipal converts OIDC claims to a MIDAS *identity.Principal. It enforces AllowedGroups and DenyIfNoRoles per configuration. Returns ErrGroupNotAllowed or ErrNoRolesMapped on policy denial.

func (*Service) Exchange

func (s *Service) Exchange(ctx context.Context, code, pkceVerifier string) (*Claims, error)

Exchange exchanges the authorization code for tokens, validates the ID token, extracts claims, and returns a populated Claims value. pkceVerifier is included in the exchange when non-empty.

func (*Service) UsePKCE

func (s *Service) UsePKCE() bool

UsePKCE reports whether PKCE is configured.

Jump to

Keyboard shortcuts

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