auth

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package auth handles NEPSE API authentication using embedded WASM to decode obfuscated tokens returned by the NEPSE API.

Index

Constants

View Source
const DefaultTokenTTL = 45 * time.Second

DefaultTokenTTL defines when to proactively refresh tokens. NEPSE tokens expire after ~60 seconds; we refresh at 45s to avoid mid-request expiration.

Variables

This section is empty.

Functions

func SetAuthHeader

func SetAuthHeader(req *http.Request, token string)

SetAuthHeader adds the NEPSE-specific "Salter" authorization header.

Types

type Manager

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

Manager provides thread-safe access to NEPSE authentication tokens. It uses singleflight to prevent thundering herd when multiple goroutines request tokens simultaneously during refresh.

func NewManager

func NewManager(httpClient NepseHTTP) (*Manager, error)

NewManager creates a Manager with the embedded WASM token parser.

func (*Manager) AccessToken

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

AccessToken returns a valid access token, refreshing if expired.

func (*Manager) Close

func (m *Manager) Close() error

Close must be called to release WASM runtime memory.

func (*Manager) ForceUpdate

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

ForceUpdate invalidates the cache and fetches fresh tokens. Used after receiving 401 to force re-authentication.

func (*Manager) GetSalts added in v0.1.2

func (m *Manager) GetSalts(ctx context.Context) (Salts, error)

GetSalts returns the current salt values, refreshing the token if expired. Salts are used to compute POST payload IDs for graph endpoints.

type NepseHTTP

type NepseHTTP interface {
	Token(ctx context.Context) (*TokenResponse, error)
}

NepseHTTP abstracts HTTP calls needed for token acquisition.

type Salts added in v0.1.2

type Salts struct {
	Salt1 int
	Salt2 int
	Salt3 int
	Salt4 int
	Salt5 int
}

Salts holds the 5 salt values from the NEPSE token response. These are used to compute POST payload IDs for certain endpoints.

type TokenResponse

type TokenResponse struct {
	Salt1        int    `json:"salt1"`
	Salt2        int    `json:"salt2"`
	Salt3        int    `json:"salt3"`
	Salt4        int    `json:"salt4"`
	Salt5        int    `json:"salt5"`
	AccessToken  string `json:"accessToken"`
	RefreshToken string `json:"refreshToken"`
	ServerTime   int64  `json:"serverTime"`
}

TokenResponse is the JSON structure from /api/authenticate/prove. Salt values are used to compute which characters to strip from tokens.

Jump to

Keyboard shortcuts

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