-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v2: move OAuth support into the OAuthConfig type
Including it on the core Client object complicated state management. The example in the README now shows how to use OAuthConfig to obtain an HTTP client to use with our Client. Updates tailscale/corp#21867 Signed-off-by: Percy Wegmann <[email protected]>
- Loading branch information
Showing
3 changed files
with
56 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright (c) David Bond, Tailscale Inc, & Contributors | ||
// SPDX-License-Identifier: MIT | ||
|
||
package tsclient | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"path" | ||
|
||
"golang.org/x/oauth2/clientcredentials" | ||
) | ||
|
||
// OAuthConfig provides a mechanism for configuring OAuth authentication. | ||
type OAuthConfig struct { | ||
// ClientID is the client ID of the OAuth client. | ||
ClientID string | ||
// ClientSecret is the client secret of the OAuth client. | ||
ClientSecret string | ||
// Scopes are the scopes to request when generating tokens for this OAuth client. | ||
Scopes []string | ||
// BaseURL is an optional base URL for the API server to which we'll connect. Defaults to https://api.tailscale.com. | ||
BaseURL string | ||
} | ||
|
||
// HTTPClient constructs an HTTP client that authenticates using OAuth. | ||
func (ocfg OAuthConfig) HTTPClient() *http.Client { | ||
baseURL := ocfg.BaseURL | ||
if baseURL == "" { | ||
baseURL = defaultBaseURL.String() | ||
} | ||
oauthConfig := clientcredentials.Config{ | ||
ClientID: ocfg.ClientID, | ||
ClientSecret: ocfg.ClientSecret, | ||
Scopes: ocfg.Scopes, | ||
TokenURL: path.Join(baseURL, "/api/v2/oauth/token"), | ||
} | ||
|
||
// Use context.Background() here, since this is used to refresh the token in the future. | ||
client := oauthConfig.Client(context.Background()) | ||
client.Timeout = defaultHttpClientTimeout | ||
return client | ||
} |