(httpClient *http.Client, opts buildEntryOptions)
| 373 | } |
| 374 | |
| 375 | func buildEntry(httpClient *http.Client, opts buildEntryOptions) authEntry { |
| 376 | tokenSource := opts.tokenSource |
| 377 | if tokenSource == "oauth_token" { |
| 378 | // The go-gh function TokenForHost returns this value as source for tokens read from the |
| 379 | // config file, but we want the file path instead. This attempts to reconstruct it. |
| 380 | tokenSource = filepath.Join(config.ConfigDir(), "hosts.yml") |
| 381 | } |
| 382 | entry := authEntry{ |
| 383 | Active: opts.active, |
| 384 | Host: opts.hostname, |
| 385 | Login: opts.username, |
| 386 | TokenSource: tokenSource, |
| 387 | Token: opts.token, |
| 388 | GitProtocol: opts.gitProtocol, |
| 389 | } |
| 390 | |
| 391 | // If token is not writeable, then it came from an environment variable and |
| 392 | // we need to fetch the username as it won't be stored in the config. |
| 393 | if !authTokenWriteable(tokenSource) { |
| 394 | // The httpClient will automatically use the correct token here as |
| 395 | // the token from the environment variable take highest precedence. |
| 396 | apiClient := api.NewClientFromHTTP(httpClient) |
| 397 | var err error |
| 398 | entry.Login, err = api.CurrentLoginName(apiClient, opts.hostname) |
| 399 | if err != nil { |
| 400 | entry.State = authEntryStateError |
| 401 | entry.Error = err.Error() |
| 402 | return entry |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | // Get scopes for token. |
| 407 | scopesHeader, err := shared.GetScopes(httpClient, opts.hostname, opts.token) |
| 408 | if err != nil { |
| 409 | var networkError net.Error |
| 410 | if errors.As(err, &networkError) && networkError.Timeout() { |
| 411 | entry.State = authEntryStateTimeout |
| 412 | entry.Error = err.Error() |
| 413 | return entry |
| 414 | } |
| 415 | |
| 416 | entry.State = authEntryStateError |
| 417 | entry.Error = err.Error() |
| 418 | return entry |
| 419 | } |
| 420 | entry.Scopes = scopesHeader |
| 421 | |
| 422 | entry.State = authEntryStateSuccess |
| 423 | return entry |
| 424 | } |
| 425 | |
| 426 | func authTokenWriteable(src string) bool { |
| 427 | return !strings.HasSuffix(src, "_TOKEN") |
no test coverage detected