-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add external credentials support (#80)
- Loading branch information
1 parent
7fddd18
commit ab6a898
Showing
5 changed files
with
135 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package integrations | ||
|
||
import ( | ||
"context" | ||
"github.com/databricks/databricks-sql-go/auth" | ||
"github.com/grafana/grafana-plugin-sdk-go/backend/log" | ||
"golang.org/x/oauth2" | ||
"golang.org/x/oauth2/clientcredentials" | ||
"net/http" | ||
"sync" | ||
"time" | ||
) | ||
|
||
type oauth2ClientCredentials struct { | ||
clientID string | ||
clientSecret string | ||
tokenUrl string | ||
tokenSource oauth2.TokenSource | ||
mx sync.Mutex | ||
} | ||
|
||
func (c *oauth2ClientCredentials) Authenticate(r *http.Request) error { | ||
c.mx.Lock() | ||
defer c.mx.Unlock() | ||
if c.tokenSource != nil { | ||
token, err := c.tokenSource.Token() | ||
if err != nil { | ||
return err | ||
} | ||
token.SetAuthHeader(r) | ||
return nil | ||
} | ||
|
||
config := clientcredentials.Config{ | ||
ClientID: c.clientID, | ||
ClientSecret: c.clientSecret, | ||
TokenURL: c.tokenUrl, | ||
} | ||
|
||
// Create context with 1m timeout to cancel token fetching | ||
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute) | ||
if cancel != nil { | ||
log.DefaultLogger.Debug("ignoring defer for timeout to not cancel original request") | ||
} | ||
|
||
c.tokenSource = config.TokenSource(ctx) | ||
|
||
log.DefaultLogger.Debug("token fetching started") | ||
token, err := c.tokenSource.Token() | ||
|
||
if err != nil { | ||
log.DefaultLogger.Error("token fetching failed", "err", err) | ||
return err | ||
} else { | ||
log.DefaultLogger.Debug("token fetched successfully") | ||
} | ||
token.SetAuthHeader(r) | ||
|
||
return nil | ||
|
||
} | ||
|
||
func NewOauth2ClientCredentials(clientID, clientSecret, tokenUrl string) auth.Authenticator { | ||
return &oauth2ClientCredentials{ | ||
clientID: clientID, | ||
clientSecret: clientSecret, | ||
tokenUrl: tokenUrl, | ||
tokenSource: nil, | ||
mx: sync.Mutex{}, | ||
} | ||
} |
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