Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 35 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ A Go package for secure HMAC-SHA256-based authentication and validation of HTTP
- **HTTP Request Signing:** Attach authentication headers (`x-signature`, `x-api-key-id`, `x-timestamp`) to HTTP requests.
- **Request Validation:** Validate signed HTTP requests, including signature and timestamp checks.
- **Configurable Validity Window:** Control how long a signed request remains valid.
- **Secure HTTP Client:** Use the `client` package to automatically sign and send authenticated HTTP requests.

## Usage

Expand All @@ -16,7 +17,7 @@ A Go package for secure HMAC-SHA256-based authentication and validation of HTTP
```go
import (
"fmt"
"yourmodule/hash"
"github.com/go-core-stack/auth/hash"
)

func main() {
Expand All @@ -32,7 +33,7 @@ func main() {
```go
import (
"net/http"
"yourmodule/hash"
"github.com/go-core-stack/auth/hash"
)

func main() {
Expand All @@ -48,7 +49,7 @@ func main() {
```go
import (
"net/http"
"yourmodule/hash"
"github.com/go-core-stack/auth/hash"
"fmt"
)

Expand All @@ -64,6 +65,29 @@ func main() {
}
```

### 4. Use the Secure HTTP Client

```go
import (
"github.com/go-core-stack/auth/client"
"net/http"
)

func main() {
cli, err := client.NewClient("https://api.example.com", "api-key-id", "supersecret", false)
if err != nil {
panic(err)
}
req, _ := http.NewRequest("GET", "/resource", nil)
resp, err := cli.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// Handle response...
}
```

## API

### `GenerateSHA256HMAC(secret string, v ...string) string`
Expand All @@ -86,6 +110,14 @@ func main() {

- Returns a Validator for validating HTTP requests. `validity` is the allowed time window (in seconds).

### `client.Client` interface

- `Do(*http.Request) (*http.Response, error)`: Sends a signed HTTP request.

### `client.NewClient(endpoint, apiKey, secret string, allowInsecure bool) (Client, error)`

- Returns a secure HTTP client that signs all requests. Set `allowInsecure` to `true` to disable TLS verification (for testing only).

## Testing

Run all tests:
Expand Down