session

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: MPL-2.0 Imports: 12 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 session signs the customer in on the web (OAuth 2.0 Device Authorization Grant against Keycloak), keeps the tokens on disk, refreshes them and revokes them on logout.

Index

Constants

View Source
const ClientId = "openvicloud-cli"

ClientId is the public Keycloak client of the CLI (gitops/keycloak/realm-import/configmap.yaml).

Variables

View Source
var (
	ErrLoginDenied    = errors.New("the login was refused on the web page")
	ErrLoginExpired   = errors.New("the login code expired before it was confirmed on the web page")
	ErrSessionExpired = errors.New("the session has expired or was revoked")
	ErrRevokeFailed   = errors.New("the server did not confirm the session was revoked")
)
View Source
var ErrNotLoggedIn = errors.New("not logged in")
View Source
var ErrOtherEndpoint = errors.New("the saved session belongs to another endpoint")

Functions

func DefaultDir

func DefaultDir(getenv func(string) string) (string, error)

DefaultDir is $XDG_CONFIG_HOME/openvicloud, else ~/.config/openvicloud on every OS.

Types

type DeviceCode

type DeviceCode struct {
	DeviceCode              string `json:"device_code"`
	UserCode                string `json:"user_code"`
	VerificationUri         string `json:"verification_uri"`
	VerificationUriComplete string `json:"verification_uri_complete"`
	ExpiresIn               int    `json:"expires_in"`
	Interval                int    `json:"interval"`
}

DeviceCode is what Keycloak hands out to start a login: the user opens VerificationUriComplete (or VerificationUri and types UserCode) while the CLI polls with DeviceCode.

type Endpoints

type Endpoints struct {
	Api    string
	Auth   string
	Portal string
}

Endpoints are where the CLI talks to: the /v1 API, the Keycloak realm (issuer URL) and the customer portal, where a customer accepts the terms.

type Keycloak

type Keycloak struct {
	Issuer string
	HTTP   *http.Client
	Sleep  func(time.Duration)
	Now    func() time.Time
}

Keycloak talks to the OpenID Connect endpoints of one realm. Sleep and Now are injected so tests do not wait.

type Manager

type Manager struct {
	Endpoints Endpoints
	Store     Store
	Keycloak  Keycloak
}

func (*Manager) AccessToken

func (m *Manager) AccessToken(ctx context.Context) (string, error)

AccessToken returns a token valid for the configured API, refreshing it first when it is about to expire.

func (*Manager) Current

func (m *Manager) Current() (Session, error)

Current is the saved session as it is on disk, whichever endpoint it belongs to.

func (*Manager) Login

func (m *Manager) Login(ctx context.Context, show func(DeviceCode)) (Session, error)

Login runs the Device Authorization Grant: show tells the user where to confirm, then Login waits for the confirmation and saves the session. Nothing is saved when the login fails.

func (*Manager) Logout

func (m *Manager) Logout(ctx context.Context) error

Logout revokes the session at Keycloak, then deletes it from disk. The file goes even when the revoke fails, and the error says so: the refresh token may still be valid on the server until it expires.

type Session

type Session struct {
	Api             string    `json:"api"`
	Issuer          string    `json:"issuer"`
	Username        string    `json:"username"`
	AccessToken     string    `json:"accessToken"`
	AccessExpiresAt time.Time `json:"accessExpiresAt"`
	RefreshToken    string    `json:"refreshToken"`
}

Session is what one `auth login` leaves on disk. Api and Issuer record where it belongs, so a token issued for one environment is never sent to another.

type Store

type Store struct {
	Dir string
}

Store keeps the session in <Dir>/session.json, readable by the owner only: the refresh token is a 30-day credential.

func (Store) Delete

func (s Store) Delete() error

func (Store) Load

func (s Store) Load() (Session, error)

func (Store) Save

func (s Store) Save(session Session) error

Jump to

Keyboard shortcuts

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