SetupAuth sets the client's authMode. It tries from the environment first if we're on android or in dev mode, and then from the client configuration.
()
| 308 | |
| 309 | // SetupAuth sets the client's authMode. It tries from the environment first if we're on android or in dev mode, and then from the client configuration. |
| 310 | func (c *Client) SetupAuth() error { |
| 311 | if c.noExtConfig { |
| 312 | if c.authMode != nil { |
| 313 | if _, ok := c.authMode.(*auth.None); !ok { |
| 314 | return nil |
| 315 | } |
| 316 | } |
| 317 | return errors.New("client: noExtConfig set; auth should not be configured from config or env vars") |
| 318 | } |
| 319 | // env var takes precedence, but only if we're in dev mode or on android. |
| 320 | // Too risky otherwise. |
| 321 | if android.OnAndroid() || |
| 322 | env.IsDev() || |
| 323 | configDisabled { |
| 324 | authMode, err := auth.FromEnv() |
| 325 | if err == nil { |
| 326 | c.authMode = authMode |
| 327 | return nil |
| 328 | } |
| 329 | if err != auth.ErrNoAuth { |
| 330 | return fmt.Errorf("Could not set up auth from env var CAMLI_AUTH: %v", err) |
| 331 | } |
| 332 | } |
| 333 | if c.server == "" { |
| 334 | return fmt.Errorf("no server defined for this client: can not set up auth") |
| 335 | } |
| 336 | authConf := serverAuth(c.server) |
| 337 | if authConf == "" { |
| 338 | c.authErr = fmt.Errorf("could not find auth key for server %q in config, defaulting to no auth", c.server) |
| 339 | c.authMode = auth.None{} |
| 340 | return nil |
| 341 | } |
| 342 | var err error |
| 343 | c.authMode, err = auth.FromConfig(authConf) |
| 344 | return err |
| 345 | } |
| 346 | |
| 347 | // serverAuth returns the auth scheme for server from the config, or the empty string if the server was not found in the config. |
| 348 | func serverAuth(server string) string { |
no test coverage detected