session

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2026 License: MIT Imports: 21 Imported by: 0 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 session manages tuned HTTP transport, strict host-boundary redirect validation, IServ form/OIDC authentication handoffs, and authenticated requests.

Index

Constants

View Source
const (
	// DefaultTimeout is the default per-request HTTP timeout.
	DefaultTimeout = 20 * time.Second
	// DefaultMaxBodyBytes is the default maximum buffered response size (10 MiB).
	DefaultMaxBodyBytes int64 = 10 * 1024 * 1024
	// DefaultUserAgent is the default User-Agent header sent by the SDK.
	DefaultUserAgent = "iserv-go/0.2"
)

Variables

This section is empty.

Functions

func BuildTargetURL

func BuildTargetURL(baseURL, relPath string) string

BuildTargetURL resolves relPath against baseURL while avoiding duplicate subpath mount prefixes (e.g., baseURL "https://host/school" + "/school/iserv/...").

func DrainAndClose

func DrainAndClose(body io.ReadCloser)

DrainAndClose drains a bounded remainder of body and closes it so HTTP/1.1 keep-alive connections remain reusable.

func DrainBody

func DrainBody(body io.Reader)

DrainBody drains a bounded remainder of body so HTTP/1.1 keep-alive connections remain reusable before the caller closes the body.

func HasKind

func HasKind(err error, kinds ...ErrorKind) bool

HasKind reports whether err contains an *UpstreamError matching any of kinds.

func IsForbidden

func IsForbidden(err error) bool

IsForbidden reports whether err represents an HTTP 403 forbidden error.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound reports whether err represents an HTTP 404 resource not found error.

func IsProtocolOrValidation

func IsProtocolOrValidation(err error) bool

IsProtocolOrValidation reports whether err represents an upstream protocol or validation rejection.

func IsUnauthorized

func IsUnauthorized(err error) bool

IsUnauthorized reports whether err represents a session-expired / unauthorized condition.

func MapStatus

func MapStatus(code int, targetURL string) error

MapStatus translates an upstream HTTP status code into nil or an *UpstreamError.

func NewDefaultTransport

func NewDefaultTransport() *http.Transport

NewDefaultTransport constructs a production-tuned HTTP transport with TLS 1.2+, elevated per-host idle connections, and bounded handshake/header timeouts.

func NewRedirectPolicy

func NewRedirectPolicy(allowedHost string, allowInsecureHTTP bool) func(req *http.Request, via []*http.Request) error

NewRedirectPolicy enforces same-host redirect boundaries, prevents HTTPS-to-HTTP downgrades, caps redirect depth, and strips Authorization headers across origin changes.

func ReadLimited

func ReadLimited(r io.Reader, limit int64) ([]byte, error)

ReadLimited reads at most limit bytes from r, returning KindTooLarge on overflow.

Types

type ErrorKind

type ErrorKind int

ErrorKind categorizes upstream transport and protocol errors without coupling internal packages to the root package.

const (
	KindUnknown ErrorKind = iota
	KindValidation
	KindUnauthorized
	KindInvalidCredentials
	KindForbidden
	KindNotFound
	KindUnavailable
	KindProtocol
	KindTooLarge
)

Canonical ErrorKind classifications returned by UpstreamError.

type Factory

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

Factory constructs per-session HTTP clients sharing a tuned transport and performs form + OIDC authentication against IServ portals.

func NewFactory

func NewFactory(cfg FactoryConfig) *Factory

NewFactory creates a Factory with secure TLS, connection pooling, and silent default logging via slog.DiscardHandler.

func (*Factory) Authenticate

func (f *Factory) Authenticate(ctx context.Context, creds credentials.Credentials) (*Session, error)

Authenticate signs into IServ using the web form flow (plus any OIDC handoff steps) and returns a live Session.

func (*Factory) Logout

func (f *Factory) Logout(ctx context.Context, sess *Session) error

Logout terminates the upstream IServ session on a best-effort basis.

type FactoryConfig

type FactoryConfig struct {
	Transport    http.RoundTripper
	Timeout      time.Duration
	MaxBodyBytes int64
	UserAgent    string
	Logger       *slog.Logger
}

FactoryConfig configures a Factory.

type Session

type Session struct {
	ID           string
	Credentials  credentials.Credentials
	HTTPClient   *http.Client
	MaxBodyBytes int64
	UserAgent    string
	Logger       *slog.Logger
	CreatedAt    time.Time
}

Session represents an authenticated IServ upstream session with its own isolated cookie jar and HTTP client.

func (*Session) Get

func (s *Session) Get(ctx context.Context, relPath string) (*http.Response, error)

Get sends an authenticated GET request for HTML or binary resources. Callers must close resp.Body when finished.

func (*Session) GetBytes

func (s *Session) GetBytes(ctx context.Context, relPath string, limitBytes int64) ([]byte, string, error)

GetBytes performs an authenticated GET and reads up to limitBytes (or MaxBodyBytes).

func (*Session) GetHTML

func (s *Session) GetHTML(ctx context.Context, relPath string) (*goquery.Document, error)

GetHTML performs an authenticated GET and parses the HTML response body into a goquery document.

func (*Session) GetJSON

func (s *Session) GetJSON(ctx context.Context, relPath string, dst any) error

GetJSON performs an authenticated AJAX GET and decodes JSON into dst.

func (*Session) GetJSONBytes

func (s *Session) GetJSONBytes(ctx context.Context, relPath string) ([]byte, string, error)

GetJSONBytes performs an authenticated AJAX GET (Accept: application/json + X-Requested-With).

func (*Session) Log

func (s *Session) Log() *slog.Logger

Log returns the session's structured logger (never nil).

func (*Session) MaxBody

func (s *Session) MaxBody() int64

MaxBody returns the effective maximum response body size in bytes.

func (*Session) PostJSON

func (s *Session) PostJSON(ctx context.Context, relPath string, payload []byte) ([]byte, string, error)

PostJSON performs an authenticated AJAX POST against relPath.

type UpstreamError

type UpstreamError struct {
	Kind       ErrorKind
	Code       string
	Message    string
	StatusCode int
	URL        string
	Details    map[string]string
	Cause      error
}

UpstreamError captures structured upstream HTTP, protocol, or auth failures.

func (*UpstreamError) Error

func (e *UpstreamError) Error() string

Error implements the error interface.

func (*UpstreamError) Unwrap

func (e *UpstreamError) Unwrap() error

Unwrap returns the underlying cause when present.

Jump to

Keyboard shortcuts

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