Skip to content

Commit

Permalink
Add support for OAuth client credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
clstokes committed Mar 27, 2023
1 parent a18bf07 commit 4a0bfca
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ go 1.19
require (
github.com/stretchr/testify v1.8.2
github.com/tailscale/hujson v0.0.0-20220506213045-af5ed07155e5
golang.org/x/oauth2 v0.6.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/net v0.8.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
22 changes: 22 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand All @@ -13,6 +18,23 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tailscale/hujson v0.0.0-20220506213045-af5ed07155e5 h1:erxeiTyq+nw4Cz5+hLDkOwNF5/9IQWCQPv0gpb3+QHU=
github.com/tailscale/hujson v0.0.0-20220506213045-af5ed07155e5/go.mod h1:DFSS3NAGHthKo1gTlmEcSBiZrRJXi28rLNd/1udP1c8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.8.0 h1:Zrh2ngAOFYneWTAIAPethzeaQLuHwhuBkuV6ZiRnUaQ=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/oauth2 v0.6.0 h1:Lh8GPgSKBfWSwFvtuWOfeI3aAAnbXTSutYxJiOJFgIw=
golang.org/x/oauth2 v0.6.0/go.mod h1:ycmewcwgD4Rpr3eZJLSB4Kyyljb3qDh40vJ8STE5HKw=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
50 changes: 47 additions & 3 deletions tailscale/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
"time"

"github.com/tailscale/hujson"

"golang.org/x/oauth2/clientcredentials"
)

type (
Expand Down Expand Up @@ -44,28 +46,45 @@ type (

const baseURL = "https://api.tailscale.com"
const contentType = "application/json"
const defaultHttpClientTimeout = time.Minute

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

c := &Client{
apiKey: apiKey,
http: &http.Client{Timeout: time.Minute},
baseURL: u,
tailnet: tailnet,
}

if apiKey != "" {
c.apiKey = apiKey
c.http = &http.Client{Timeout: defaultHttpClientTimeout}
}

for _, option := range options {
if err = option(c); err != nil {
return nil, err
}
}

// apiKey or WithOAuthClientCredentials will initialize the http client. Fail here if both are not set.
if c.apiKey == "" && c.http == nil {
return nil, errors.New("no authentication credentials provided")
}

return c, nil
}

Expand All @@ -82,6 +101,27 @@ func WithBaseURL(baseURL string) ClientOption {
}
}

// WithOAuthClientCredentials sets the OAuth Client Credentials to use for the Tailscale API.
func WithOAuthClientCredentials(clientID, clientSecret string, scopes []string) ClientOption {
return func(c *Client) error {
relTokenURL, err := url.Parse("/api/v2/oauth/token")
if err != nil {
return err
}
oauthConfig := clientcredentials.Config{
ClientID: clientID,
ClientSecret: clientSecret,
TokenURL: c.baseURL.ResolveReference(relTokenURL).String(),
Scopes: scopes,
}

// use context.Background() here, since this is used to refresh the token in the future
c.http = oauthConfig.Client(context.Background())
c.http.Timeout = defaultHttpClientTimeout
return nil
}
}

// TODO: consider setting `headers` and `body` via opts to decrease the number of arguments.
func (c *Client) buildRequest(ctx context.Context, method, uri string, headers map[string]string, body interface{}) (*http.Request, error) {
u, err := c.baseURL.Parse(uri)
Expand Down Expand Up @@ -113,7 +153,11 @@ func (c *Client) buildRequest(ctx context.Context, method, uri string, headers m
req.Header.Set("Content-Type", contentType)
}

req.SetBasicAuth(c.apiKey, "")
// c.apiKey will not be set on the client was configured with WithOAuthClientCredentials()
if c.apiKey != "" {
req.SetBasicAuth(c.apiKey, "")
}

return req, nil
}

Expand Down
2 changes: 1 addition & 1 deletion tailscale/tailscale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewTestHarness(t *testing.T) (*tailscale.Client, *TestServer) {
})

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

return client, testServer
Expand Down

0 comments on commit 4a0bfca

Please sign in to comment.