serviceauth

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package serviceauth provides shared helpers for service-to-service authentication.

Index

Constants

View Source
const (
	// ServiceTokenHeader is the header name for service-to-service tokens.
	ServiceTokenHeader = "X-Service-Token"

	// ServiceIDHeader is the header name for service identification.
	ServiceIDHeader = "X-Service-ID"

	// UserIDHeader is the header name for user identification.
	UserIDHeader = "X-User-ID"

	// DefaultServiceTokenExpiry is the default expiration time for service tokens.
	DefaultServiceTokenExpiry = 1 * time.Hour
)

Variables

This section is empty.

Functions

func GetServiceID

func GetServiceID(ctx context.Context) string

GetServiceID extracts service ID from context.

func GetUserID

func GetUserID(ctx context.Context) string

GetUserID extracts user ID from context.

func NewServiceTokenRoundTripper

func NewServiceTokenRoundTripper(base http.RoundTripper, generator *ServiceTokenGenerator) http.RoundTripper

NewServiceTokenRoundTripper wraps a base transport with service-token injection.

func ParseRSAPrivateKeyFromPEM

func ParseRSAPrivateKeyFromPEM(pemBytes []byte) (*rsa.PrivateKey, error)

ParseRSAPrivateKeyFromPEM parses an RSA private key from PEM bytes. Supported PEM types: RSA PRIVATE KEY (PKCS#1), PRIVATE KEY (PKCS#8).

func ParseRSAPublicKeyFromPEM

func ParseRSAPublicKeyFromPEM(pemBytes []byte) (*rsa.PublicKey, error)

ParseRSAPublicKeyFromPEM parses an RSA public key from PEM bytes. Supported PEM types: PUBLIC KEY (PKIX), RSA PUBLIC KEY (PKCS#1), CERTIFICATE.

func WithServiceID

func WithServiceID(ctx context.Context, serviceID string) context.Context

WithServiceID returns a new context with the service ID set.

func WithUserID

func WithUserID(ctx context.Context, userID string) context.Context

WithUserID returns a new context with the user ID set. This is useful for propagating user ID through service-to-service calls.

Types

type ServiceClaims

type ServiceClaims struct {
	ServiceID string `json:"service_id"`
	jwt.RegisteredClaims
}

ServiceClaims represents JWT claims for service-to-service authentication.

type ServiceTokenGenerator

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

ServiceTokenGenerator generates service-to-service JWT tokens.

func NewServiceTokenGenerator

func NewServiceTokenGenerator(privateKey *rsa.PrivateKey, serviceID string, expiry time.Duration) *ServiceTokenGenerator

NewServiceTokenGenerator creates a new service token generator.

func (*ServiceTokenGenerator) GenerateToken

func (g *ServiceTokenGenerator) GenerateToken() (string, error)

GenerateToken generates a new service token.

type ServiceTokenRoundTripper

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

ServiceTokenRoundTripper injects X-Service-Token (and optionally X-User-ID) into outgoing HTTP requests.

func (*ServiceTokenRoundTripper) RoundTrip

func (t *ServiceTokenRoundTripper) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements http.RoundTripper.

Jump to

Keyboard shortcuts

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