Documentation
¶
Overview ¶
Package auth performs the JumpServer dual login and MFA handling.
KoKo's WebSocket terminal only accepts the jms_sessionid cookie from a Django form login, while the REST API needs a Bearer token — so a usable session needs both (DESIGN.md「登录双流程」). The REST transport itself lives in internal/api.
Dependency direction: auth depends on api and config.
Index ¶
Constants ¶
const ( PathAPILogin = "/api/v1/authentication/auth/" PathMFAChallenge = "/api/v1/authentication/mfa/challenge/" PathFormLogin = "/core/auth/login/" )
Endpoint paths used by the dual login (DESIGN.md「登录双流程」).
const ( CookieSession = "jms_sessionid" CookieCSRF = "jms_csrftoken" HeaderCSRF = "X-CSRFToken" )
Cookie and header names.
Variables ¶
var ( // ErrMFANotProvided is returned when the server demands MFA but no // TOTP secret is configured and no prompt callback was supplied. ErrMFANotProvided = errors.New("MFA required but no TOTP secret configured") // ErrMissingSessionCookie is returned when the form login completed // without yielding a jms_sessionid cookie. ErrMissingSessionCookie = errors.New("form login returned no session cookie") )
Sentinel errors. Callers should test them with errors.Is.
Functions ¶
func GenerateCode ¶
GenerateCode returns the current TOTP code for a base32 secret.
It is exported so the CLI can validate a secret at entry time (`jms config add`) and so tests can assert against a known code.
func KeyOptions ¶
func KeyOptions() totp.ValidateOpts
KeyOptions returns the TOTP parameters used when provisioning a secret. Retained for the `jms config add --provision-otp` flow (post-M1).
func ValidateSecret ¶
ValidateSecret reports whether secret is a usable base32 TOTP secret.
An empty or whitespace-only secret is rejected here rather than handed to pquerna/otp, which happily derives a code from the empty string: that would let a missing secret silently bypass the interactive prompt and send a bogus challenge (DESIGN.md 3.4 / 7.3).
Types ¶
type Credentials ¶
Credentials carries the secret material for one login attempt. It is deliberately separate from config.ServerConfig: the server entry holds metadata only.
type Session ¶
type Session struct {
Server *config.ServerConfig
Client *api.Client
BaseURL string
// Proxy is the resolved proxy policy the session was established
// with. Every later connection derived from this session (KoKo
// WebSocket, SSH, SFTP) must use the same policy, or the failover
// policy in 「端点故障转移」 would reason about a different network than the
// login it observed (DESIGN.md「代理策略」).
Proxy netproxy.Proxy
}
Session is an authenticated connection to one JumpServer address.
The address is bound for the session's whole lifetime: REST calls and (in later milestones) the KoKo connection both derive from BaseURL (DESIGN.md「端点故障转移」).
func Login ¶
func Login(ctx context.Context, srv *config.ServerConfig, baseURL string, creds Credentials, otpPrompt func() (string, error)) (*Session, error)
Login performs the full dual login against baseURL over a direct connection.
It is LoginWithProxy with the direct policy, which is the default (DESIGN.md「代理策略」): jms never reads HTTP_PROXY/HTTPS_PROXY on its own.
func LoginWithProxy ¶
func LoginWithProxy(ctx context.Context, srv *config.ServerConfig, baseURL string, creds Credentials, otpPrompt func() (string, error), px netproxy.Proxy) (*Session, error)
LoginWithProxy performs the full dual login against baseURL:
- POST /api/v1/authentication/auth/ for the Bearer token, retrying through /api/v1/authentication/mfa/challenge/ when the server asks for MFA
- GET then POST /core/auth/login/ for the jms_sessionid cookie
otpPrompt is consulted only when the server demands MFA and no TOTP secret is available; it may be nil, in which case a missing secret surfaces as ErrMFANotProvided.
Any failure during the login flow is reported as an *api.AuthError, so callers can distinguish "this endpoint rejects the credential" from "this endpoint is unreachable" (DESIGN.md「端点故障转移」).
The proxy policy is recorded on the session so every transport derived from it stays on the same network path.
func (*Session) NetProxy ¶
NetProxy returns the proxy policy this session was established with.
A session built outside the login flow (tests, or a hand-assembled session) has the zero value, which is the direct policy. Every transport derived from a session must dial through this policy so that probe, login, KoKo WebSocket, SSH and SFTP all measure one network (DESIGN.md「代理策略」).