mirror of
https://gitea.com/gitea/tea.git
synced 2024-11-25 12:01:36 +01:00
Login: add BasicAuth & Interactive (#174)
Add Interactive login move login logic into it's own func add BasicAuth as login method Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: appleboy <appleboy.tw@gmail.com> Reviewed-on: https://gitea.com/gitea/tea/pulls/174 Reviewed-by: Andrew Thornton <art27@cantab.net> Reviewed-by: appleboy <appleboy.tw@gmail.com>
This commit is contained in:
parent
e7ee745488
commit
ed961c795e
172
cmd/login.go
172
cmd/login.go
@ -10,6 +10,10 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/cookiejar"
|
"net/http/cookiejar"
|
||||||
|
"net/url"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"code.gitea.io/sdk/gitea"
|
"code.gitea.io/sdk/gitea"
|
||||||
|
|
||||||
@ -21,7 +25,7 @@ var CmdLogin = cli.Command{
|
|||||||
Name: "login",
|
Name: "login",
|
||||||
Usage: "Log in to a Gitea server",
|
Usage: "Log in to a Gitea server",
|
||||||
Description: `Log in to a Gitea server`,
|
Description: `Log in to a Gitea server`,
|
||||||
Action: runLoginList,
|
Action: runLoginAddInteractive,
|
||||||
Subcommands: []*cli.Command{
|
Subcommands: []*cli.Command{
|
||||||
&cmdLoginList,
|
&cmdLoginList,
|
||||||
&cmdLoginAdd,
|
&cmdLoginAdd,
|
||||||
@ -38,7 +42,6 @@ var cmdLoginAdd = cli.Command{
|
|||||||
Name: "name",
|
Name: "name",
|
||||||
Aliases: []string{"n"},
|
Aliases: []string{"n"},
|
||||||
Usage: "Login name",
|
Usage: "Login name",
|
||||||
Required: true,
|
|
||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "url",
|
Name: "url",
|
||||||
@ -54,7 +57,19 @@ var cmdLoginAdd = cli.Command{
|
|||||||
Value: "",
|
Value: "",
|
||||||
EnvVars: []string{"GITEA_SERVER_TOKEN"},
|
EnvVars: []string{"GITEA_SERVER_TOKEN"},
|
||||||
Usage: "Access token. Can be obtained from Settings > Applications",
|
Usage: "Access token. Can be obtained from Settings > Applications",
|
||||||
Required: true,
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "user",
|
||||||
|
Value: "",
|
||||||
|
EnvVars: []string{"GITEA_SERVER_USER"},
|
||||||
|
Usage: "User for basic auth (will create token)",
|
||||||
|
},
|
||||||
|
&cli.StringFlag{
|
||||||
|
Name: "password",
|
||||||
|
Aliases: []string{"pwd"},
|
||||||
|
Value: "",
|
||||||
|
EnvVars: []string{"GITEA_SERVER_PASSWORD"},
|
||||||
|
Usage: "Password for basic auth (will create token)",
|
||||||
},
|
},
|
||||||
&cli.StringFlag{
|
&cli.StringFlag{
|
||||||
Name: "ssh-key",
|
Name: "ssh-key",
|
||||||
@ -71,14 +86,100 @@ var cmdLoginAdd = cli.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
func runLoginAdd(ctx *cli.Context) error {
|
func runLoginAdd(ctx *cli.Context) error {
|
||||||
if !ctx.IsSet("url") {
|
return runLoginAddMain(
|
||||||
|
ctx.String("name"),
|
||||||
|
ctx.String("token"),
|
||||||
|
ctx.String("user"),
|
||||||
|
ctx.String("password"),
|
||||||
|
ctx.String("ssh-key"),
|
||||||
|
ctx.String("url"),
|
||||||
|
ctx.Bool("insecure"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func runLoginAddInteractive(ctx *cli.Context) error {
|
||||||
|
var stdin, name, token, user, passwd, sshKey, giteaURL string
|
||||||
|
var insecure = false
|
||||||
|
|
||||||
|
fmt.Print("URL of Gitea instance: ")
|
||||||
|
if _, err := fmt.Scanln(&stdin); err != nil {
|
||||||
|
stdin = ""
|
||||||
|
}
|
||||||
|
giteaURL = strings.TrimSpace(stdin)
|
||||||
|
if len(giteaURL) == 0 {
|
||||||
|
fmt.Println("URL is required!")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
parsedURL, err := url.Parse(giteaURL)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
name = strings.ReplaceAll(strings.Title(parsedURL.Host), ".", "")
|
||||||
|
|
||||||
|
fmt.Print("Name of new Login [" + name + "]: ")
|
||||||
|
if _, err := fmt.Scanln(&stdin); err != nil {
|
||||||
|
stdin = ""
|
||||||
|
}
|
||||||
|
if len(strings.TrimSpace(stdin)) != 0 {
|
||||||
|
name = strings.TrimSpace(stdin)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Print("Do you have a token [Yes/no]: ")
|
||||||
|
if _, err := fmt.Scanln(&stdin); err != nil {
|
||||||
|
stdin = ""
|
||||||
|
}
|
||||||
|
if len(stdin) != 0 && strings.ToLower(stdin[:1]) == "n" {
|
||||||
|
fmt.Print("Username: ")
|
||||||
|
if _, err := fmt.Scanln(&stdin); err != nil {
|
||||||
|
stdin = ""
|
||||||
|
}
|
||||||
|
user = strings.TrimSpace(stdin)
|
||||||
|
|
||||||
|
fmt.Print("Password: ")
|
||||||
|
if _, err := fmt.Scanln(&stdin); err != nil {
|
||||||
|
stdin = ""
|
||||||
|
}
|
||||||
|
passwd = strings.TrimSpace(stdin)
|
||||||
|
} else {
|
||||||
|
fmt.Print("Token: ")
|
||||||
|
if _, err := fmt.Scanln(&stdin); err != nil {
|
||||||
|
stdin = ""
|
||||||
|
}
|
||||||
|
token = strings.TrimSpace(stdin)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Print("Set Optional settings [yes/No]: ")
|
||||||
|
if _, err := fmt.Scanln(&stdin); err != nil {
|
||||||
|
stdin = ""
|
||||||
|
}
|
||||||
|
if len(stdin) != 0 && strings.ToLower(stdin[:1]) == "y" {
|
||||||
|
fmt.Print("SSH Key Path: ")
|
||||||
|
if _, err := fmt.Scanln(&stdin); err != nil {
|
||||||
|
stdin = ""
|
||||||
|
}
|
||||||
|
sshKey = strings.TrimSpace(stdin)
|
||||||
|
|
||||||
|
fmt.Print("Allow Insecure connections [yes/No]: ")
|
||||||
|
if _, err := fmt.Scanln(&stdin); err != nil {
|
||||||
|
stdin = ""
|
||||||
|
}
|
||||||
|
insecure = len(stdin) != 0 && strings.ToLower(stdin[:1]) == "y"
|
||||||
|
}
|
||||||
|
|
||||||
|
return runLoginAddMain(name, token, user, passwd, sshKey, giteaURL, insecure)
|
||||||
|
}
|
||||||
|
|
||||||
|
func runLoginAddMain(name, token, user, passwd, sshKey, giteaURL string, insecure bool) error {
|
||||||
|
|
||||||
|
if len(giteaURL) == 0 {
|
||||||
log.Fatal("You have to input Gitea server URL")
|
log.Fatal("You have to input Gitea server URL")
|
||||||
}
|
}
|
||||||
if !ctx.IsSet("token") {
|
if len(token) == 0 && (len(user)+len(passwd)) == 0 {
|
||||||
log.Fatal("No token found")
|
log.Fatal("No token set")
|
||||||
}
|
} else if len(user) != 0 && len(passwd) == 0 {
|
||||||
if !ctx.IsSet("name") {
|
log.Fatal("No password set")
|
||||||
log.Fatal("You have to set a name for the login")
|
} else if len(user) == 0 && len(passwd) != 0 {
|
||||||
|
log.Fatal("No user set")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := loadConfig(yamlConfigPath)
|
err := loadConfig(yamlConfigPath)
|
||||||
@ -86,8 +187,11 @@ func runLoginAdd(ctx *cli.Context) error {
|
|||||||
log.Fatal("Unable to load config file " + yamlConfigPath)
|
log.Fatal("Unable to load config file " + yamlConfigPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
client := gitea.NewClient(ctx.String("url"), ctx.String("token"))
|
client := gitea.NewClient(giteaURL, token)
|
||||||
if ctx.Bool("insecure") {
|
if len(token) == 0 {
|
||||||
|
client.SetBasicAuth(user, passwd)
|
||||||
|
}
|
||||||
|
if insecure {
|
||||||
cookieJar, _ := cookiejar.New(nil)
|
cookieJar, _ := cookiejar.New(nil)
|
||||||
|
|
||||||
client.SetHTTPClient(&http.Client{
|
client.SetHTTPClient(&http.Client{
|
||||||
@ -97,19 +201,55 @@ func runLoginAdd(ctx *cli.Context) error {
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
u, err := client.GetMyUserInfo()
|
u, err := client.GetMyUserInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(token) == 0 {
|
||||||
|
// create token
|
||||||
|
host, _ := os.Hostname()
|
||||||
|
tl, err := client.ListAccessTokens(gitea.ListAccessTokensOptions{})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
tokenName := host + "-tea"
|
||||||
|
for i := range tl {
|
||||||
|
if tl[i].Name == tokenName {
|
||||||
|
tokenName += time.Now().Format("2006-01-02_15-04-05")
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t, err := client.CreateAccessToken(gitea.CreateAccessTokenOption{Name: tokenName})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
token = t.Token
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println("Login successful! Login name " + u.UserName)
|
fmt.Println("Login successful! Login name " + u.UserName)
|
||||||
|
|
||||||
|
if len(name) == 0 {
|
||||||
|
parsedURL, err := url.Parse(giteaURL)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
name = strings.ReplaceAll(strings.Title(parsedURL.Host), ".", "")
|
||||||
|
for _, l := range config.Logins {
|
||||||
|
if l.Name == name {
|
||||||
|
name += "_" + u.UserName
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
err = addLogin(Login{
|
err = addLogin(Login{
|
||||||
Name: ctx.String("name"),
|
Name: name,
|
||||||
URL: ctx.String("url"),
|
URL: giteaURL,
|
||||||
Token: ctx.String("token"),
|
Token: token,
|
||||||
Insecure: ctx.Bool("insecure"),
|
Insecure: insecure,
|
||||||
SSHKey: ctx.String("ssh-key"),
|
SSHKey: sshKey,
|
||||||
User: u.UserName,
|
User: u.UserName,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user