auth

package
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: Apache-2.0 Imports: 25 Imported by: 0

Documentation

Overview

Package auth provides authentication and authorization middleware for gRPC.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrDecodePEM is returned when the PEM block cannot be decoded.
	ErrDecodePEM = errors.New("failed to decode PEM block")
	// ErrNotECDSA is returned when the public key is not ECDSA.
	ErrNotECDSA = errors.New("public key is not ECDSA")
	// ErrInvalidToken is returned when the token is not valid.
	ErrInvalidToken = errors.New("invalid token")
	// ErrTokenProcessing is returned when the token claims cannot be read.
	ErrTokenProcessing = errors.New("failed to process token")
	// ErrMissingUserID is returned when the token does not contain a user ID.
	ErrMissingUserID = errors.New("missing user ID in token")
)
View Source
var (
	// ErrNilPublicKey is returned when a nil public key is passed to JWKS.
	ErrNilPublicKey = errors.New("nil public key")
	// ErrNotP521 is returned when the public key is not on the P-521 curve.
	ErrNotP521 = errors.New("expected P-521 curve")
	// ErrUnexpectedKeyLength is returned when the encoded public key has an unexpected length.
	ErrUnexpectedKeyLength = errors.New("unexpected key length")
)
View Source
var (
	// ErrDecodeKeyData is returned when the private key PEM block cannot be decoded.
	ErrDecodeKeyData = errors.New("failed to decode key data")
	// ErrWrongKeyType is returned when the private key is not a PRIVATE KEY block.
	ErrWrongKeyType = errors.New("key is of the wrong type")
)
View Source
var ErrMissingOAuthInput = errors.New("auth: missing oauth access input field")

ErrMissingOAuthInput is returned when a required OAuthAccessInput field is empty.

View Source
var ErrUserNotFound = errors.New("user not found")

ErrUserNotFound is returned when an update affects no user rows.

Functions

func JWKS

func JWKS(pub *ecdsa.PublicKey) ([]byte, string, error)

JWKS renders a single-key JWKS for the given P-521 public key. Returns the JSON body, the deterministic kid (sha256 over X||Y, base64url-no-pad), and any error.

func MintOAuthAccess

func MintOAuthAccess(ctx context.Context, in OAuthAccessInput) (string, error)

MintOAuthAccess returns a fresh signed JWT carrying the OAuth-standard iss/sub/aud/scope claims in addition to the existing user_id claim the gRPC interceptor reads.

func UpdateUserTimezone

func UpdateUserTimezone(ctx context.Context, id uuid.UUID, timezone string) error

UpdateUserTimezone sets users.timezone for the given user. Caller must have already validated the IANA name (see internal/timezones.Validate).

Types

type Interceptor

type Interceptor struct {
	PublicKeyFile string
}

Interceptor holds configuration for gRPC authentication middleware.

func (*Interceptor) Stream

func (i *Interceptor) Stream(
	srv any,
	ss grpc.ServerStream,
	info *grpc.StreamServerInfo,
	handler grpc.StreamHandler,
) error

Stream is the stream middleware that checks authentication.

func (*Interceptor) Unary

func (i *Interceptor) Unary(
	ctx context.Context,
	req any,
	info *grpc.UnaryServerInfo,
	handler grpc.UnaryHandler,
) (any, error)

Unary is the unary middleware that checks authentication.

type OAuthAccessInput

type OAuthAccessInput struct {
	Issuer         string
	Audience       string
	Scope          string
	PrivateKeyFile string
	Lifetime       time.Duration
	UserID         uuid.UUID
}

OAuthAccessInput holds the fields used by MintOAuthAccess.

type Session

type Session struct {
	Token string
}

Session contains a session token.

func Login

func Login(ctx context.Context, username, password, privateKeyFile string) (Session, error)

Login takes a username and password, authenticates the user, and returns a session object.

func MintSession

func MintSession(ctx context.Context, userID uuid.UUID, privateKeyFile string) (Session, error)

MintSession returns a fresh signed JWT for the given user. Used both by password Login and by passkey assertion success. Caller is responsible for having authenticated the user.

type User

type User struct {
	CreatedAt      time.Time `db:"created_at" json:"created_at"`
	UpdatedAt      time.Time `db:"updated_at" json:"updated_at"`
	Timezone       *string   `db:"timezone" json:"timezone,omitempty"`
	Username       string    `db:"username" json:"username"`
	Password       string    `db:"-" json:"password"`
	HashedPassword []byte    `db:"hashed_password"`
	ID             uuid.UUID `db:"id" json:"id"`
}

User represents a single system user.

func CreateUser

func CreateUser(ctx context.Context, user *User) (*User, error)

CreateUser adds a new user record to the database and populates the new ID.

func GetUserByID

func GetUserByID(ctx context.Context, id uuid.UUID) (User, error)

GetUserByID returns a persisted user given a UUID.

func GetUserByUsername

func GetUserByUsername(ctx context.Context, username string) (User, error)

GetUserByUsername returns a persisted user given a username.

Jump to

Keyboard shortcuts

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