login

package
v0.10.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CmdLoginAdd = cli.Command{
	Name:        "add",
	Usage:       "Add a Gitea login",
	Description: `Add a Gitea login, without args it will create one interactively`,
	ArgsUsage:   " ",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:    "name",
			Aliases: []string{"n"},
			Usage:   "Login name",
		},
		&cli.StringFlag{
			Name:    "url",
			Aliases: []string{"u"},
			Value:   "https://gitea.com",
			Sources: cli.EnvVars("GITEA_SERVER_URL"),
			Usage:   "Server URL",
		},
		&cli.BoolFlag{
			Name:    "no-version-check",
			Aliases: []string{"nv"},
			Usage:   "Do not check version of Gitea instance",
		},
		&cli.StringFlag{
			Name:    "token",
			Aliases: []string{"t"},
			Value:   "",
			Sources: cli.EnvVars("GITEA_SERVER_TOKEN"),
			Usage:   "Access token. Can be obtained from Settings > Applications",
		},
		&cli.StringFlag{
			Name:    "user",
			Value:   "",
			Sources: cli.EnvVars("GITEA_SERVER_USER"),
			Usage:   "User for basic auth (will create token)",
		},
		&cli.StringFlag{
			Name:    "password",
			Aliases: []string{"pwd"},
			Value:   "",
			Sources: cli.EnvVars("GITEA_SERVER_PASSWORD"),
			Usage:   "Password for basic auth (will create token)",
		},
		&cli.StringFlag{
			Name:    "otp",
			Sources: cli.EnvVars("GITEA_SERVER_OTP"),
			Usage:   "OTP token for auth, if necessary",
		},
		&cli.StringFlag{
			Name:    "scopes",
			Sources: cli.EnvVars("GITEA_SCOPES"),
			Usage:   "Token scopes to add when creating a new token, separated by a comma",
		},
		&cli.StringFlag{
			Name:    "ssh-key",
			Aliases: []string{"s"},
			Usage:   "Path to a SSH key/certificate to use, overrides auto-discovery",
		},
		&cli.BoolFlag{
			Name:    "insecure",
			Aliases: []string{"i"},
			Usage:   "Disable TLS verification",
		},
		&cli.StringFlag{
			Name:    "ssh-agent-principal",
			Aliases: []string{"c"},
			Usage:   "Use SSH certificate with specified principal to login (needs a running ssh-agent with certificate loaded)",
		},
		&cli.StringFlag{
			Name:    "ssh-agent-key",
			Aliases: []string{"a"},
			Usage:   "Use SSH public key or SSH fingerprint to login (needs a running ssh-agent with ssh key loaded)",
		},
		&cli.BoolFlag{
			Name:    "helper",
			Aliases: []string{"j"},
			Usage:   "Add helper",
		},
		&cli.BoolFlag{
			Name:    "oauth",
			Aliases: []string{"o"},
			Usage:   "Use interactive OAuth2 flow for authentication",
		},
		&cli.StringFlag{
			Name:  "client-id",
			Usage: "OAuth client ID (for use with --oauth)",
		},
		&cli.StringFlag{
			Name:  "redirect-url",
			Usage: "OAuth redirect URL (for use with --oauth)",
		},
	},
	Action: runLoginAdd,
}

CmdLoginAdd represents to login a gitea server.

View Source
var CmdLoginDelete = cli.Command{
	Name:        "delete",
	Aliases:     []string{"rm"},
	Usage:       "Remove a Gitea login",
	Description: `Remove a Gitea login`,
	ArgsUsage:   "<login name>",
	Action:      RunLoginDelete,
}

CmdLoginDelete is a command to delete a login

View Source
var CmdLoginEdit = cli.Command{
	Name:        "edit",
	Aliases:     []string{"e"},
	Usage:       "Edit Gitea logins",
	Description: `Edit Gitea logins`,
	ArgsUsage:   " ",
	Action:      runLoginEdit,
	Flags:       []cli.Flag{&flags.OutputFlag},
}

CmdLoginEdit represents to login a gitea server.

View Source
var CmdLoginHelper = cli.Command{
	Name:        "helper",
	Aliases:     []string{"git-credential"},
	Usage:       "Git helper",
	Description: `Git helper`,
	Hidden:      true,
	Commands: []*cli.Command{
		{
			Name:        "store",
			Description: "Command drops",
			Aliases:     []string{"erase"},
			Action: func(_ context.Context, _ *cli.Command) error {
				return nil
			},
		},
		{
			Name:        "setup",
			Description: "Setup helper to tea authenticate",
			Action: func(_ context.Context, _ *cli.Command) error {
				logins, err := config.GetLogins()
				if err != nil {
					return err
				}
				for _, login := range logins {
					added, err := task.SetupHelper(login)
					if err != nil {
						return err
					} else if added {
						fmt.Printf("Added \"%s\"\n", login.Name)
					} else {
						fmt.Printf("\"%s\" has already been added!\n", login.Name)
					}
				}
				return nil
			},
		},
		{
			Name:        "get",
			Description: "Get token to auth",
			Action: func(_ context.Context, cmd *cli.Command) error {
				wants := map[string]string{}
				s := bufio.NewScanner(os.Stdin)
				for s.Scan() {
					line := s.Text()
					if line == "" {
						break
					}
					parts := strings.SplitN(line, "=", 2)
					if len(parts) < 2 {
						continue
					}
					key, value := parts[0], parts[1]
					if key == "url" {
						u, err := url.Parse(value)
						if err != nil {
							return err
						}
						wants["protocol"] = u.Scheme
						wants["host"] = u.Host
						wants["path"] = u.Path
						wants["username"] = u.User.Username()
						wants["password"], _ = u.User.Password()
					} else {
						wants[key] = value
					}
				}

				if len(wants["host"]) == 0 {
					log.Fatal("Require hostname")
				} else if len(wants["protocol"]) == 0 {
					wants["protocol"] = "http"
				}

				userConfig := config.GetLoginByHost(wants["host"])
				if userConfig == nil {
					log.Fatal("host not exists")
				} else if len(userConfig.Token) == 0 {
					log.Fatal("User no set")
				}

				host, err := url.Parse(userConfig.URL)
				if err != nil {
					return err
				}

				if userConfig.TokenExpiry > 0 && time.Now().Unix() > userConfig.TokenExpiry {

					err = auth.RefreshAccessToken(userConfig)
					if err != nil {
						return err
					}

					refreshedConfig := config.GetLoginByHost(wants["host"])
					if refreshedConfig != nil {
						userConfig = refreshedConfig
					}
				}

				_, err = fmt.Fprintf(os.Stdout, "protocol=%s\nhost=%s\nusername=%s\npassword=%s\n", host.Scheme, host.Host, userConfig.User, userConfig.Token)
				if err != nil {
					return err
				}

				return nil
			},
		},
	},
}

CmdLoginHelper represents to login a gitea helper.

View Source
var CmdLoginList = cli.Command{
	Name:        "list",
	Aliases:     []string{"ls"},
	Usage:       "List Gitea logins",
	Description: `List Gitea logins`,
	ArgsUsage:   " ",
	Action:      RunLoginList,
	Flags:       []cli.Flag{&flags.OutputFlag},
}

CmdLoginList represents to login a gitea server.

View Source
var CmdLoginOAuthRefresh = cli.Command{
	Name:        "oauth-refresh",
	Usage:       "Refresh an OAuth token",
	Description: "Manually refresh an expired OAuth token. Usually only used when troubleshooting authentication.",
	ArgsUsage:   "[<login name>]",
	Action:      runLoginOAuthRefresh,
}

CmdLoginOAuthRefresh represents a command to refresh an OAuth token

View Source
var CmdLoginSetDefault = cli.Command{
	Name:        "default",
	Usage:       "Get or Set Default Login",
	Description: `Get or Set Default Login`,
	ArgsUsage:   "<Login>",
	Action:      runLoginSetDefault,
	Flags:       []cli.Flag{&flags.OutputFlag},
}

CmdLoginSetDefault represents to login a gitea server.

Functions

func RunLoginDelete

func RunLoginDelete(_ context.Context, cmd *cli.Command) error

RunLoginDelete runs the action of a login delete command

func RunLoginList

func RunLoginList(_ context.Context, cmd *cli.Command) error

RunLoginList list all logins

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL