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