auth

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: MIT Imports: 24 Imported by: ? 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 auth implements USC's shared SSO flow without exposing secret-bearing values.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrCredentialsRequired means the saved session cannot reach the target
	// and no complete credentials were supplied for a fresh USC login.
	ErrCredentialsRequired = errors.New("USC sign-in is required")
	// ErrPasswordRejected means USC did not accept the username and password.
	ErrPasswordRejected = errors.New("USC rejected the username or password")
	// ErrBypassRejected means Duo did not accept the supplied bypass code.
	ErrBypassRejected = errors.New("Duo rejected the bypass code")
	// ErrSessionInvalid means a site stopped accepting its session after the
	// client was opened. Open a new client.
	ErrSessionInvalid = errors.New("session expired")
	// ErrSessionNotFound means there is no saved session file.
	ErrSessionNotFound = errors.New("no saved session")
)

Functions

func ForSite

func ForSite(name site.Name, err error) error

ForSite attributes an authentication failure to a site. Other errors, and errors already attributed, pass through unchanged.

func SaveCredentials

func SaveCredentials(file string, credentials Credentials) error

SaveCredentials writes a credentials file readable only by its owner.

Types

type BypassCode added in v0.1.1

type BypassCode struct {
	Code             string `json:"code"`
	Validity         string `json:"validity,omitempty"`
	CredentialsSaved bool   `json:"credentials_saved"`
}

BypassCode contains a secret. Callers must not log it.

func CreateBypassCode added in v0.1.1

func CreateBypassCode(ctx context.Context, options Options) (*BypassCode, error)

CreateBypassCode requests a Duo bypass code from USC Account Services. Issuance may invalidate earlier codes. It never prompts or retries issuance. If CredentialsFile is set, the resolved credentials are saved with the new code before session persistence. Otherwise the caller owns the returned code. Validity is USC's description, not an independently verified expiration time. A non-nil result may accompany a persistence error after issuance succeeded.

type CookiePolicy

type CookiePolicy struct {
	ResetDomains    []string // Skip these domains' saved cookies; replace them on save.
	PreserveCookies []string // Cookie names within ResetDomains to leave untouched on disk.
}

CookiePolicy makes a session start over at some sites each time it is opened. LibCal needs this: a stale checkout cookie breaks a new booking.

type Credentials

type Credentials struct {
	Username   string // USC NetID
	Password   string // USC NetID password
	BypassCode string // current Duo bypass code
}

Credentials are used only when an existing session cannot reach the target.

func LoadCredentials

func LoadCredentials(file string) (Credentials, error)

LoadCredentials reads a credentials file written by SaveCredentials. A missing file yields empty credentials.

func (Credentials) Complete

func (c Credentials) Complete() bool

Complete reports whether a fresh SSO login can proceed.

type Options

type Options struct {
	// Credentials are submitted only when the session's cookies are not enough.
	// Fields set here override those read from CredentialsFile.
	Credentials Credentials
	// CredentialsFile supplies any credentials not set above. A secret from
	// it that USC rejects is removed from the file so it is never submitted
	// twice.
	CredentialsFile string
	// SessionFile stores the session's cookies. Empty keeps them in memory.
	SessionFile string
}

Options is how a program signs in. The zero value has no credentials and keeps its cookies in memory, so it can only reach public pages.

Supply credentials either directly or as a credentials file written by usc auth login (config.CredentialsPath is the usc command's). Set SessionFile to keep cookies between runs; a program owns its session file and should not share one with the usc command or with another running program.

func (Options) ResolveCredentials

func (o Options) ResolveCredentials() (Credentials, error)

ResolveCredentials returns the credentials a login would submit: the explicit ones, completed from CredentialsFile.

type Session

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

Session sends requests with USC browser cookies. Do never reauthenticates; Authenticate is the explicit SSO handoff.

func Open

func Open(ctx context.Context, name site.Name, options Options) (*Session, error)

Open signs in to a site through USC SSO and returns its session. It never prompts. Authentication failures are returned as *SiteError.

func OpenSession

func OpenSession(options Options, policy CookiePolicy) (*Session, error)

OpenSession restores the cookies in options.SessionFile, if any. Nothing is written until Authenticate succeeds.

func (*Session) Authenticate

func (s *Session) Authenticate(ctx context.Context, target, referer string) (*http.Response, error)

Authenticate opens target, completing USC SSO with the session's credentials if its cookies are not enough, then saves the cookies. The caller owns the final response body, including when saving returns an error.

func (*Session) Do

func (s *Session) Do(request *http.Request) (*http.Response, error)

Do implements the small HTTP contract used by site clients.

type SiteError

type SiteError struct {
	Site site.Name
	Err  error
}

SiteError is an authentication failure at one site. It unwraps to ErrCredentialsRequired, ErrPasswordRejected, ErrBypassRejected, or ErrSessionInvalid.

func (*SiteError) Error

func (e *SiteError) Error() string

func (*SiteError) Unwrap

func (e *SiteError) Unwrap() error

type StoredCookie

type StoredCookie struct {
	SetBy  string      `json:"set_by"`
	Cookie http.Cookie `json:"cookie"`
}

StoredCookie is a session cookie plus the URL that set it, which is needed to restore host-only and path-scoped cookies. Never log Cookie.Value.

func LoadStoredCookies

func LoadStoredCookies(sessionFile string) ([]StoredCookie, error)

LoadStoredCookies reads the cookies in a saved session file. A missing file returns ErrSessionNotFound.

Jump to

Keyboard shortcuts

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