| 350 | } |
| 351 | |
| 352 | func requireAuth() error { |
| 353 | if apiKey, ok := cache.Get("api_key").(string); ok && apiKey != "" { |
| 354 | logger.Debug("Using API key") |
| 355 | api.SetAuth("bearer", apiKey) |
| 356 | return nil |
| 357 | } |
| 358 | if token, ok := cache.Get("oauth_token").(*oauth2.Token); ok && token != nil { |
| 359 | friendlyExpiry := token.Expiry.Truncate(time.Minute).Format(time.Kitchen) |
| 360 | if time.Until(token.Expiry) > 0 { |
| 361 | logger.Debugf("Using access token (valid until %s)", friendlyExpiry) |
| 362 | api.SetAuth(token.TokenType, token.AccessToken) |
| 363 | return nil |
| 364 | } |
| 365 | logger.Warnf("Access token expired at %s", friendlyExpiry) |
| 366 | } |
| 367 | return errUnauthenticated.New() |
| 368 | } |
| 369 | |
| 370 | var ( |
| 371 | versionCommand = version.Print(Root) |