Skip to content

Commit a05fc3b

Browse files
committed
add fields to create auth clients
Supports creating oauth clients Updates: tailscale/tailscale#9632 * fix scopes in test * remove redundency & increase test coverage * fix comment * rename test for consistency * test fix
1 parent 17a9401 commit a05fc3b

File tree

2 files changed

+72
-6
lines changed

2 files changed

+72
-6
lines changed

keys.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ type CreateKeyRequest struct {
3333
Description string `json:"description"`
3434
}
3535

36+
// CreateOAuthClientRequest describes the definition of an OAuth client to create.
37+
type CreateOAuthClientRequest struct {
38+
Scopes []string `json:"scopes"`
39+
Tags []string `json:"tags"`
40+
Description string `json:"description"`
41+
}
42+
43+
type createOAuthClientWithKeyTypeRequest struct {
44+
KeyType string `json:"keyType"`
45+
CreateOAuthClientRequest
46+
}
47+
3648
// Key describes an authentication key within the tailnet.
3749
type Key struct {
3850
ID string `json:"id"`
@@ -47,6 +59,7 @@ type Key struct {
4759
}
4860

4961
// Create creates a new authentication key. Returns the generated [Key] if successful.
62+
// Deprecated: Use CreateAuthKey instead.
5063
func (kr *KeysResource) Create(ctx context.Context, ckr CreateKeyRequest) (*Key, error) {
5164
req, err := kr.buildRequest(ctx, http.MethodPost, kr.buildTailnetURL("keys"), requestBody(ckr))
5265
if err != nil {
@@ -56,6 +69,24 @@ func (kr *KeysResource) Create(ctx context.Context, ckr CreateKeyRequest) (*Key,
5669
return body[Key](kr, req)
5770
}
5871

72+
// CreateAuthKey creates a new authentication key. Returns the generated [Key] if successful.
73+
func (kr *KeysResource) CreateAuthKey(ctx context.Context, ckr CreateKeyRequest) (*Key, error) {
74+
return kr.Create(ctx, ckr)
75+
}
76+
77+
// CreateOAuthClient creates a new OAuth client. Returns the generated [Key] if successful.
78+
func (kr *KeysResource) CreateOAuthClient(ctx context.Context, ckr CreateOAuthClientRequest) (*Key, error) {
79+
req, err := kr.buildRequest(ctx, http.MethodPost, kr.buildTailnetURL("keys"), requestBody(createOAuthClientWithKeyTypeRequest{
80+
KeyType: "oauthclient",
81+
CreateOAuthClientRequest: ckr,
82+
}))
83+
if err != nil {
84+
return nil, err
85+
}
86+
87+
return body[Key](kr, req)
88+
}
89+
5990
// Get returns all information on a [Key] whose identifier matches the one provided. This will not return the
6091
// authentication key itself, just the metadata.
6192
func (kr *KeysResource) Get(ctx context.Context, id string) (*Key, error) {

keys_test.go

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/stretchr/testify/assert"
1414
)
1515

16-
func TestClient_CreateKey(t *testing.T) {
16+
func TestClient_CreateAuthKey(t *testing.T) {
1717
t.Parallel()
1818

1919
client, server := NewTestHarness(t)
@@ -36,7 +36,7 @@ func TestClient_CreateKey(t *testing.T) {
3636

3737
server.ResponseBody = expected
3838

39-
actual, err := client.Keys().Create(context.Background(), CreateKeyRequest{
39+
actual, err := client.Keys().CreateAuthKey(context.Background(), CreateKeyRequest{
4040
Capabilities: capabilities,
4141
})
4242
assert.NoError(t, err)
@@ -51,7 +51,7 @@ func TestClient_CreateKey(t *testing.T) {
5151
assert.EqualValues(t, "", actualReq.Description)
5252
}
5353

54-
func TestClient_CreateKeyWithExpirySeconds(t *testing.T) {
54+
func TestClient_CreateAuthKeyWithExpirySeconds(t *testing.T) {
5555
t.Parallel()
5656

5757
client, server := NewTestHarness(t)
@@ -74,7 +74,7 @@ func TestClient_CreateKeyWithExpirySeconds(t *testing.T) {
7474

7575
server.ResponseBody = expected
7676

77-
actual, err := client.Keys().Create(context.Background(), CreateKeyRequest{
77+
actual, err := client.Keys().CreateAuthKey(context.Background(), CreateKeyRequest{
7878
Capabilities: capabilities,
7979
ExpirySeconds: 1440,
8080
})
@@ -90,7 +90,7 @@ func TestClient_CreateKeyWithExpirySeconds(t *testing.T) {
9090
assert.EqualValues(t, "", actualReq.Description)
9191
}
9292

93-
func TestClient_CreateKeyWithDescription(t *testing.T) {
93+
func TestClient_CreateAuthKeyWithDescription(t *testing.T) {
9494
t.Parallel()
9595

9696
client, server := NewTestHarness(t)
@@ -113,7 +113,7 @@ func TestClient_CreateKeyWithDescription(t *testing.T) {
113113

114114
server.ResponseBody = expected
115115

116-
actual, err := client.Keys().Create(context.Background(), CreateKeyRequest{
116+
actual, err := client.Keys().CreateAuthKey(context.Background(), CreateKeyRequest{
117117
Capabilities: capabilities,
118118
Description: "key description",
119119
})
@@ -129,6 +129,41 @@ func TestClient_CreateKeyWithDescription(t *testing.T) {
129129
assert.EqualValues(t, "key description", actualReq.Description)
130130
}
131131

132+
func TestClient_CreateOAuthClient(t *testing.T) {
133+
t.Parallel()
134+
135+
client, server := NewTestHarness(t)
136+
server.ResponseCode = http.StatusOK
137+
138+
expected := &Key{
139+
ID: "test",
140+
Key: "thisisatestclient",
141+
Created: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
142+
Expires: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC),
143+
Description: "",
144+
}
145+
146+
server.ResponseBody = expected
147+
148+
actual, err := client.Keys().CreateOAuthClient(context.Background(), CreateOAuthClientRequest{
149+
Scopes: []string{"all:read"},
150+
Tags: []string{"tag:test"},
151+
})
152+
assert.NoError(t, err)
153+
assert.EqualValues(t, expected, actual)
154+
assert.Equal(t, http.MethodPost, server.Method)
155+
assert.Equal(t, "/api/v2/tailnet/example.com/keys", server.Path)
156+
157+
var actualReq createOAuthClientWithKeyTypeRequest
158+
assert.NoError(t, json.Unmarshal(server.Body.Bytes(), &actualReq))
159+
assert.EqualValues(t, "oauthclient", actualReq.KeyType)
160+
assert.EqualValues(t, 1, len(actualReq.Scopes))
161+
assert.EqualValues(t, "all:read", actualReq.Scopes[0])
162+
assert.EqualValues(t, 1, len(actualReq.Tags))
163+
assert.EqualValues(t, "tag:test", actualReq.Tags[0])
164+
assert.EqualValues(t, "", actualReq.Description)
165+
}
166+
132167
func TestClient_GetKey(t *testing.T) {
133168
t.Parallel()
134169

0 commit comments

Comments
 (0)