mirror of
https://github.com/grafana/grafana.git
synced 2024-12-02 05:29:42 -06:00
45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package pluginproxy
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/grafana/grafana/pkg/models"
|
|
"github.com/grafana/grafana/pkg/plugins"
|
|
"golang.org/x/oauth2/google"
|
|
)
|
|
|
|
type gceAccessTokenProvider struct {
|
|
datasourceId int64
|
|
datasourceVersion int
|
|
ctx context.Context
|
|
route *plugins.AppPluginRoute
|
|
authParams *plugins.JwtTokenAuth
|
|
}
|
|
|
|
func newGceAccessTokenProvider(ctx context.Context, ds *models.DataSource, pluginRoute *plugins.AppPluginRoute,
|
|
authParams *plugins.JwtTokenAuth) *gceAccessTokenProvider {
|
|
return &gceAccessTokenProvider{
|
|
datasourceId: ds.Id,
|
|
datasourceVersion: ds.Version,
|
|
ctx: ctx,
|
|
route: pluginRoute,
|
|
authParams: authParams,
|
|
}
|
|
}
|
|
|
|
func (provider *gceAccessTokenProvider) GetAccessToken() (string, error) {
|
|
tokenSrc, err := google.DefaultTokenSource(provider.ctx, provider.authParams.Scopes...)
|
|
if err != nil {
|
|
logger.Error("Failed to get default token from meta data server", "error", err)
|
|
return "", err
|
|
} else {
|
|
token, err := tokenSrc.Token()
|
|
if err != nil {
|
|
logger.Error("Failed to get default access token from meta data server", "error", err)
|
|
return "", err
|
|
} else {
|
|
return token.AccessToken, nil
|
|
}
|
|
}
|
|
}
|