Authentication
Describes github.com/stxapp/stx-go v0.3.1, the current release.
Functions
Section titled “Functions”LoadPrivateKey
Section titled “LoadPrivateKey”func LoadPrivateKey(value string) (ed25519.PrivateKey, error)LoadPrivateKey parses a PKCS#8 Ed25519 private key from PEM text or from a path to a PEM file (a leading ~ is expanded).
ParsePrivateKeyPEM
Section titled “ParsePrivateKeyPEM”func ParsePrivateKeyPEM(data []byte) (ed25519.PrivateKey, error)ParsePrivateKeyPEM parses a PKCS#8 Ed25519 private key from PEM bytes.
SigningMessage
Section titled “SigningMessage”func SigningMessage(timestampMs int64, method, path string) stringSigningMessage returns the string an API key signs: the Unix time in milliseconds, the uppercase HTTP method and the request path (including any query string), concatenated without separators.
APIKey
Section titled “APIKey”type APIKey struct { // contains filtered or unexported fields}APIKey is an STX API key: the key id sent with every request, and an Ed25519 signer. The private key never leaves the process; only signatures are sent. It implements Auth.
NewAPIKey
Section titled “NewAPIKey”func NewAPIKey(id, privateKey string) (*APIKey, error)NewAPIKey builds an API key from its id and an Ed25519 private key given as PKCS#8 PEM text or as a path to a PEM file (a leading ~ is expanded).
NewAPIKeyWithSigner
Section titled “NewAPIKeyWithSigner”func NewAPIKeyWithSigner(id string, signer crypto.Signer) (*APIKey, error)NewAPIKeyWithSigner builds an API key around any crypto.Signer that produces pure Ed25519 signatures, for keys held in an HSM or a cloud KMS. An ed25519.PrivateKey works as is.
APIKey.AuthHeaders
Section titled “APIKey.AuthHeaders”func (k *APIKey) AuthHeaders(_ context.Context, method, path string) (http.Header, error)AuthHeaders signs one HTTP request.
APIKey.GoString
Section titled “APIKey.GoString”func (k APIKey) GoString() stringGoString is what %#v prints: the key id, never key material.
APIKey.ID
Section titled “APIKey.ID”func (k *APIKey) ID() stringID returns the key id.
APIKey.Kind
Section titled “APIKey.Kind”func (k *APIKey) Kind() stringKind returns “api_key”.
APIKey.Sign
Section titled “APIKey.Sign”func (k *APIKey) Sign(message []byte) ([]byte, error)Sign returns the raw Ed25519 signature over message.
APIKey.SignedHeaders
Section titled “APIKey.SignedHeaders”func (k *APIKey) SignedHeaders(method, path string, at time.Time) (http.Header, error)SignedHeaders returns the three access headers for method and path at the given time. The server rejects a timestamp more than 30 seconds from its clock, so sign every request afresh.
APIKey.String
Section titled “APIKey.String”func (k APIKey) String() stringString returns a description that never includes key material.
APIKey.WebSocketHeaders
Section titled “APIKey.WebSocketHeaders”func (k *APIKey) WebSocketHeaders(_ context.Context, _ bool) (http.Header, error)WebSocketHeaders signs the WebSocket handshake: GET and the socket path, without the query string.
type Auth interface { // Kind is a stable label for logs, such as "api_key" or "oauth". Kind() string // AuthHeaders returns the headers for one HTTP request. It is called // afresh for every attempt. path includes the query string, exactly as // sent. AuthHeaders(ctx context.Context, method, path string) (http.Header, error)}Auth authenticates requests. *APIKey (Ed25519 request signing) is the usual one; the oauth package provides bearer-token implementations for apps acting on a member’s behalf. Pass one with WithAuth.
Reauthenticator
Section titled “Reauthenticator”type Reauthenticator interface { Reauthenticate(ctx context.Context) (bool, error)}Reauthenticator is an Auth that can recover from a 401, for example by refreshing an OAuth access token. After a 401 the client calls Reauthenticate once; when it returns true the request is sent again.
WebSocketAuth
Section titled “WebSocketAuth”type WebSocketAuth interface { WebSocketHeaders(ctx context.Context, reconnect bool) (http.Header, error)}WebSocketAuth is an Auth that can authenticate the WebSocket handshake. reconnect is true when the handshake follows a dropped connection.

