# Authentication

> API keys, request signing and the Auth interface.

Source: https://docs.stxapp.io/sdks/go/reference/auth/

## Functions

### LoadPrivateKey

```go
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

```go
func ParsePrivateKeyPEM(data []byte) (ed25519.PrivateKey, error)
```

ParsePrivateKeyPEM parses a PKCS#8 Ed25519 private key from PEM bytes.

### SigningMessage

```go
func SigningMessage(timestampMs int64, method, path string) string
```

SigningMessage 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

```go
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](/sdks/go/reference/auth/#auth).

### NewAPIKey

```go
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

```go
func NewAPIKeyWithSigner(id string, signer crypto.Signer) (*APIKey, error)
```

NewAPIKeyWithSigner builds an API key around any [crypto.Signer](https://pkg.go.dev/crypto#Signer) that produces pure Ed25519 signatures, for keys held in an HSM or a cloud KMS. An [ed25519.PrivateKey](https://pkg.go.dev/crypto/ed25519#PrivateKey) works as is.

### APIKey.AuthHeaders

```go
func (k *APIKey) AuthHeaders(_ context.Context, method, path string) (http.Header, error)
```

AuthHeaders signs one HTTP request.

### APIKey.GoString

```go
func (k APIKey) GoString() string
```

GoString is what %#v prints: the key id, never key material.

### APIKey.ID

```go
func (k *APIKey) ID() string
```

ID returns the key id.

### APIKey.Kind

```go
func (k *APIKey) Kind() string
```

Kind returns "api\_key".

### APIKey.Sign

```go
func (k *APIKey) Sign(message []byte) ([]byte, error)
```

Sign returns the raw Ed25519 signature over message.

### APIKey.SignedHeaders

```go
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

```go
func (k APIKey) String() string
```

String returns a description that never includes key material.

### APIKey.WebSocketHeaders

```go
func (k *APIKey) WebSocketHeaders(_ context.Context, _ bool) (http.Header, error)
```

WebSocketHeaders signs the WebSocket handshake: GET and the socket path, without the query string.

## Auth

```go
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](/sdks/go/reference/auth/#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](/sdks/go/reference/client/#withauth).

## Reauthenticator

```go
type Reauthenticator interface {
	Reauthenticate(ctx context.Context) (bool, error)
}
```

Reauthenticator is an [Auth](/sdks/go/reference/auth/#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

```go
type WebSocketAuth interface {
	WebSocketHeaders(ctx context.Context, reconnect bool) (http.Header, error)
}
```

WebSocketAuth is an [Auth](/sdks/go/reference/auth/#auth) that can authenticate the WebSocket handshake. reconnect is true when the handshake follows a dropped connection.
