Skip to content

keys: added expirySeconds, scopes & tags to the key response #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2025
Merged
Show file tree
Hide file tree
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
23 changes: 13 additions & 10 deletions keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,19 @@ type createOAuthClientWithKeyTypeRequest struct {

// Key describes an authentication key within the tailnet.
type Key struct {
ID string `json:"id"`
KeyType string `json:"keyType"`
Key string `json:"key"`
Description string `json:"description"`
Created time.Time `json:"created"`
Expires time.Time `json:"expires"`
Revoked time.Time `json:"revoked"`
Invalid bool `json:"invalid"`
Capabilities KeyCapabilities `json:"capabilities"`
UserID string `json:"userId"`
ID string `json:"id"`
KeyType string `json:"keyType"`
Key string `json:"key"`
Description string `json:"description"`
ExpirySeconds *time.Duration `json:"expirySeconds"`
Created time.Time `json:"created"`
Expires time.Time `json:"expires"`
Revoked time.Time `json:"revoked"`
Invalid bool `json:"invalid"`
Capabilities KeyCapabilities `json:"capabilities"`
Scopes []string `json:"scopes,omitempty"`
Tags []string `json:"tags,omitempty"`
UserID string `json:"userId"`
}

// Create creates a new authentication key. Returns the generated [Key] if successful.
Expand Down
85 changes: 52 additions & 33 deletions keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,19 @@ func TestClient_CreateAuthKey(t *testing.T) {
capabilities.Devices.Create.Preauthorized = true
capabilities.Devices.Create.Tags = []string{"test:test"}

expiry := 1440 * time.Second

expected := &Key{
ID: "test",
KeyType: "auth",
Key: "thisisatestkey",
Created: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Expires: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Capabilities: capabilities,
Description: "",
ID: "test",
KeyType: "auth",
Key: "thisisatestkey",
ExpirySeconds: &expiry,
Created: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Expires: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Capabilities: capabilities,
Scopes: nil,
Tags: nil,
Description: "",
}

server.ResponseBody = expected
Expand Down Expand Up @@ -64,14 +69,19 @@ func TestClient_CreateAuthKeyWithExpirySeconds(t *testing.T) {
capabilities.Devices.Create.Preauthorized = true
capabilities.Devices.Create.Tags = []string{"test:test"}

expiry := 1440 * time.Second

expected := &Key{
ID: "test",
KeyType: "auth",
Key: "thisisatestkey",
Created: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Expires: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Capabilities: capabilities,
Description: "",
ID: "test",
KeyType: "auth",
Key: "thisisatestkey",
ExpirySeconds: &expiry,
Created: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Expires: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Capabilities: capabilities,
Scopes: nil,
Tags: nil,
Description: "",
}

server.ResponseBody = expected
Expand Down Expand Up @@ -105,13 +115,16 @@ func TestClient_CreateAuthKeyWithDescription(t *testing.T) {
capabilities.Devices.Create.Tags = []string{"test:test"}

expected := &Key{
ID: "test",
KeyType: "auth",
Key: "thisisatestkey",
Created: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Expires: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Capabilities: capabilities,
Description: "key description",
ID: "test",
KeyType: "auth",
Key: "thisisatestkey",
ExpirySeconds: nil,
Created: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Expires: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Capabilities: capabilities,
Scopes: nil,
Tags: nil,
Description: "key description",
}

server.ResponseBody = expected
Expand Down Expand Up @@ -139,12 +152,15 @@ func TestClient_CreateOAuthClient(t *testing.T) {
server.ResponseCode = http.StatusOK

expected := &Key{
ID: "test",
KeyType: "client",
Key: "thisisatestclient",
Created: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Expires: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Description: "",
ID: "test",
KeyType: "client",
Key: "thisisatestclient",
ExpirySeconds: nil,
Created: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Expires: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Scopes: []string{"all:read"},
Tags: []string{"tag:test"},
Description: "",
}

server.ResponseBody = expected
Expand Down Expand Up @@ -181,12 +197,15 @@ func TestClient_GetKey(t *testing.T) {
capabilities.Devices.Create.Tags = []string{"test:test"}

expected := &Key{
ID: "test",
KeyType: "auth",
Created: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Expires: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Capabilities: capabilities,
Description: "",
ID: "test",
KeyType: "auth",
ExpirySeconds: nil,
Created: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Expires: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
Capabilities: capabilities,
Scopes: nil,
Tags: nil,
Description: "",
}

server.ResponseBody = expected
Expand Down