apihelper

package
v0.0.0-...-acbe8ed Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2026 License: AGPL-3.0 Imports: 17 Imported by: 0

Documentation

Overview

Package apihelper provides helper functions for API client operations.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoAuthentication is returned when no authentication method is configured.
	ErrNoAuthentication = errors.New(
		"no authentication configured: please run 'sp auth login' or configure PAT in settings.json")
	// ErrNoAccessToken is returned when no access token is received.
	ErrNoAccessToken = errors.New("no access token received")
	// ErrInvalidToken is returned when a token cannot be parsed.
	ErrInvalidToken = errors.New("invalid token")
	// ErrRefreshTokenInvalid is returned when refresh token is invalid or expired.
	ErrRefreshTokenInvalid = errors.New("refresh token is invalid or expired")
)

Functions

This section is empty.

Types

type Helper

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

Helper manages API client creation and authentication.

func NewHelper

func NewHelper(cfg *config.Config, tokenPath string, verbose bool) *Helper

NewHelper creates a new API helper.

func (*Helper) AuthMethod

func (h *Helper) AuthMethod(ctx context.Context) string

AuthMethod reports how the currently-resolved credential authenticates: "pat" for a Personal Access Token, "jwt" otherwise. It resolves the token the same way GetClient does, so it reflects the credential a request would use.

func (*Helper) GetClient

func (h *Helper) GetClient(ctx context.Context) (*client.SolidPingClient, error)

GetClient returns an authenticated API client.

func (*Helper) Login

func (h *Helper) Login(ctx context.Context, org, email, password string) (string, *client.UserSummary, error)

Login performs login and saves token.

func (*Helper) Logout

func (h *Helper) Logout(ctx context.Context, callAPI bool) error

Logout removes saved token and optionally calls logout API.

func (*Helper) ResetClient

func (h *Helper) ResetClient()

ResetClient clears the cached API client, forcing re-authentication on next GetClient call.

func (*Helper) SavePAT

func (h *Helper) SavePAT(pat string) error

SavePAT saves a Personal Access Token as the sole credential in the token file, replacing any prior JWT session. Used by the browser-login flow after self-minting a PAT, and by the `--token` / SP_TOKEN paste fallback.

func (*Helper) SaveTokens

func (h *Helper) SaveTokens(accessToken, refreshToken string) error

SaveTokens saves new access and refresh tokens to the token file.

func (*Helper) TryAuthRecovery

func (h *Helper) TryAuthRecovery(ctx context.Context) (string, error)

TryAuthRecovery attempts to recover from authentication failure. It tries refresh token, then auto-login, then prompts for credentials. Returns a new token if successful.

type TokenData

type TokenData struct {
	AccessToken           string    `json:"accessToken"`
	AccessTokenExpiresAt  time.Time `json:"accessTokenExpiresAt"`
	RefreshToken          string    `json:"refreshToken"`
	RefreshTokenExpiresAt time.Time `json:"refreshTokenExpiresAt"`
	// PAT holds a Personal Access Token saved by the browser-login flow
	// (`sp auth login`). When set, it is used directly as the bearer
	// credential and never expires client-side — the server enforces any
	// expiry. A PAT and a JWT session are mutually exclusive in one file.
	PAT string `json:"pat,omitempty"`
}

TokenData represents the stored token information.

func (*TokenData) IsAccessTokenValid

func (t *TokenData) IsAccessTokenValid() bool

IsAccessTokenValid checks if the access token is still valid.

func (*TokenData) IsRefreshTokenValid

func (t *TokenData) IsRefreshTokenValid() bool

IsRefreshTokenValid checks if the refresh token is still valid.

Jump to

Keyboard shortcuts

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