2021-05-03 07:46:32 -05:00
|
|
|
package pluginproxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-10-21 15:29:56 -05:00
|
|
|
googletokenprovider "github.com/grafana/grafana-google-sdk-go/pkg/tokenprovider"
|
2021-05-03 07:46:32 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
|
|
|
)
|
|
|
|
|
|
|
|
type jwtAccessTokenProvider struct {
|
2021-10-21 15:29:56 -05:00
|
|
|
source googletokenprovider.TokenProvider
|
|
|
|
ctx context.Context
|
2021-05-03 07:46:32 -05:00
|
|
|
}
|
|
|
|
|
2021-11-01 04:53:33 -05:00
|
|
|
func newJwtAccessTokenProvider(ctx context.Context, ds DSInfo, pluginRoute *plugins.Route,
|
|
|
|
authParams *plugins.JWTTokenAuth) *jwtAccessTokenProvider {
|
2021-10-21 15:29:56 -05:00
|
|
|
jwtConf := &googletokenprovider.JwtTokenConfig{}
|
|
|
|
if val, ok := authParams.Params["client_email"]; ok {
|
|
|
|
jwtConf.Email = val
|
2021-05-03 07:46:32 -05:00
|
|
|
}
|
|
|
|
|
2021-10-21 15:29:56 -05:00
|
|
|
if val, ok := authParams.Params["private_key"]; ok {
|
|
|
|
jwtConf.PrivateKey = []byte(val)
|
2021-05-03 07:46:32 -05:00
|
|
|
}
|
|
|
|
|
2021-10-21 15:29:56 -05:00
|
|
|
if val, ok := authParams.Params["token_uri"]; ok {
|
|
|
|
jwtConf.URI = val
|
2021-05-03 07:46:32 -05:00
|
|
|
}
|
|
|
|
|
2021-10-21 15:29:56 -05:00
|
|
|
cfg := googletokenprovider.Config{
|
|
|
|
RoutePath: pluginRoute.Path,
|
|
|
|
RouteMethod: pluginRoute.Method,
|
|
|
|
DataSourceID: ds.ID,
|
|
|
|
DataSourceUpdated: ds.Updated,
|
|
|
|
Scopes: authParams.Scopes,
|
|
|
|
JwtTokenConfig: jwtConf,
|
2021-05-03 07:46:32 -05:00
|
|
|
}
|
|
|
|
|
2021-10-21 15:29:56 -05:00
|
|
|
return &jwtAccessTokenProvider{
|
|
|
|
source: googletokenprovider.NewJwtAccessTokenProvider(cfg),
|
|
|
|
ctx: ctx,
|
2021-05-03 07:46:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-21 15:29:56 -05:00
|
|
|
func (provider *jwtAccessTokenProvider) GetAccessToken() (string, error) {
|
|
|
|
return provider.source.GetAccessToken(provider.ctx)
|
2021-05-03 07:46:32 -05:00
|
|
|
}
|