(httpClient *http.Client, opts buildEntryOptions)
| 457 | } |
| 458 | |
| 459 | func buildEntry(httpClient *http.Client, opts buildEntryOptions) authEntry { |
| 460 | tokenSource := opts.credential.Source |
| 461 | if tokenSource == gh.TokenSourceOAuthToken || tokenSource == gh.TokenSourceRefreshableOAuthToken { |
| 462 | // The go-gh function TokenForHost returns this value as source for tokens read from the |
| 463 | // config file, but we want the file path instead. This attempts to reconstruct it. |
| 464 | tokenSource = filepath.Join(config.ConfigDir(), "hosts.yml") |
| 465 | } |
| 466 | entry := authEntry{ |
| 467 | Active: opts.active, |
| 468 | Host: opts.hostname, |
| 469 | Login: opts.username, |
| 470 | TokenSource: tokenSource, |
| 471 | Token: opts.credential.Token, |
| 472 | GitProtocol: opts.gitProtocol, |
| 473 | Refreshable: opts.credential.IsRefreshable(), |
| 474 | TokenExpiresAt: opts.credential.ExpiresAt, |
| 475 | RefreshTokenExpiresAt: opts.credential.RefreshTokenExpiresAt, |
| 476 | justRefreshed: opts.refreshStatus == gh.RefreshStatusDone, |
| 477 | } |
| 478 | |
| 479 | // A rejected refresh token means the stored credential was already removed during resolution, leaving an empty |
| 480 | // token behind. Sending a scope request with it would produce a misleading generic failure, so surface the real |
| 481 | // reason and stop here. The token source is left empty on purpose: the credential is gone, so where it used to |
| 482 | // live is of no use to the user now. |
| 483 | if opts.refreshStatus == gh.RefreshStatusExpired { |
| 484 | entry.State = authEntryStateRefreshExpired |
| 485 | entry.Error = fmt.Sprintf("refresh token for %s was rejected; the stored credential was removed", opts.hostname) |
| 486 | return entry |
| 487 | } |
| 488 | |
| 489 | // If token is not writeable, then it came from an environment variable and |
| 490 | // we need to fetch the username as it won't be stored in the config. |
| 491 | if !authTokenWriteable(tokenSource) { |
| 492 | // The httpClient will automatically use the correct token here as |
| 493 | // the token from the environment variable take highest precedence. |
| 494 | apiClient := api.NewClientFromHTTP(httpClient) |
| 495 | var err error |
| 496 | entry.Login, err = api.CurrentLoginName(apiClient, opts.hostname) |
| 497 | if err != nil { |
| 498 | entry.State = authEntryStateError |
| 499 | entry.Error = err.Error() |
| 500 | return entry |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | // Get scopes for token. |
| 505 | scopesHeader, err := shared.GetScopes(httpClient, opts.hostname, opts.credential.Token) |
| 506 | if err != nil { |
| 507 | var networkError net.Error |
| 508 | if errors.As(err, &networkError) && networkError.Timeout() { |
| 509 | entry.State = authEntryStateTimeout |
| 510 | entry.Error = err.Error() |
| 511 | return entry |
| 512 | } |
| 513 | |
| 514 | entry.State = authEntryStateError |
| 515 | entry.Error = err.Error() |
| 516 | // Without a refresh, an expiring token past its expiry time is bound to be rejected with HTTP 401. Flag it so |
no test coverage detected