Documentation
¶
Overview ¶
Package auth implements USC's shared SSO flow without exposing secret-bearing values.
Index ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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 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 ¶
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 ¶
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.
type SiteError ¶
SiteError is an authentication failure at one site. It unwraps to ErrCredentialsRequired, ErrPasswordRejected, ErrBypassRejected, or ErrSessionInvalid.
type StoredCookie ¶
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.