requireOAuthToken ensures an OAuth (device flow) token is present and valid. agent-task subcommands inherit this check via PersistentPreRunE.
(f *cmdutil.Factory)
| 69 | // requireOAuthToken ensures an OAuth (device flow) token is present and valid. |
| 70 | // agent-task subcommands inherit this check via PersistentPreRunE. |
| 71 | func requireOAuthToken(f *cmdutil.Factory) error { |
| 72 | cfg, err := f.Config() |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | |
| 77 | authCfg := cfg.Authentication() |
| 78 | host, _ := authCfg.DefaultHost() |
| 79 | if host == "" { |
| 80 | return errors.New("no default host configured; run 'gh auth login'") |
| 81 | } |
| 82 | |
| 83 | if auth.IsEnterprise(host) { |
| 84 | return errors.New("agent tasks are not supported on this host") |
| 85 | } |
| 86 | |
| 87 | token, source := authCfg.ActiveToken(host) |
| 88 | |
| 89 | // Tokens from sources "oauth_token" and "keyring" are likely |
| 90 | // minted through our device flow. |
| 91 | tokenSourceIsDeviceFlow := source == "oauth_token" || source == "keyring" |
| 92 | // Tokens with "gho_" prefix are OAuth tokens. |
| 93 | tokenIsOAuth := strings.HasPrefix(token, "gho_") |
| 94 | |
| 95 | // Reject if the token is not from a device flow source or is not an OAuth token |
| 96 | if !tokenSourceIsDeviceFlow || !tokenIsOAuth { |
| 97 | return fmt.Errorf("this command requires an OAuth token. Re-authenticate with: gh auth login") |
| 98 | } |
| 99 | return nil |
| 100 | } |
no test coverage detected