auth

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 14 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 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

View Source
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「登录双流程」).

View Source
const (
	CookieSession = "jms_sessionid"
	CookieCSRF    = "jms_csrftoken"
	HeaderCSRF    = "X-CSRFToken"
)

Cookie and header names.

Variables

View Source
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

func GenerateCode(secret string) (string, error)

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

func ValidateSecret(secret string) error

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

type Credentials struct {
	Password  string
	OTPSecret string
}

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:

  1. POST /api/v1/authentication/auth/ for the Bearer token, retrying through /api/v1/authentication/mfa/challenge/ when the server asks for MFA
  2. 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) Bearer

func (s *Session) Bearer() string

Bearer returns the API bearer token, or "" before a successful login.

func (*Session) CSRF

func (s *Session) CSRF() string

CSRF returns the current jms_csrftoken cookie value, or "".

func (*Session) NetProxy

func (s *Session) NetProxy() netproxy.Proxy

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「代理策略」).

func (*Session) SessionID

func (s *Session) SessionID() string

SessionID returns the current jms_sessionid cookie value, or "".

Jump to

Keyboard shortcuts

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