Help us shape the future of Go! Take the 2026 Go Developer Survey

session

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package session provides authenticated HTTP primitives shared by all SJTU services.

A Session couples an http.Client with one identity's credentials. It is the only layer that manages response-body lifecycles and credential redaction: callers above this package never close a body themselves unless they explicitly asked for a stream, and secrets never reach logs.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ForgetSecret

func ForgetSecret(value string)

ForgetSecret drops value from the redaction set, e.g. after logout.

func Redact

func Redact(s string) string

Redact returns s with every registered secret replaced by "<redacted>".

func RegisterSecret

func RegisterSecret(value string)

RegisterSecret adds value to the redaction set. Empty values are ignored: matching the empty string would corrupt every message. Call this whenever a credential is loaded, stored, or newly issued.

Types

type HTTPError

type HTTPError struct {
	Method string
	URL    string
	Status string
	Body   string
}

HTTPError describes a non-2xx response. The body excerpt is redacted before storage, so an HTTPError is always safe to log or print.

func (*HTTPError) Error

func (e *HTTPError) Error() string

Error formats the failure for humans.

type RedactingHandler

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

RedactingHandler wraps an slog.Handler and redacts registered secrets from every message and attribute value before forwarding. Install it as the default logger at startup so no call site can leak credentials by mistake.

func NewRedactingHandler

func NewRedactingHandler(inner slog.Handler) *RedactingHandler

NewRedactingHandler wraps inner with secret redaction.

func (*RedactingHandler) Enabled

func (h *RedactingHandler) Enabled(ctx context.Context, level slog.Level) bool

Enabled reports whether inner handles the level.

func (*RedactingHandler) Handle

func (h *RedactingHandler) Handle(ctx context.Context, rec slog.Record) error

Handle redacts the record's message and string attributes, then forwards.

func (*RedactingHandler) WithAttrs

func (h *RedactingHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs forwards to the wrapped handler.

func (*RedactingHandler) WithGroup

func (h *RedactingHandler) WithGroup(name string) slog.Handler

WithGroup forwards to the wrapped handler.

type Session

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

Session is one identity's authenticated HTTP context. Exactly one of the credential fields is active: token sessions call the Canvas API with a bearer header and ignore cookies; cookie sessions carry a jar for the jAccount SSO domain family.

func NewCookies

func NewCookies() (*Session, error)

NewCookies builds a Session with a private, initially empty cookie jar. QR login depends on this isolation: a jar shared with an earlier authenticated session makes my.sjtu.edu.cn answer with the signed-in portal page instead of the login page, and no QR UUID can be extracted.

func NewToken

func NewToken(token string) *Session

NewToken builds a Session that authenticates every request with the given Canvas bearer token.

func (*Session) ClearCookies added in v0.6.0

func (s *Session) ClearCookies(rawURL string, keep ...string)

ClearCookies expires every cookie that would be sent to rawURL — except the names in keep, which are credentials the session exists to carry. The jar offers no enumeration beyond "cookies for this URL", and deletion must match the planted (name, domain, path) triple, so each sent name is expired in all three shapes an origin can plant: host cookie, host-domain cookie, and parent-domain cookie. SSO chains re-run from a clean slate: stale session residue makes mlearning answer its forscan entry with an immediate 500 instead of starting the redirect chain.

func (*Session) Cookie

func (s *Session) Cookie(rawURL, name string) (string, bool)

Cookie returns the value of a cookie currently held for rawURL, or false.

func (*Session) DoJSON

func (s *Session) DoJSON(ctx context.Context, method, rawURL string, out any) error

DoJSON performs a JSON request and fully manages the response body: the caller receives the value decoded into out, or an error, and never an open body. out may be nil for requests whose payload is irrelevant.

func (*Session) DoJSONHeader

func (s *Session) DoJSONHeader(ctx context.Context, method, rawURL string, out any) (http.Header, error)

DoJSONHeader behaves like DoJSON but also returns the response headers, which Canvas list pagination needs for the Link header.

func (*Session) DoJSONWith

func (s *Session) DoJSONWith(ctx context.Context, method, rawURL string, header http.Header, out any) error

DoJSONWith behaves like DoJSON but carries extra request headers; the v.sjtu.edu.cn API authenticates per-request with a jwt-token header.

func (*Session) DoStream

func (s *Session) DoStream(ctx context.Context, method, rawURL string) (*http.Response, error)

DoStream performs a request whose body the CALLER must close. It is reserved for large or unbounded payloads (video download, live streams); everything else belongs in DoJSON.

func (*Session) GetHTML

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

GetHTML fetches one HTML page, following redirects. It returns the body and the final URL after redirection, which form actions resolve against.

func (*Session) Jar

func (s *Session) Jar() http.CookieJar

Jar exposes the cookie jar for the WebSocket dialer, which needs it to attach the login session to its handshake. Nil on token sessions.

func (*Session) PostFormHTML

func (s *Session) PostFormHTML(ctx context.Context, rawURL string, form url.Values) ([]byte, string, error)

PostFormHTML submits one form, following redirects, and returns the resulting page body and final URL.

func (*Session) PostFormRedirect

func (s *Session) PostFormRedirect(ctx context.Context, rawURL string, form url.Values) (string, error)

PostFormRedirect submits one form WITHOUT following redirects and returns the raw Location header of the 3xx response. Any other status is an error. The LTI launch chain carries its token in that redirect, so following it would lose exactly the value being extracted.

func (*Session) SeedCookie

func (s *Session) SeedCookie(name, value string, domains ...string)

SeedCookie plants a JAAuthCookie restored from the credential store. Each domain is passed as a bare host ("jaccount.sjtu.edu.cn").

Jump to

Keyboard shortcuts

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