|
| 1 | +//go:build js && wasm |
| 2 | + |
| 3 | +package ovh |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "errors" |
| 8 | + "fmt" |
| 9 | + "os" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "golang.org/x/oauth2/clientcredentials" |
| 13 | +) |
| 14 | + |
| 15 | +// loadConfig loads client configuration from params or environments (by order of decreasing precedence). |
| 16 | +// |
| 17 | +// loadConfig will check OVH_CONSUMER_KEY, OVH_APPLICATION_KEY, OVH_APPLICATION_SECRET |
| 18 | +// and OVH_ENDPOINT environment variables. |
| 19 | +// It will also check for alternative authentication means: |
| 20 | +// - OVH_ACCESS_TOKEN |
| 21 | +// - OVH_CLIENT_ID and OVH_CLIENT_SECRET |
| 22 | +func (c *Client) loadConfig(endpointName string) error { |
| 23 | + if strings.HasSuffix(endpointName, "/") { |
| 24 | + return fmt.Errorf("endpoint name cannot have a tailing slash") |
| 25 | + } |
| 26 | + |
| 27 | + // Canonicalize configuration |
| 28 | + if endpointName == "" { |
| 29 | + endpointName = getConfigValue("OVH_ENDPOINT", "ovh-eu") |
| 30 | + } |
| 31 | + |
| 32 | + if c.AccessToken == "" { |
| 33 | + c.AccessToken = getConfigValue("OVH_ACCESS_TOKEN", "") |
| 34 | + } |
| 35 | + |
| 36 | + if c.AppKey == "" { |
| 37 | + c.AppKey = getConfigValue("OVH_APPLICATION_KEY", "") |
| 38 | + } |
| 39 | + |
| 40 | + if c.AppSecret == "" { |
| 41 | + c.AppSecret = getConfigValue("OVH_APPLICATION_SECRET", "") |
| 42 | + } |
| 43 | + |
| 44 | + if c.ConsumerKey == "" { |
| 45 | + c.ConsumerKey = getConfigValue("OVH_CONSUMER_KEY", "") |
| 46 | + } |
| 47 | + |
| 48 | + if c.ClientID == "" { |
| 49 | + c.ClientID = getConfigValue("OVH_CLIENT_ID", "") |
| 50 | + } |
| 51 | + |
| 52 | + if c.ClientSecret == "" { |
| 53 | + c.ClientSecret = getConfigValue("OVH_CLIENT_SECRET", "") |
| 54 | + } |
| 55 | + |
| 56 | + configuredAuthMethods := []string{} |
| 57 | + if c.AppKey != "" || c.AppSecret != "" || c.ConsumerKey != "" { |
| 58 | + configuredAuthMethods = append(configuredAuthMethods, "application_key/application_secret") |
| 59 | + } |
| 60 | + if c.ClientID != "" || c.ClientSecret != "" { |
| 61 | + configuredAuthMethods = append(configuredAuthMethods, "client_id/client_secret") |
| 62 | + } |
| 63 | + if c.AccessToken != "" { |
| 64 | + configuredAuthMethods = append(configuredAuthMethods, "access_token") |
| 65 | + } |
| 66 | + |
| 67 | + if len(configuredAuthMethods) > 1 { |
| 68 | + return fmt.Errorf("can't use multiple authentication methods: %s", strings.Join(configuredAuthMethods, ", ")) |
| 69 | + } |
| 70 | + if len(configuredAuthMethods) == 0 { |
| 71 | + return errors.New( |
| 72 | + "missing authentication information, you need to provide one of the following: application_key/application_secret, client_id/client_secret, or access_token", |
| 73 | + ) |
| 74 | + } |
| 75 | + |
| 76 | + if (c.ClientID != "") != (c.ClientSecret != "") { |
| 77 | + return errors.New("invalid oauth2 config, both client_id and client_secret must be given") |
| 78 | + } |
| 79 | + if (c.AppKey != "") != (c.AppSecret != "") { |
| 80 | + return errors.New("invalid authentication config, both application_key and application_secret must be given") |
| 81 | + } |
| 82 | + |
| 83 | + // Load real endpoint URL by name. If endpoint contains a '/', consider it as a URL |
| 84 | + if strings.Contains(endpointName, "/") { |
| 85 | + c.endpoint = endpointName |
| 86 | + } else { |
| 87 | + c.endpoint = Endpoints[endpointName] |
| 88 | + } |
| 89 | + |
| 90 | + // If we still have no valid endpoint, AppKey or AppSecret, return an error |
| 91 | + if c.endpoint == "" { |
| 92 | + return fmt.Errorf("unknown endpoint '%s', consider checking 'Endpoints' list or using an URL", endpointName) |
| 93 | + } |
| 94 | + |
| 95 | + if c.ClientID != "" { |
| 96 | + if _, ok := tokensURLs[c.endpoint]; !ok { |
| 97 | + return fmt.Errorf("oauth2 authentication is not compatible with endpoint %q", c.endpoint) |
| 98 | + } |
| 99 | + |
| 100 | + conf := &clientcredentials.Config{ |
| 101 | + ClientID: c.ClientID, |
| 102 | + ClientSecret: c.ClientSecret, |
| 103 | + TokenURL: tokensURLs[c.endpoint], |
| 104 | + Scopes: []string{"all"}, |
| 105 | + } |
| 106 | + |
| 107 | + c.oauth2TokenSource = conf.TokenSource(context.Background()) |
| 108 | + } |
| 109 | + |
| 110 | + return nil |
| 111 | +} |
| 112 | + |
| 113 | +// getConfigValue returns the value 'name' from environment. |
| 114 | +// If the value could not be read from env, return 'def'. |
| 115 | +func getConfigValue(name, defaultValue string) string { |
| 116 | + // Attempt to load from environment |
| 117 | + if fromEnv := os.Getenv(name); fromEnv != "" { |
| 118 | + return fromEnv |
| 119 | + } |
| 120 | + |
| 121 | + return defaultValue |
| 122 | +} |
0 commit comments