Documentation
¶
Index ¶
- Constants
- type Cap
- func (c *Cap) Cleanup() error
- func (c *Cap) CreateChallenge(conf *ChallengeConfig) (*ChallengeResponse, error)
- func (c *Cap) Flush() error
- func (c *Cap) RedeemChallenge(solution *Solution) (*RedeemResponse, error)
- func (c *Cap) ValidateToken(token string, conf *TokenConfig) (*ValidationResponse, error)
- type CapConfig
- type ChallengeConfig
- type ChallengeData
- type ChallengeResponse
- type ChallengeState
- type ChallengeTuple
- type RedeemResponse
- type Solution
- type TokenConfig
- type ValidationResponse
Constants ¶
const ( // Version is the released version of this library. Version = "1.0.1" DefaultTokensStore = ".data/tokensList.json" DefaultChallengeCount = 50 DefaultChallengeSize = 32 DefaultChallengeDifficulty = 4 DefaultExpiresMs = 600000 // 10 minutes DefaultTokenExpiresMs = 1200000 // 20 minutes DefaultSaveThrottleMs = 2000 // coalesce disk writes within 2 seconds )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cap ¶
type Cap struct {
// contains filtered or unexported fields
}
Cap represents the main Cap instance
func New ¶
New creates a new Cap instance with the given configuration.
File persistence is opt-in: without CapConfig.EnableFSState, New touches no file or directory at all and all state is kept in memory.
func (*Cap) CreateChallenge ¶
func (c *Cap) CreateChallenge(conf *ChallengeConfig) (*ChallengeResponse, error)
CreateChallenge generates a new challenge with the specified configuration
func (*Cap) Flush ¶ added in v1.0.1
Flush writes any pending token changes to disk right away, ignoring the throttle interval. It is a no-op when persistence is disabled or when there is nothing to write. Call it before shutting down so throttled changes are not lost.
func (*Cap) RedeemChallenge ¶
func (c *Cap) RedeemChallenge(solution *Solution) (*RedeemResponse, error)
RedeemChallenge validates a challenge solution and returns a verification token
func (*Cap) ValidateToken ¶
func (c *Cap) ValidateToken(token string, conf *TokenConfig) (*ValidationResponse, error)
ValidateToken validates a verification token
type CapConfig ¶
type CapConfig struct {
TokensStorePath string `json:"tokensStorePath,omitempty"` // Path to store tokens file, absolute path recommended
State *ChallengeState `json:"state,omitempty"` // State configuration
// EnableFSState turns on file-based token persistence (default: false).
// Since v1.0.1 persistence is opt-in: unless this is set, Cap never touches
// the filesystem, so no directory or file is created on New().
EnableFSState bool `json:"enableFSState,omitempty"`
// Deprecated: kept for backward compatibility only. Setting it to true still
// force-disables persistence; setting it to false no longer enables it.
// Use EnableFSState instead.
NoFSState bool `json:"noFSState,omitempty"`
// SaveThrottleMs is the minimum interval between two disk writes, in
// milliseconds. 0 uses DefaultSaveThrottleMs, a negative value writes on
// every change. Pending changes are flushed by Flush() or Cleanup().
SaveThrottleMs int `json:"saveThrottleMs,omitempty"`
// Logger receives diagnostic messages. When nil, Cap stays completely
// silent instead of writing to stdout.
Logger func(format string, args ...interface{}) `json:"-"`
}
CapConfig contains the main configuration for the Cap instance
type ChallengeConfig ¶
type ChallengeConfig struct {
ChallengeCount int `json:"challengeCount,omitempty"` // Number of challenges to generate (default: 50)
ChallengeSize int `json:"challengeSize,omitempty"` // Size of each challenge in bytes (default: 32)
ChallengeDifficulty int `json:"challengeDifficulty,omitempty"` // Difficulty level (default: 4)
ExpiresMs int `json:"expiresMs,omitempty"` // Expiration time in milliseconds (default: 600000)
Store bool `json:"store,omitempty"` // Whether to store the challenge in memory (default: true)
}
ChallengeConfig contains configuration options for challenge generation
type ChallengeData ¶
type ChallengeData struct {
Challenge []ChallengeTuple `json:"challenge"`
Expires int64 `json:"expires"`
Token string `json:"token"`
}
ChallengeData contains the complete challenge information
type ChallengeResponse ¶
type ChallengeResponse struct {
Challenge []ChallengeTuple `json:"challenge"`
Token string `json:"token,omitempty"`
Expires int64 `json:"expires"`
}
ChallengeResponse represents the response from CreateChallenge
type ChallengeState ¶
type ChallengeState struct {
ChallengesList map[string]*ChallengeData `json:"challengesList"`
TokensList map[string]int64 `json:"tokensList"`
}
ChallengeState represents the internal state of challenges and tokens
type ChallengeTuple ¶
type ChallengeTuple [2]string
ChallengeTuple represents a single challenge consisting of salt and target
type RedeemResponse ¶
type RedeemResponse struct {
Success bool `json:"success"`
Message string `json:"message,omitempty"`
Token string `json:"token,omitempty"`
Expires int64 `json:"expires,omitempty"`
}
RedeemResponse represents the response from RedeemChallenge
type Solution ¶
type Solution struct {
Token string `json:"token"`
Solutions [][]interface{} `json:"solutions"` // Array of [salt, target, solution] tuples
}
Solution represents a solution to a challenge
type TokenConfig ¶
type TokenConfig struct {
KeepToken bool `json:"keepToken,omitempty"` // Whether to keep the token after validation
}
TokenConfig contains configuration options for token validation
type ValidationResponse ¶
type ValidationResponse struct {
Success bool `json:"success"`
}
ValidationResponse represents the response from ValidateToken
