Skip to content

Commit 936e156

Browse files
authored
Add support for OAuth client credentials (#44)
Signed-off-by: Cameron Stokes <[email protected]> Signed-off-by: Cameron Stokes <[email protected]>
1 parent a18bf07 commit 936e156

File tree

4 files changed

+75
-4
lines changed

4 files changed

+75
-4
lines changed

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@ go 1.19
55
require (
66
github.com/stretchr/testify v1.8.2
77
github.com/tailscale/hujson v0.0.0-20220506213045-af5ed07155e5
8+
golang.org/x/oauth2 v0.6.0
89
)
910

1011
require (
1112
github.com/davecgh/go-spew v1.1.1 // indirect
13+
github.com/golang/protobuf v1.5.2 // indirect
1214
github.com/pmezard/go-difflib v1.0.0 // indirect
15+
golang.org/x/net v0.8.0 // indirect
16+
google.golang.org/appengine v1.6.7 // indirect
17+
google.golang.org/protobuf v1.28.0 // indirect
1318
gopkg.in/yaml.v3 v3.0.1 // indirect
1419
)

go.sum

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
22
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
33
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
5+
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
6+
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
7+
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
8+
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
49
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
510
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
611
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
@@ -13,6 +18,23 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ
1318
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
1419
github.com/tailscale/hujson v0.0.0-20220506213045-af5ed07155e5 h1:erxeiTyq+nw4Cz5+hLDkOwNF5/9IQWCQPv0gpb3+QHU=
1520
github.com/tailscale/hujson v0.0.0-20220506213045-af5ed07155e5/go.mod h1:DFSS3NAGHthKo1gTlmEcSBiZrRJXi28rLNd/1udP1c8=
21+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
22+
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
23+
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
24+
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
25+
golang.org/x/oauth2 v0.6.0 h1:Lh8GPgSKBfWSwFvtuWOfeI3aAAnbXTSutYxJiOJFgIw=
26+
golang.org/x/oauth2 v0.6.0/go.mod h1:ycmewcwgD4Rpr3eZJLSB4Kyyljb3qDh40vJ8STE5HKw=
27+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
28+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
29+
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
30+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
31+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
32+
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
33+
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
34+
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
35+
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
36+
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
37+
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
1638
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1739
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
1840
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

tailscale/client.go

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414
"time"
1515

1616
"github.com/tailscale/hujson"
17+
18+
"golang.org/x/oauth2/clientcredentials"
1719
)
1820

1921
type (
@@ -44,28 +46,45 @@ type (
4446

4547
const baseURL = "https://api.tailscale.com"
4648
const contentType = "application/json"
49+
const defaultHttpClientTimeout = time.Minute
4750

4851
// NewClient returns a new instance of the Client type that will perform operations against a chosen tailnet and will
4952
// provide the apiKey for authorization. Additional options can be provided, see ClientOption for more details.
53+
//
54+
// To use OAuth Client credentials pass an empty string as apiKey and use WithOAuthClientCredentials() as below:
55+
//
56+
// client, err := tailscale.NewClient(
57+
// "",
58+
// tailnet,
59+
// tailscale.WithOAuthClientCredentials(oauthClientID, oauthClientSecret, oauthScopes),
60+
// )
5061
func NewClient(apiKey, tailnet string, options ...ClientOption) (*Client, error) {
5162
u, err := url.Parse(baseURL)
5263
if err != nil {
5364
return nil, err
5465
}
5566

5667
c := &Client{
57-
apiKey: apiKey,
58-
http: &http.Client{Timeout: time.Minute},
5968
baseURL: u,
6069
tailnet: tailnet,
6170
}
6271

72+
if apiKey != "" {
73+
c.apiKey = apiKey
74+
c.http = &http.Client{Timeout: defaultHttpClientTimeout}
75+
}
76+
6377
for _, option := range options {
6478
if err = option(c); err != nil {
6579
return nil, err
6680
}
6781
}
6882

83+
// apiKey or WithOAuthClientCredentials will initialize the http client. Fail here if both are not set.
84+
if c.apiKey == "" && c.http == nil {
85+
return nil, errors.New("no authentication credentials provided")
86+
}
87+
6988
return c, nil
7089
}
7190

@@ -82,6 +101,27 @@ func WithBaseURL(baseURL string) ClientOption {
82101
}
83102
}
84103

104+
// WithOAuthClientCredentials sets the OAuth Client Credentials to use for the Tailscale API.
105+
func WithOAuthClientCredentials(clientID, clientSecret string, scopes []string) ClientOption {
106+
return func(c *Client) error {
107+
relTokenURL, err := url.Parse("/api/v2/oauth/token")
108+
if err != nil {
109+
return err
110+
}
111+
oauthConfig := clientcredentials.Config{
112+
ClientID: clientID,
113+
ClientSecret: clientSecret,
114+
TokenURL: c.baseURL.ResolveReference(relTokenURL).String(),
115+
Scopes: scopes,
116+
}
117+
118+
// use context.Background() here, since this is used to refresh the token in the future
119+
c.http = oauthConfig.Client(context.Background())
120+
c.http.Timeout = defaultHttpClientTimeout
121+
return nil
122+
}
123+
}
124+
85125
// TODO: consider setting `headers` and `body` via opts to decrease the number of arguments.
86126
func (c *Client) buildRequest(ctx context.Context, method, uri string, headers map[string]string, body interface{}) (*http.Request, error) {
87127
u, err := c.baseURL.Parse(uri)
@@ -113,7 +153,11 @@ func (c *Client) buildRequest(ctx context.Context, method, uri string, headers m
113153
req.Header.Set("Content-Type", contentType)
114154
}
115155

116-
req.SetBasicAuth(c.apiKey, "")
156+
// c.apiKey will not be set on the client was configured with WithOAuthClientCredentials()
157+
if c.apiKey != "" {
158+
req.SetBasicAuth(c.apiKey, "")
159+
}
160+
117161
return req, nil
118162
}
119163

tailscale/tailscale_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func NewTestHarness(t *testing.T) (*tailscale.Client, *TestServer) {
5353
})
5454

5555
baseURL := fmt.Sprintf("http://localhost:%v", listener.Addr().(*net.TCPAddr).Port)
56-
client, err := tailscale.NewClient("", "example.com", tailscale.WithBaseURL(baseURL))
56+
client, err := tailscale.NewClient("not a real key", "example.com", tailscale.WithBaseURL(baseURL))
5757
assert.NoError(t, err)
5858

5959
return client, testServer

0 commit comments

Comments
 (0)