MCPcopy Index your code
hub / github.com/cli/cli / requireOAuthToken

Function requireOAuthToken

pkg/cmd/agent-task/agent_task.go:71–100  ·  view source on GitHub ↗

requireOAuthToken ensures an OAuth (device flow) token is present and valid. agent-task subcommands inherit this check via PersistentPreRunE.

(f *cmdutil.Factory)

Source from the content-addressed store, hash-verified

69// requireOAuthToken ensures an OAuth (device flow) token is present and valid.
70// agent-task subcommands inherit this check via PersistentPreRunE.
71func 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}

Callers 1

NewCmdAgentTaskFunction · 0.85

Calls 5

ConfigMethod · 0.65
AuthenticationMethod · 0.65
DefaultHostMethod · 0.65
ActiveTokenMethod · 0.65
ErrorfMethod · 0.65

Tested by

no test coverage detected