Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: source auth vars from env #318

Merged
merged 11 commits into from
Jul 5, 2021
22 changes: 16 additions & 6 deletions internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,23 @@ import (
"fmt"
"net/http"
"net/url"
"os"
"strings"
"time"
)

const (
clientID = "2iZo3Uczt5LFHacKdM0zzgUO2eG2uDjT"
deviceCodeEndpoint = "https://auth0.auth0.com/oauth/device/code"
oauthTokenEndpoint = "https://auth0.auth0.com/oauth/token"
audiencePath = "/api/v2/"
waitThresholdInSeconds = 3

// namespace used to set/get values from the keychain
SecretsNamespace = "auth0-cli"
)

var clientID = getEnv("CLIENT_ID", "2iZo3Uczt5LFHacKdM0zzgUO2eG2uDjT")
as-herzog marked this conversation as resolved.
Show resolved Hide resolved
var baseDomain = getEnv("BASE_DOMAIN", "auth0.auth0.com")
var deviceCodeEndpoint = fmt.Sprintf("https://%s/oauth/device/code", baseDomain)
var oauthTokenEndpoint = fmt.Sprintf("https://%s/oauth/token", baseDomain)

var requiredScopes = []string{
"openid",
"offline_access", // <-- to get a refresh token.
Expand All @@ -34,7 +36,7 @@ var requiredScopes = []string{
"create:users", "delete:users", "read:users", "update:users",
"read:branding", "update:branding",
"read:connections", "update:connections",
"read:client_keys", "read:logs", "read:tenant_settings",
"read:client_keys", "read:logs", "read:tenant_settings",
"read:custom_domains", "create:custom_domains", "update:custom_domains", "delete:custom_domains",
"read:anomaly_blocks", "delete:anomaly_blocks",
"create:log_streams", "delete:log_streams", "read:log_streams", "update:log_streams",
Expand Down Expand Up @@ -158,7 +160,7 @@ func (a *Authenticator) getDeviceCode(ctx context.Context) (State, error) {
data := url.Values{
"client_id": {clientID},
"scope": {strings.Join(requiredScopes, " ")},
"audience": {"https://*.auth0.com/api/v2/"},
"audience": {getEnv("AUDIENCE", "https://*.auth0.com/api/v2/")},
}
r, err := http.PostForm(deviceCodeEndpoint, data)
if err != nil {
Expand Down Expand Up @@ -199,3 +201,11 @@ func parseTenant(accessToken string) (tenant, domain string, err error) {
}
return "", "", fmt.Errorf("audience not found for %s", audiencePath)
}

// getEnv returns an env variable if exists, otherwise uses the fallback
func getEnv(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}