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