2019-06-15 23:03:27 +02:00
|
|
|
package util
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2019-06-17 22:30:17 +02:00
|
|
|
"fmt"
|
2019-06-15 23:03:27 +02:00
|
|
|
"net/http"
|
2019-06-17 22:30:17 +02:00
|
|
|
"os"
|
|
|
|
|
"strings"
|
2019-06-15 23:03:27 +02:00
|
|
|
|
2019-06-17 22:45:49 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2019-06-15 23:03:27 +02:00
|
|
|
"golang.org/x/oauth2"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//CreateBearerHTTPClient with given token
|
|
|
|
|
func CreateBearerHTTPClient(ctx context.Context, token string) *http.Client {
|
|
|
|
|
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{
|
|
|
|
|
AccessToken: token},
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
client := oauth2.NewClient(ctx, tokenSource)
|
|
|
|
|
|
|
|
|
|
return client
|
|
|
|
|
}
|
2019-06-17 22:30:17 +02:00
|
|
|
|
|
|
|
|
// GetAccessToken lookup for the providers accesstoken
|
|
|
|
|
func GetAccessToken(providerName string) (string, error) {
|
|
|
|
|
var token string
|
|
|
|
|
var exists bool
|
|
|
|
|
envName := fmt.Sprintf("%s_ACCESS_TOKEN", strings.ToUpper(providerName))
|
|
|
|
|
|
2019-06-17 22:45:49 +02:00
|
|
|
log.Debugf("check if %s environment variable is set", envName)
|
|
|
|
|
|
2019-06-17 22:30:17 +02:00
|
|
|
if token, exists = os.LookupEnv(envName); !exists {
|
2019-06-18 21:35:51 +02:00
|
|
|
return "", fmt.Errorf("could not find %s in the enviroment variables. Please check if it is set", envName)
|
2019-06-17 22:30:17 +02:00
|
|
|
}
|
|
|
|
|
return token, nil
|
|
|
|
|
}
|