import "go.chromium.org/luci/auth/client/authcli"
Package authcli implements authentication related flags parsing and CLI subcommands.
It can be used from CLI tools that want customize authentication configuration from the command line.
Minimal example of using flags parsing:
authFlags := authcli.Flags{} defaults := ... // prepare default auth.Options authFlags.Register(flag.CommandLine, defaults) flag.Parse() opts, err := authFlags.Options() if err != nil { // handle error } authenticator := auth.NewAuthenticator(ctx, auth.SilentLogin, opts) httpClient, err := authenticator.Client() if err != nil { // handle error }
This assumes that either a service account credentials are used (passed via -service-account-json), or the user has previously ran "login" subcommand and their refresh token is already cached. In any case, there will be no interaction with the user (this is what auth.SilentLogin means): if there are no cached token, authenticator.Client will return auth.ErrLoginRequired.
Interaction with the user happens only in "login" subcommand. This subcommand (as well as a bunch of other related commands) can be added to any subcommands.Application.
While it will work with any subcommand.Application, it uses luci-go/common/cli.GetContext() to grab a context for logging, so callers should prefer using cli.Application for hosting auth subcommands and making the context. This ensures consistent logging style between all subcommands of a CLI application:
import ( ... "go.chromium.org/luci/client/authcli" "go.chromium.org/luci/common/cli" ) func GetApplication(defaultAuthOpts auth.Options) *cli.Application { return &cli.Application{ Name: "app_name", Context: func(ctx context.Context) context.Context { ... configure logging, etc. ... return ctx }, Commands: []*subcommands.Command{ authcli.SubcommandInfo(defaultAuthOpts, "auth-info", false), authcli.SubcommandLogin(defaultAuthOpts, "auth-login", false), authcli.SubcommandLogout(defaultAuthOpts, "auth-logout", false), ... }, } } func main() { defaultAuthOpts := ... app := GetApplication(defaultAuthOpts) os.Exit(subcommands.Run(app, nil)) }
const ( ExitCodeSuccess = iota ExitCodeNoValidToken ExitCodeInvalidInput ExitCodeInternalError ExitCodeBadLogin )
Process exit codes for subcommands.
SubcommandContext returns subcommand.Command that can be used to setup new LUCI authentication context for a process tree.
This is an advanced command and shouldn't be usually embedded into binaries. It is primarily used by 'luci-auth' program. It exists to simplify development and debugging of programs that rely on LUCI authentication context.
func SubcommandContextWithParams(params CommandParams) *subcommands.Command
SubcommandContextWithParams returns subcommand.Command that can be used to setup new LUCI authentication context for a process tree.
SubcommandInfo returns subcommand.Command that can be used to print current cached credentials.
func SubcommandInfoWithParams(params CommandParams) *subcommands.Command
SubcommandInfoWithParams returns subcommand.Command that can be used to print current cached credentials.
SubcommandLogin returns subcommands.Command that can be used to perform interactive login.
func SubcommandLoginWithParams(params CommandParams) *subcommands.Command
SubcommandLoginWithParams returns subcommands.Command that can be used to perform interactive login.
SubcommandLogout returns subcommands.Command that can be used to purge cached credentials.
func SubcommandLogoutWithParams(params CommandParams) *subcommands.Command
SubcommandLogoutWithParams returns subcommands.Command that can be used to purge cached credentials.
SubcommandToken returns subcommand.Command that can be used to print current access token.
func SubcommandTokenWithParams(params CommandParams) *subcommands.Command
SubcommandTokenWithParams returns subcommand.Command that can be used to print current access token.
type CommandParams struct { Name string // name of the subcommand. Advanced bool // subcommands should treat this as an 'advanced' command AuthOptions auth.Options // default auth options. // ScopesFlag specifies if -scope flag must be registered. // AuthOptions.Scopes is used as a default value. // If it is empty, defaults to "https://www.googleapis.com/auth/userinfo.email". ScopesFlag bool }
CommandParams specifies various parameters for a subcommand.
type Flags struct { // RegisterScopesFlag tells Register to add -scopes flag. RegisterScopesFlag bool // contains filtered or unexported fields }
Flags defines command line flags related to authentication.
Options return instance of auth.Options struct with values set accordingly to parsed command line flags.
Register adds auth related flags to a FlagSet.
Package authcli imports 19 packages (graph) and is imported by 45 packages. Updated 2019-12-09. Refresh now. Tools for package owners.